From c2f3537a322e25b8ffdfcd6ab31b9081f8695995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 17 Sep 2021 16:26:20 +0200 Subject: [PATCH] Separate "main" package from "ghcs" package To make "ghcs" importable, this separates out the `main()` function into its own package that lives under "cmd/ghcs/main". Typically the main package would be called "cmd/ghcs", but we wanted to leave the current ghcs implementation where it is to avoid causing conflicts with current work in progress. Co-authored-by: Jose Garcia --- cmd/ghcs/code.go | 6 +----- cmd/ghcs/common.go | 2 +- cmd/ghcs/create.go | 6 +----- cmd/ghcs/delete.go | 6 +----- cmd/ghcs/list.go | 6 +----- cmd/ghcs/logs.go | 6 +----- cmd/ghcs/main/main.go | 26 ++++++++++++++++++++++++++ cmd/ghcs/ports.go | 6 +----- cmd/ghcs/{main.go => root.go} | 35 ++++++++++++++--------------------- cmd/ghcs/ssh.go | 6 +----- 10 files changed, 48 insertions(+), 57 deletions(-) create mode 100644 cmd/ghcs/main/main.go rename cmd/ghcs/{main.go => root.go} (77%) diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index d905c75ac..245a362be 100644 --- a/cmd/ghcs/code.go +++ b/cmd/ghcs/code.go @@ -1,4 +1,4 @@ -package main +package ghcs import ( "context" @@ -32,10 +32,6 @@ func newCodeCmd() *cobra.Command { return codeCmd } -func init() { - rootCmd.AddCommand(newCodeCmd()) -} - func code(codespaceName string, useInsiders bool) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() diff --git a/cmd/ghcs/common.go b/cmd/ghcs/common.go index e71e3dfe4..f15d2fef6 100644 --- a/cmd/ghcs/common.go +++ b/cmd/ghcs/common.go @@ -1,4 +1,4 @@ -package main +package ghcs // This file defines functions common to the entire ghcs command set. diff --git a/cmd/ghcs/create.go b/cmd/ghcs/create.go index 2125176fd..493a70247 100644 --- a/cmd/ghcs/create.go +++ b/cmd/ghcs/create.go @@ -1,4 +1,4 @@ -package main +package ghcs import ( "context" @@ -42,10 +42,6 @@ func newCreateCmd() *cobra.Command { return createCmd } -func init() { - rootCmd.AddCommand(newCreateCmd()) -} - func create(opts *createOptions) error { ctx := context.Background() apiClient := api.New(os.Getenv("GITHUB_TOKEN")) diff --git a/cmd/ghcs/delete.go b/cmd/ghcs/delete.go index 2c0de43ea..b75def7b6 100644 --- a/cmd/ghcs/delete.go +++ b/cmd/ghcs/delete.go @@ -1,4 +1,4 @@ -package main +package ghcs import ( "context" @@ -47,10 +47,6 @@ func newDeleteCmd() *cobra.Command { return deleteCmd } -func init() { - rootCmd.AddCommand(newDeleteCmd()) -} - func delete_(log *output.Logger, codespaceName string, force bool) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() diff --git a/cmd/ghcs/list.go b/cmd/ghcs/list.go index 72a25becc..09b0ea5b0 100644 --- a/cmd/ghcs/list.go +++ b/cmd/ghcs/list.go @@ -1,4 +1,4 @@ -package main +package ghcs import ( "context" @@ -31,10 +31,6 @@ func newListCmd() *cobra.Command { return listCmd } -func init() { - rootCmd.AddCommand(newListCmd()) -} - func list(opts *listOptions) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() diff --git a/cmd/ghcs/logs.go b/cmd/ghcs/logs.go index f65fa1109..40cac88ed 100644 --- a/cmd/ghcs/logs.go +++ b/cmd/ghcs/logs.go @@ -1,4 +1,4 @@ -package main +package ghcs import ( "context" @@ -36,10 +36,6 @@ func newLogsCmd() *cobra.Command { return logsCmd } -func init() { - rootCmd.AddCommand(newLogsCmd()) -} - func logs(ctx context.Context, log *output.Logger, codespaceName string, follow bool) error { // Ensure all child tasks (port forwarding, remote exec) terminate before return. ctx, cancel := context.WithCancel(ctx) diff --git a/cmd/ghcs/main/main.go b/cmd/ghcs/main/main.go new file mode 100644 index 000000000..01dde1270 --- /dev/null +++ b/cmd/ghcs/main/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "errors" + "fmt" + "io" + "os" + + "github.com/github/ghcs/cmd/ghcs" +) + +func main() { + rootCmd := ghcs.NewRootCmd() + if err := rootCmd.Execute(); err != nil { + explainError(os.Stderr, err) + os.Exit(1) + } +} + +func explainError(w io.Writer, err error) { + if errors.Is(err, ghcs.ErrTokenMissing) { + fmt.Fprintln(w, "The GITHUB_TOKEN environment variable is required. Create a Personal Access Token at https://github.com/settings/tokens/new?scopes=repo") + fmt.Fprintln(w, "Make sure to enable SSO for your organizations after creating the token.") + return + } +} diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index 7bc53c441..052dfd37c 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -1,4 +1,4 @@ -package main +package ghcs import ( "bytes" @@ -47,10 +47,6 @@ func newPortsCmd() *cobra.Command { return portsCmd } -func init() { - rootCmd.AddCommand(newPortsCmd()) -} - func ports(codespaceName string, asJSON bool) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() diff --git a/cmd/ghcs/main.go b/cmd/ghcs/root.go similarity index 77% rename from cmd/ghcs/main.go rename to cmd/ghcs/root.go index 7903dad2a..6db4144a8 100644 --- a/cmd/ghcs/main.go +++ b/cmd/ghcs/root.go @@ -1,9 +1,8 @@ -package main +package ghcs import ( "errors" "fmt" - "io" "log" "os" "strconv" @@ -14,18 +13,12 @@ import ( "github.com/spf13/cobra" ) -func main() { - if err := rootCmd.Execute(); err != nil { - explainError(os.Stderr, err) - os.Exit(1) - } -} - var version = "DEV" // Replaced in the release build process (by GoReleaser or Homebrew) by the git tag version number. -var rootCmd = newRootCmd() +// GithubToken is a temporary stopgap to make the token configurable by apps that import this package +var GithubToken = os.Getenv("GITHUB_TOKEN") -func newRootCmd() *cobra.Command { +func NewRootCmd() *cobra.Command { var lightstep string root := &cobra.Command{ @@ -40,7 +33,7 @@ token to access the GitHub API with.`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if os.Getenv("GITHUB_TOKEN") == "" { - return errTokenMissing + return ErrTokenMissing } return initLightstep(lightstep) }, @@ -48,18 +41,18 @@ token to access the GitHub API with.`, root.PersistentFlags().StringVar(&lightstep, "lightstep", "", "Lightstep tracing endpoint (service:token@host:port)") + root.AddCommand(newCodeCmd()) + root.AddCommand(newCreateCmd()) + root.AddCommand(newDeleteCmd()) + root.AddCommand(newListCmd()) + root.AddCommand(newLogsCmd()) + root.AddCommand(newPortsCmd()) + root.AddCommand(newSSHCmd()) + return root } -var errTokenMissing = errors.New("GITHUB_TOKEN is missing") - -func explainError(w io.Writer, err error) { - if errors.Is(err, errTokenMissing) { - fmt.Fprintln(w, "The GITHUB_TOKEN environment variable is required. Create a Personal Access Token at https://github.com/settings/tokens/new?scopes=repo") - fmt.Fprintln(w, "Make sure to enable SSO for your organizations after creating the token.") - return - } -} +var ErrTokenMissing = errors.New("GITHUB_TOKEN is missing") // initLightstep parses the --lightstep=service:token@host:port flag and // enables tracing if non-empty. diff --git a/cmd/ghcs/ssh.go b/cmd/ghcs/ssh.go index 6d5f2376b..527ae120f 100644 --- a/cmd/ghcs/ssh.go +++ b/cmd/ghcs/ssh.go @@ -1,4 +1,4 @@ -package main +package ghcs import ( "context" @@ -32,10 +32,6 @@ func newSSHCmd() *cobra.Command { return sshCmd } -func init() { - rootCmd.AddCommand(newSSHCmd()) -} - func ssh(ctx context.Context, sshProfile, codespaceName string, localSSHServerPort int) error { // Ensure all child tasks (e.g. port forwarding) terminate before return. ctx, cancel := context.WithCancel(ctx)