-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
select.go
83 lines (70 loc) · 1.65 KB
/
select.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package codespace
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
)
type selectOptions struct {
filePath string
selector *CodespaceSelector
}
func newSelectCmd(app *App) *cobra.Command {
var (
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)
},
}
opts.selector = AddCodespaceSelector(selectCmd, app.apiClient)
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, opts selectOptions) (err error) {
codespace, err := opts.selector.Select(ctx)
if err != nil {
return err
}
if opts.filePath != "" {
f, err := os.Create(opts.filePath)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer safeClose(f, &err)
_, err = f.WriteString(codespace.Name)
if err != nil {
return fmt.Errorf("failed to write codespace name to output file: %w", err)
}
return nil
}
fmt.Fprintln(a.io.Out, codespace.Name)
return nil
}