From 530c0244f9461874b206a4a1324759d8032ec745 Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Thu, 19 Aug 2021 17:37:57 -0400 Subject: [PATCH 1/2] Add support to `code` for VS Code Insiders --- cmd/ghcs/code.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index ac5bffe8b..9a5abddfb 100644 --- a/cmd/ghcs/code.go +++ b/cmd/ghcs/code.go @@ -12,8 +12,14 @@ import ( "github.com/spf13/cobra" ) +type CodeOptions struct { + UseInsiders bool +} + func NewCodeCmd() *cobra.Command { - return &cobra.Command{ + opts := &CodeOptions{} + + codeCmd := &cobra.Command{ Use: "code []", Short: "Open a Codespace in VS Code", Args: cobra.MaximumNArgs(1), @@ -22,16 +28,20 @@ func NewCodeCmd() *cobra.Command { if len(args) > 0 { codespaceName = args[0] } - return Code(codespaceName) + return Code(codespaceName, opts) }, } + + codeCmd.Flags().BoolVar(&opts.UseInsiders, "insiders", false, "Use the insiders version of VS Code") + + return codeCmd } func init() { rootCmd.AddCommand(NewCodeCmd()) } -func Code(codespaceName string) error { +func Code(codespaceName string, opts *CodeOptions) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() @@ -51,13 +61,17 @@ func Code(codespaceName string) error { codespaceName = codespace.Name } - if err := open.Run(vscodeProtocolURL(codespaceName)); err != nil { + if err := open.Run(vscodeProtocolURL(codespaceName, opts.UseInsiders)); err != nil { return fmt.Errorf("error opening vscode URL") } return nil } -func vscodeProtocolURL(codespaceName string) string { - return fmt.Sprintf("vscode://github.codespaces/connect?name=%s", url.QueryEscape(codespaceName)) +func vscodeProtocolURL(codespaceName string, useInsiders bool) string { + application := "vscode" + if useInsiders { + application = "vscode-insiders" + } + return fmt.Sprintf("%s://github.codespaces/connect?name=%s", application, url.QueryEscape(codespaceName)) } From ae88091fd8276d57437aa7f44053f1462f3412e2 Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Mon, 23 Aug 2021 12:01:13 -0400 Subject: [PATCH 2/2] Replace options struct with variable --- cmd/ghcs/code.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index 9a5abddfb..81dbdbb2c 100644 --- a/cmd/ghcs/code.go +++ b/cmd/ghcs/code.go @@ -12,12 +12,8 @@ import ( "github.com/spf13/cobra" ) -type CodeOptions struct { - UseInsiders bool -} - func NewCodeCmd() *cobra.Command { - opts := &CodeOptions{} + useInsiders := false codeCmd := &cobra.Command{ Use: "code []", @@ -28,11 +24,11 @@ func NewCodeCmd() *cobra.Command { if len(args) > 0 { codespaceName = args[0] } - return Code(codespaceName, opts) + return Code(codespaceName, useInsiders) }, } - codeCmd.Flags().BoolVar(&opts.UseInsiders, "insiders", false, "Use the insiders version of VS Code") + codeCmd.Flags().BoolVar(&useInsiders, "insiders", false, "Use the insiders version of VS Code") return codeCmd } @@ -41,7 +37,7 @@ func init() { rootCmd.AddCommand(NewCodeCmd()) } -func Code(codespaceName string, opts *CodeOptions) error { +func Code(codespaceName string, useInsiders bool) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() @@ -61,7 +57,7 @@ func Code(codespaceName string, opts *CodeOptions) error { codespaceName = codespace.Name } - if err := open.Run(vscodeProtocolURL(codespaceName, opts.UseInsiders)); err != nil { + if err := open.Run(vscodeProtocolURL(codespaceName, useInsiders)); err != nil { return fmt.Errorf("error opening vscode URL") }