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 <josebalius@github.com>
This commit is contained in:
Mislav Marohnić 2021-09-17 16:26:20 +02:00
parent a4f1fa076b
commit c2f3537a32
10 changed files with 48 additions and 57 deletions

View file

@ -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()

View file

@ -1,4 +1,4 @@
package main
package ghcs
// This file defines functions common to the entire ghcs command set.

View file

@ -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"))

View file

@ -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()

View file

@ -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()

View file

@ -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)

26
cmd/ghcs/main/main.go Normal file
View file

@ -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
}
}

View file

@ -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()

View file

@ -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.

View file

@ -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)