cli/pkg/cmd/codespace/select.go
2022-04-01 12:30:55 -07:00

68 lines
1.4 KiB
Go

package codespace
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
)
type selectOptions struct {
filePath string
}
func newSelectCmd(app *App) *cobra.Command {
opts := selectOptions{}
selectCmd := &cobra.Command{
Use: "select",
Short: "Select a codespace",
Hidden: true,
Args: noArgsConstraint,
RunE: func(cmd *cobra.Command, args []string) error {
return app.Select(cmd.Context(), "", opts)
},
}
selectCmd.Flags().StringVarP(&opts.filePath, "file", "f", "", "Output file path")
return selectCmd
}
// Hidden codespace `select` command allows to reuse existing codespace selection
// dialog by external GH CLI extensions. By default output selected codespace name
// into `stdout`. Pass `--file`(`-f`) flag along with a file path to output selected
// codespace name into a file instead.
//
// ## Examples
//
// With `stdout` output:
//
// ```shell
// gh codespace select
// ```
//
// With `into-a-file` output:
//
// ```shell
// gh codespace select --file /tmp/selected_codespace.txt
// ```
func (a *App) Select(ctx context.Context, name string, opts selectOptions) error {
codespace, err := getOrChooseCodespace(ctx, a.apiClient, name)
if err != nil {
return err
}
if opts.filePath != "" {
f, err := os.Create(opts.filePath)
if err != nil {
return err
}
f.WriteString(codespace.Name);
return nil
}
a.io.Out.Write([]byte(fmt.Sprintf("%s\n", codespace.Name)));
return nil
}