X Tutup
Skip to content

Commit 8d9e8e3

Browse files
authored
Merge pull request cli#4591 from cli/codespaces-json-export
Add export functionality to codespace commands
2 parents ba4a7c6 + 905cb3b commit 8d9e8e3

File tree

8 files changed

+139
-220
lines changed

8 files changed

+139
-220
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ require (
2626
github.com/muesli/reflow v0.2.1-0.20210502190812-c80126ec2ad5
2727
github.com/muesli/termenv v0.9.0
2828
github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38
29-
github.com/olekukonko/tablewriter v0.0.5
3029
github.com/opentracing/opentracing-go v1.1.0
3130
github.com/shurcooL/githubv4 v0.0.0-20200928013246-d292edc3691b
3231
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f

internal/codespaces/api/api.go

Lines changed: 39 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"
@@ -177,6 +178,44 @@ type CodespaceConnection struct {
177178
HostPublicKeys []string `json:"hostPublicKeys"`
178179
}
179180

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

pkg/cmd/codespace/output/format_json.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

pkg/cmd/codespace/output/format_table.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

pkg/cmd/codespace/output/format_tsv.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

pkg/cmd/codespace/output/logger.go

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)
X Tutup