Skip to content

Commit

Permalink
Merge pull request cli#4591 from cli/codespaces-json-export
Browse files Browse the repository at this point in the history
Add export functionality to codespace commands
  • Loading branch information
mislav authored Oct 25, 2021
2 parents ba4a7c6 + 905cb3b commit 8d9e8e3
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 220 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/muesli/reflow v0.2.1-0.20210502190812-c80126ec2ad5
github.com/muesli/termenv v0.9.0
github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38
github.com/olekukonko/tablewriter v0.0.5
github.com/opentracing/opentracing-go v1.1.0
github.com/shurcooL/githubv4 v0.0.0-20200928013246-d292edc3691b
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f
Expand Down
39 changes: 39 additions & 0 deletions internal/codespaces/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -177,6 +178,44 @@ type CodespaceConnection struct {
HostPublicKeys []string `json:"hostPublicKeys"`
}

// CodespaceFields is the list of exportable fields for a codespace.
var CodespaceFields = []string{
"name",
"owner",
"repository",
"state",
"gitStatus",
"createdAt",
"lastUsedAt",
}

func (c *Codespace) ExportData(fields []string) *map[string]interface{} {
v := reflect.ValueOf(c).Elem()
data := map[string]interface{}{}

for _, f := range fields {
switch f {
case "owner":
data[f] = c.Owner.Login
case "repository":
data[f] = c.Repository.FullName
case "gitStatus":
data[f] = map[string]interface{}{
"ref": c.GitStatus.Ref,
"hasUnpushedChanges": c.GitStatus.HasUnpushedChanges,
"hasUncommitedChanges": c.GitStatus.HasUncommitedChanges,
}
default:
sf := v.FieldByNameFunc(func(s string) bool {
return strings.EqualFold(f, s)
})
data[f] = sf.Interface()
}
}

return &data
}

// ListCodespaces returns a list of codespaces for the user. Pass a negative limit to request all pages from
// the API until all codespaces have been fetched.
func (a *API) ListCodespaces(ctx context.Context, limit int) (codespaces []*Codespace, err error) {
Expand Down
12 changes: 8 additions & 4 deletions pkg/cmd/codespace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

func newListCmd(app *App) *cobra.Command {
var asJSON bool
var limit int
var exporter cmdutil.Exporter

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

return app.List(cmd.Context(), asJSON, limit)
return app.List(cmd.Context(), limit, exporter)
},
}

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

return listCmd
}

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

if exporter != nil {
return exporter.Write(a.io, codespaces)
}

tp := utils.NewTablePrinter(a.io)
if tp.IsTTY() {
tp.AddField("NAME", nil, nil)
Expand Down
55 changes: 0 additions & 55 deletions pkg/cmd/codespace/output/format_json.go

This file was deleted.

31 changes: 0 additions & 31 deletions pkg/cmd/codespace/output/format_table.go

This file was deleted.

25 changes: 0 additions & 25 deletions pkg/cmd/codespace/output/format_tsv.go

This file was deleted.

78 changes: 0 additions & 78 deletions pkg/cmd/codespace/output/logger.go

This file was deleted.

Loading

0 comments on commit 8d9e8e3

Please sign in to comment.