X Tutup
Skip to content

Commit badbf51

Browse files
committed
codespace list: support --json and --template export flags
1 parent 884d73d commit badbf51

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

internal/codespaces/api/api.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"io/ioutil"
3636
"net/http"
3737
"net/url"
38+
"reflect"
3839
"regexp"
3940
"strconv"
4041
"strings"
@@ -176,6 +177,43 @@ type CodespaceConnection struct {
176177
HostPublicKeys []string `json:"hostPublicKeys"`
177178
}
178179

180+
var CodespaceFields = []string{
181+
"name",
182+
"owner",
183+
"repository",
184+
"state",
185+
"gitStatus",
186+
"createdAt",
187+
"lastUsedAt",
188+
}
189+
190+
func (c *Codespace) ExportData(fields []string) *map[string]interface{} {
191+
v := reflect.ValueOf(c).Elem()
192+
data := map[string]interface{}{}
193+
194+
for _, f := range fields {
195+
switch f {
196+
case "owner":
197+
data[f] = c.Owner.Login
198+
case "repository":
199+
data[f] = c.Repository.FullName
200+
case "gitStatus":
201+
data[f] = map[string]interface{}{
202+
"ref": c.GitStatus.Ref,
203+
"hasUnpushedChanges": c.GitStatus.HasUnpushedChanges,
204+
"hasUncommitedChanges": c.GitStatus.HasUncommitedChanges,
205+
}
206+
default:
207+
sf := v.FieldByNameFunc(func(s string) bool {
208+
return strings.EqualFold(f, s)
209+
})
210+
data[f] = sf.Interface()
211+
}
212+
}
213+
214+
return &data
215+
}
216+
179217
// ListCodespaces returns a list of codespaces for the user. Pass a negative limit to request all pages from
180218
// the API until all codespaces have been fetched.
181219
func (a *API) ListCodespaces(ctx context.Context, limit int) (codespaces []*Codespace, err error) {

pkg/cmd/codespace/list.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
)
1313

1414
func newListCmd(app *App) *cobra.Command {
15-
var asJSON bool
1615
var limit int
16+
var exporter cmdutil.Exporter
1717

1818
listCmd := &cobra.Command{
1919
Use: "list",
@@ -24,17 +24,17 @@ func newListCmd(app *App) *cobra.Command {
2424
return cmdutil.FlagErrorf("invalid limit: %v", limit)
2525
}
2626

27-
return app.List(cmd.Context(), asJSON, limit)
27+
return app.List(cmd.Context(), limit, exporter)
2828
},
2929
}
3030

31-
listCmd.Flags().BoolVar(&asJSON, "json", false, "Output as JSON")
3231
listCmd.Flags().IntVarP(&limit, "limit", "L", 30, "Maximum number of codespaces to list")
32+
cmdutil.AddJSONFlags(listCmd, &exporter, api.CodespaceFields)
3333

3434
return listCmd
3535
}
3636

37-
func (a *App) List(ctx context.Context, asJSON bool, limit int) error {
37+
func (a *App) List(ctx context.Context, limit int, exporter cmdutil.Exporter) error {
3838
a.StartProgressIndicatorWithLabel("Fetching codespaces")
3939
codespaces, err := a.apiClient.ListCodespaces(ctx, limit)
4040
a.StopProgressIndicator()
@@ -47,6 +47,10 @@ func (a *App) List(ctx context.Context, asJSON bool, limit int) error {
4747
}
4848
defer a.io.StopPager()
4949

50+
if exporter != nil {
51+
return exporter.Write(a.io, codespaces)
52+
}
53+
5054
tp := utils.NewTablePrinter(a.io)
5155
if tp.IsTTY() {
5256
tp.AddField("NAME", nil, nil)

0 commit comments

Comments
 (0)
X Tutup