Repository: cli/cli
Pull request #4395 (2021-09-30 15:22:57)

pkg/cmd/codespace: move `cmd/ghcs` to `pkg/cmd/codespace`
- Delete pkg/cmd/codespace/main as it is no longer needed in this codebase

I think the next steps will be to separate the command functionality from the application functionality but that can happen in a different PR.



Patch:
diff --git a/cmd/ghcs/main/main.go b/cmd/ghcs/main/main.go
deleted file mode 100644
index ea7345dc9..000000000
--- a/cmd/ghcs/main/main.go
+++ /dev/null
@@ -1,54 +0,0 @@
-package main
-
-import (
-	"errors"
-	"fmt"
-	"io"
-	"net/http"
-	"os"
-
-	"github.com/cli/cli/v2/cmd/ghcs"
-	"github.com/cli/cli/v2/cmd/ghcs/output"
-	"github.com/cli/cli/v2/internal/codespaces/api"
-	"github.com/spf13/cobra"
-)
-
-func main() {
-	token := os.Getenv("GITHUB_TOKEN")
-	rootCmd := ghcs.NewRootCmd(ghcs.NewApp(
-		output.NewLogger(os.Stdout, os.Stderr, false),
-		api.New(token, http.DefaultClient),
-	))
-
-	// Require GITHUB_TOKEN through a Cobra pre-run hook so that Cobra's help system for commands can still
-	// function without the token set.
-	oldPreRun := rootCmd.PersistentPreRunE
-	rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
-		if token == "" {
-			return errTokenMissing
-		}
-		if oldPreRun != nil {
-			return oldPreRun(cmd, args)
-		}
-		return nil
-	}
-
-	if cmd, err := rootCmd.ExecuteC(); err != nil {
-		explainError(os.Stderr, err, cmd)
-		os.Exit(1)
-	}
-}
-
-var errTokenMissing = errors.New("GITHUB_TOKEN is missing")
-
-func explainError(w io.Writer, err error, cmd *cobra.Command) {
-	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
-	}
-	if errors.Is(err, ghcs.ErrTooManyArgs) {
-		_ = cmd.Usage()
-		return
-	}
-}
diff --git a/cmd/ghcs/code.go b/pkg/cmd/codespace/code.go
similarity index 100%
rename from cmd/ghcs/code.go
rename to pkg/cmd/codespace/code.go
diff --git a/cmd/ghcs/common.go b/pkg/cmd/codespace/common.go
similarity index 99%
rename from cmd/ghcs/common.go
rename to pkg/cmd/codespace/common.go
index 64286e3bd..da40c2b86 100644
--- a/cmd/ghcs/common.go
+++ b/pkg/cmd/codespace/common.go
@@ -12,8 +12,8 @@ import (
 
 	"github.com/AlecAivazis/survey/v2"
 	"github.com/AlecAivazis/survey/v2/terminal"
-	"github.com/cli/cli/v2/cmd/ghcs/output"
 	"github.com/cli/cli/v2/internal/codespaces/api"
+	"github.com/cli/cli/v2/pkg/cmd/codespace/output"
 	"github.com/spf13/cobra"
 	"golang.org/x/term"
 )
diff --git a/cmd/ghcs/create.go b/pkg/cmd/codespace/create.go
similarity index 99%
rename from cmd/ghcs/create.go
rename to pkg/cmd/codespace/create.go
index 9f0e6c956..68f6d5a86 100644
--- a/cmd/ghcs/create.go
+++ b/pkg/cmd/codespace/create.go
@@ -8,9 +8,9 @@ import (
 	"strings"
 
 	"github.com/AlecAivazis/survey/v2"
-	"github.com/cli/cli/v2/cmd/ghcs/output"
 	"github.com/cli/cli/v2/internal/codespaces"
 	"github.com/cli/cli/v2/internal/codespaces/api"
+	"github.com/cli/cli/v2/pkg/cmd/codespace/output"
 	"github.com/fatih/camelcase"
 	"github.com/spf13/cobra"
 )
diff --git a/cmd/ghcs/list.go b/pkg/cmd/codespace/list.go
similarity index 96%
rename from cmd/ghcs/list.go
rename to pkg/cmd/codespace/list.go
index 1c45949aa..1f87f1ba1 100644
--- a/cmd/ghcs/list.go
+++ b/pkg/cmd/codespace/list.go
@@ -5,8 +5,8 @@ import (
 	"fmt"
 	"os"
 
-	"github.com/cli/cli/v2/cmd/ghcs/output"
 	"github.com/cli/cli/v2/internal/codespaces/api"
+	"github.com/cli/cli/v2/pkg/cmd/codespace/output"
 	"github.com/spf13/cobra"
 )
 
