mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: remove server subcommand from slim binaries (#5747)
This commit is contained in:
@@ -181,3 +181,26 @@ func workspaceAgent() *cobra.Command {
|
||||
cliflag.StringVarP(cmd.Flags(), &pprofAddress, "pprof-address", "", "CODER_AGENT_PPROF_ADDRESS", "127.0.0.1:6060", "The address to serve pprof.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func serveHandler(ctx context.Context, logger slog.Logger, handler http.Handler, addr, name string) (closeFunc func()) {
|
||||
logger.Debug(ctx, "http server listening", slog.F("addr", addr), slog.F("name", name))
|
||||
|
||||
// ReadHeaderTimeout is purposefully not enabled. It caused some issues with
|
||||
// websockets over the dev tunnel.
|
||||
// See: https://github.com/coder/coder/pull/3730
|
||||
//nolint:gosec
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: handler,
|
||||
}
|
||||
go func() {
|
||||
err := srv.ListenAndServe()
|
||||
if err != nil && !xerrors.Is(err, http.ErrServerClosed) {
|
||||
logger.Error(ctx, "http server listen", slog.F("name", name), slog.Error(err))
|
||||
}
|
||||
}()
|
||||
|
||||
return func() {
|
||||
_ = srv.Close()
|
||||
}
|
||||
}
|
||||
|
||||
+12
-2
@@ -213,12 +213,22 @@ func versionCmd() *cobra.Command {
|
||||
Short: "Show coder version",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var str strings.Builder
|
||||
_, _ = str.WriteString(fmt.Sprintf("Coder %s", buildinfo.Version()))
|
||||
_, _ = str.WriteString("Coder ")
|
||||
if buildinfo.IsAGPL() {
|
||||
_, _ = str.WriteString("(AGPL) ")
|
||||
}
|
||||
_, _ = str.WriteString(buildinfo.Version())
|
||||
buildTime, valid := buildinfo.Time()
|
||||
if valid {
|
||||
_, _ = str.WriteString(" " + buildTime.Format(time.UnixDate))
|
||||
}
|
||||
_, _ = str.WriteString("\r\n" + buildinfo.ExternalURL() + "\r\n")
|
||||
_, _ = str.WriteString("\r\n" + buildinfo.ExternalURL() + "\r\n\r\n")
|
||||
|
||||
if buildinfo.IsSlim() {
|
||||
_, _ = str.WriteString(fmt.Sprintf("Slim build of Coder, does not support the %s subcommand.\n", cliui.Styles.Code.Render("server")))
|
||||
} else {
|
||||
_, _ = str.WriteString(fmt.Sprintf("Full build of Coder, supports the %s subcommand.\n", cliui.Styles.Code.Render("server")))
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprint(cmd.OutOrStdout(), str.String())
|
||||
return nil
|
||||
|
||||
+2
-23
@@ -1,3 +1,5 @@
|
||||
//go:build !slim
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
@@ -1370,29 +1372,6 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
|
||||
}, nil
|
||||
}
|
||||
|
||||
func serveHandler(ctx context.Context, logger slog.Logger, handler http.Handler, addr, name string) (closeFunc func()) {
|
||||
logger.Debug(ctx, "http server listening", slog.F("addr", addr), slog.F("name", name))
|
||||
|
||||
// ReadHeaderTimeout is purposefully not enabled. It caused some issues with
|
||||
// websockets over the dev tunnel.
|
||||
// See: https://github.com/coder/coder/pull/3730
|
||||
//nolint:gosec
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: handler,
|
||||
}
|
||||
go func() {
|
||||
err := srv.ListenAndServe()
|
||||
if err != nil && !xerrors.Is(err, http.ErrServerClosed) {
|
||||
logger.Error(ctx, "http server listen", slog.F("name", name), slog.Error(err))
|
||||
}
|
||||
}()
|
||||
|
||||
return func() {
|
||||
_ = srv.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// embeddedPostgresURL returns the URL for the embedded PostgreSQL deployment.
|
||||
func embeddedPostgresURL(cfg config.Root) (string, error) {
|
||||
pgPassword, err := cfg.PostgresPassword().Read()
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
//go:build slim
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/coder/coder/cli/cliui"
|
||||
"github.com/coder/coder/cli/deployment"
|
||||
"github.com/coder/coder/coderd"
|
||||
)
|
||||
|
||||
func Server(vip *viper.Viper, _ func(context.Context, *coderd.Options) (*coderd.API, io.Closer, error)) *cobra.Command {
|
||||
root := &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "Start a Coder server",
|
||||
Hidden: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
serverUnsupported(cmd.ErrOrStderr())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var pgRawURL bool
|
||||
postgresBuiltinURLCmd := &cobra.Command{
|
||||
Use: "postgres-builtin-url",
|
||||
Short: "Output the connection URL for the built-in PostgreSQL deployment.",
|
||||
Hidden: true,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
serverUnsupported(cmd.ErrOrStderr())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
postgresBuiltinServeCmd := &cobra.Command{
|
||||
Use: "postgres-builtin-serve",
|
||||
Short: "Run the built-in PostgreSQL deployment.",
|
||||
Hidden: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
serverUnsupported(cmd.ErrOrStderr())
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// We still have to attach the flags to the commands so users don't get
|
||||
// an error when they try to use them.
|
||||
postgresBuiltinURLCmd.Flags().BoolVar(&pgRawURL, "raw-url", false, "Output the raw connection URL instead of a psql command.")
|
||||
postgresBuiltinServeCmd.Flags().BoolVar(&pgRawURL, "raw-url", false, "Output the raw connection URL instead of a psql command.")
|
||||
|
||||
root.AddCommand(postgresBuiltinURLCmd, postgresBuiltinServeCmd)
|
||||
|
||||
deployment.AttachFlags(root.Flags(), vip, false)
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
func serverUnsupported(w io.Writer) {
|
||||
_, _ = fmt.Fprintf(w, "You are using a 'slim' build of Coder, which does not support the %s subcommand.\n", cliui.Styles.Code.Render("server"))
|
||||
_, _ = fmt.Fprintln(w, "")
|
||||
_, _ = fmt.Fprintln(w, "Please use a build of Coder from GitHub releases:")
|
||||
_, _ = fmt.Fprintln(w, " https://github.com/coder/coder/releases")
|
||||
os.Exit(1)
|
||||
}
|
||||
Reference in New Issue
Block a user