X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -176,6 +177,44 @@ type CodespaceConnection struct {
HostPublicKeys []string `json:"hostPublicKeys"`
}

// CodespaceFields is the list of exportable fields for a codespace.
var CodespaceFields = []string{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All exported declarations should have a doc comment.

"name",
"owner",
"repository",
"state",
"gitStatus",
"createdAt",
"lastUsedAt",
}

func (c *Codespace) ExportData(fields []string) *map[string]interface{} {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Go map value is a reference to a hash table, so you almost never need *map types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good to know! I will address this across all commands in a followup PR.

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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when f is misspelled? I suspect the map has an (f, nil) entry. It would be better to log.Fatal.

This comment was marked as spam.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It panics, but the user is not able to pass in misspelled fields because the list of requested fields is always checked against a list of allowed fields.

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
X Tutup