diff --git a/cmd/ghcs/logs.go b/pkg/cmd/codespace/logs.go
similarity index 100%
rename from cmd/ghcs/logs.go
rename to pkg/cmd/codespace/logs.go
diff --git a/cmd/ghcs/mock_api.go b/pkg/cmd/codespace/mock_api.go
similarity index 100%
rename from cmd/ghcs/mock_api.go
rename to pkg/cmd/codespace/mock_api.go
diff --git a/cmd/ghcs/mock_prompter.go b/pkg/cmd/codespace/mock_prompter.go
similarity index 100%
rename from cmd/ghcs/mock_prompter.go
rename to pkg/cmd/codespace/mock_prompter.go
diff --git a/cmd/ghcs/output/format_json.go b/pkg/cmd/codespace/output/format_json.go
similarity index 100%
rename from cmd/ghcs/output/format_json.go
rename to pkg/cmd/codespace/output/format_json.go
diff --git a/cmd/ghcs/output/format_table.go b/pkg/cmd/codespace/output/format_table.go
similarity index 100%
rename from cmd/ghcs/output/format_table.go
rename to pkg/cmd/codespace/output/format_table.go
diff --git a/cmd/ghcs/output/format_tsv.go b/pkg/cmd/codespace/output/format_tsv.go
similarity index 100%
rename from cmd/ghcs/output/format_tsv.go
rename to pkg/cmd/codespace/output/format_tsv.go
diff --git a/cmd/ghcs/output/logger.go b/pkg/cmd/codespace/output/logger.go
similarity index 100%
rename from cmd/ghcs/output/logger.go
rename to pkg/cmd/codespace/output/logger.go
diff --git a/cmd/ghcs/ports.go b/pkg/cmd/codespace/ports.go
similarity index 99%
rename from cmd/ghcs/ports.go
rename to pkg/cmd/codespace/ports.go
index ea95ecd32..dee726bc4 100644
--- a/cmd/ghcs/ports.go
+++ b/pkg/cmd/codespace/ports.go
@@ -11,9 +11,9 @@ import (
 	"strconv"
 	"strings"
 
-	"github.com/cli/cli/v2/cmd/ghcs/output"
 	"github.com/cli/cli/v2/internal/codespaces"
 	"github.com/cli/cli/v2/internal/codespaces/api"
+	"github.com/cli/cli/v2/pkg/cmd/codespace/output"
 	"github.com/cli/cli/v2/pkg/liveshare"
 	"github.com/muhammadmuzzammil1998/jsonc"
 	"github.com/spf13/cobra"
diff --git a/cmd/ghcs/root.go b/pkg/cmd/codespace/root.go
similarity index 100%
rename from cmd/ghcs/root.go
rename to pkg/cmd/codespace/root.go
diff --git a/cmd/ghcs/ssh.go b/pkg/cmd/codespace/ssh.go
similarity index 100%
rename from cmd/ghcs/ssh.go
rename to pkg/cmd/codespace/ssh.go
diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go
index 45b96b983..f96922f9f 100644
--- a/pkg/cmd/root/root.go
+++ b/pkg/cmd/root/root.go
@@ -5,14 +5,14 @@ import (
 	"sync"
 
 	"github.com/MakeNowJust/heredoc"
-	"github.com/cli/cli/v2/cmd/ghcs"
-	"github.com/cli/cli/v2/cmd/ghcs/output"
 	codespacesAPI "github.com/cli/cli/v2/internal/codespaces/api"
 	actionsCmd "github.com/cli/cli/v2/pkg/cmd/actions"
 	aliasCmd "github.com/cli/cli/v2/pkg/cmd/alias"
 	apiCmd "github.com/cli/cli/v2/pkg/cmd/api"
 	authCmd "github.com/cli/cli/v2/pkg/cmd/auth"
 	browseCmd "github.com/cli/cli/v2/pkg/cmd/browse"
+	codespaceCmd "github.com/cli/cli/v2/pkg/cmd/codespace"
+	"github.com/cli/cli/v2/pkg/cmd/codespace/output"
 	completionCmd "github.com/cli/cli/v2/pkg/cmd/completion"
 	configCmd "github.com/cli/cli/v2/pkg/cmd/config"
 	extensionCmd "github.com/cli/cli/v2/pkg/cmd/extension"
@@ -128,7 +128,7 @@ func bareHTTPClient(f *cmdutil.Factory, version string) func() (*http.Client, er
 }
 
 func newCodespaceCmd(f *cmdutil.Factory) *cobra.Command {
-	cmd := ghcs.NewRootCmd(ghcs.NewApp(
+	cmd := codespaceCmd.NewRootCmd(codespaceCmd.NewApp(
 		output.NewLogger(f.IOStreams.Out, f.IOStreams.ErrOut, !f.IOStreams.IsStdoutTTY()),
 		codespacesAPI.New("", &lazyLoadedHTTPClient{factory: f}),
 	))


Test patch:
diff --git a/cmd/ghcs/delete.go b/pkg/cmd/codespace/delete.go
similarity index 100%
rename from cmd/ghcs/delete.go
rename to pkg/cmd/codespace/delete.go
diff --git a/cmd/ghcs/delete_test.go b/pkg/cmd/codespace/delete_test.go
similarity index 99%
rename from cmd/ghcs/delete_test.go
rename to pkg/cmd/codespace/delete_test.go
index 1c239a74e..35e37fae8 100644
--- a/cmd/ghcs/delete_test.go
+++ b/pkg/cmd/codespace/delete_test.go
@@ -11,8 +11,8 @@ import (
 	"time"
 
 	"github.com/MakeNowJust/heredoc"
-	"github.com/cli/cli/v2/cmd/ghcs/output"
 	"github.com/cli/cli/v2/internal/codespaces/api"
+	"github.com/cli/cli/v2/pkg/cmd/codespace/output"
 )
 
 func TestDelete(t *testing.T) {