Skip to content

Commit 38ff786

Browse files
committed
cmd/ghcs: style tweaks
1 parent a48f107 commit 38ff786

File tree

10 files changed

+92
-96
lines changed

10 files changed

+92
-96
lines changed

api/api.go

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO(adonovan): rename to package codespaces, and codespaces.Client?
12
package api
23

34
import (
@@ -9,7 +10,6 @@ import (
910
"fmt"
1011
"io/ioutil"
1112
"net/http"
12-
"sort"
1313
"strconv"
1414
"strings"
1515
)
@@ -29,10 +29,6 @@ type User struct {
2929
Login string `json:"login"`
3030
}
3131

32-
type errResponse struct {
33-
Message string `json:"message"`
34-
}
35-
3632
func (a *API) GetUser(ctx context.Context) (*User, error) {
3733
req, err := http.NewRequest(http.MethodGet, githubAPI+"/user", nil)
3834
if err != nil {
@@ -63,7 +59,9 @@ func (a *API) GetUser(ctx context.Context) (*User, error) {
6359
}
6460

6561
func (a *API) errorResponse(b []byte) error {
66-
var response errResponse
62+
var response struct {
63+
Message string `json:"message"`
64+
}
6765
if err := json.Unmarshal(b, &response); err != nil {
6866
return fmt.Errorf("error unmarshaling error response: %v", err)
6967
}
@@ -104,14 +102,6 @@ func (a *API) GetRepository(ctx context.Context, nwo string) (*Repository, error
104102
return &response, nil
105103
}
106104

107-
type Codespaces []*Codespace
108-
109-
func (c Codespaces) SortByCreatedAt() {
110-
sort.Slice(c, func(i, j int) bool {
111-
return c[i].CreatedAt > c[j].CreatedAt
112-
})
113-
}
114-
115105
type Codespace struct {
116106
Name string `json:"name"`
117107
GUID string `json:"guid"`
@@ -139,7 +129,7 @@ type CodespaceEnvironmentConnection struct {
139129
RelaySAS string `json:"relaySas"`
140130
}
141131

142-
func (a *API) ListCodespaces(ctx context.Context, user *User) (Codespaces, error) {
132+
func (a *API) ListCodespaces(ctx context.Context, user *User) ([]*Codespace, error) {
143133
req, err := http.NewRequest(
144134
http.MethodGet, githubAPI+"/vscs_internal/user/"+user.Login+"/codespaces", nil,
145135
)
@@ -162,9 +152,9 @@ func (a *API) ListCodespaces(ctx context.Context, user *User) (Codespaces, error
162152
return nil, a.errorResponse(b)
163153
}
164154

165-
response := struct {
166-
Codespaces Codespaces `json:"codespaces"`
167-
}{}
155+
var response struct {
156+
Codespaces []*Codespace `json:"codespaces"`
157+
}
168158
if err := json.Unmarshal(b, &response); err != nil {
169159
return nil, fmt.Errorf("error unmarshaling response: %v", err)
170160
}
@@ -297,14 +287,12 @@ func (a *API) GetCodespaceRegionLocation(ctx context.Context) (string, error) {
297287
return response.Current, nil
298288
}
299289

300-
type Skus []*Sku
301-
302-
type Sku struct {
290+
type SKU struct {
303291
Name string `json:"name"`
304292
DisplayName string `json:"display_name"`
305293
}
306294

307-
func (a *API) GetCodespacesSkus(ctx context.Context, user *User, repository *Repository, location string) (Skus, error) {
295+
func (a *API) GetCodespacesSkus(ctx context.Context, user *User, repository *Repository, location string) ([]*SKU, error) {
308296
req, err := http.NewRequest(http.MethodGet, githubAPI+"/vscs_internal/user/"+user.Login+"/skus", nil)
309297
if err != nil {
310298
return nil, fmt.Errorf("err creating request: %v", err)
@@ -326,14 +314,14 @@ func (a *API) GetCodespacesSkus(ctx context.Context, user *User, repository *Rep
326314
return nil, fmt.Errorf("error reading response body: %v", err)
327315
}
328316

329-
response := struct {
330-
Skus Skus `json:"skus"`
331-
}{}
317+
var response struct {
318+
SKUs []*SKU `json:"skus"`
319+
}
332320
if err := json.Unmarshal(b, &response); err != nil {
333321
return nil, fmt.Errorf("error unmarshaling response: %v", err)
334322
}
335323

336-
return response.Skus, nil
324+
return response.SKUs, nil
337325
}
338326

339327
type createCodespaceRequest struct {

cmd/ghcs/code.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
func NewCodeCmd() *cobra.Command {
15+
func newCodeCmd() *cobra.Command {
1616
useInsiders := false
1717

1818
codeCmd := &cobra.Command{
@@ -24,7 +24,7 @@ func NewCodeCmd() *cobra.Command {
2424
if len(args) > 0 {
2525
codespaceName = args[0]
2626
}
27-
return Code(codespaceName, useInsiders)
27+
return code(codespaceName, useInsiders)
2828
},
2929
}
3030

@@ -34,10 +34,10 @@ func NewCodeCmd() *cobra.Command {
3434
}
3535

3636
func init() {
37-
rootCmd.AddCommand(NewCodeCmd())
37+
rootCmd.AddCommand(newCodeCmd())
3838
}
3939

40-
func Code(codespaceName string, useInsiders bool) error {
40+
func code(codespaceName string, useInsiders bool) error {
4141
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
4242
ctx := context.Background()
4343

cmd/ghcs/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func newCreateCmd() *cobra.Command {
2222
Short: "Create a Codespace",
2323
Args: cobra.NoArgs,
2424
RunE: func(cmd *cobra.Command, args []string) error {
25-
return Create()
25+
return create()
2626
},
2727
}
2828

@@ -37,7 +37,7 @@ func init() {
3737
rootCmd.AddCommand(newCreateCmd())
3838
}
3939

40-
func Create() error {
40+
func create() error {
4141
ctx := context.Background()
4242
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
4343
locationCh := getLocation(ctx, apiClient)
@@ -176,7 +176,7 @@ func getMachineName(ctx context.Context, user *api.User, repo *api.Repository, l
176176
}
177177

178178
skuNames := make([]string, 0, len(skus))
179-
skuByName := make(map[string]*api.Sku)
179+
skuByName := make(map[string]*api.SKU)
180180
for _, sku := range skus {
181181
nameParts := camelcase.Split(sku.Name)
182182
machineName := strings.Title(strings.ToLower(nameParts[0]))

cmd/ghcs/delete.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
func NewDeleteCmd() *cobra.Command {
15+
func newDeleteCmd() *cobra.Command {
1616
deleteCmd := &cobra.Command{
1717
Use: "delete [<codespace>]",
1818
Short: "Delete a Codespace",
@@ -22,7 +22,7 @@ func NewDeleteCmd() *cobra.Command {
2222
if len(args) > 0 {
2323
codespaceName = args[0]
2424
}
25-
return Delete(codespaceName)
25+
return delete_(codespaceName)
2626
},
2727
}
2828

@@ -31,7 +31,7 @@ func NewDeleteCmd() *cobra.Command {
3131
Short: "Delete all Codespaces for the current user",
3232
Args: cobra.NoArgs,
3333
RunE: func(cmd *cobra.Command, args []string) error {
34-
return DeleteAll()
34+
return deleteAll()
3535
},
3636
}
3737

@@ -40,7 +40,7 @@ func NewDeleteCmd() *cobra.Command {
4040
Short: "Delete all Codespaces for a repository",
4141
Args: cobra.ExactArgs(1),
4242
RunE: func(cmd *cobra.Command, args []string) error {
43-
return DeleteByRepo(args[0])
43+
return deleteByRepo(args[0])
4444
},
4545
}
4646

@@ -50,10 +50,10 @@ func NewDeleteCmd() *cobra.Command {
5050
}
5151

5252
func init() {
53-
rootCmd.AddCommand(NewDeleteCmd())
53+
rootCmd.AddCommand(newDeleteCmd())
5454
}
5555

56-
func Delete(codespaceName string) error {
56+
func delete_(codespaceName string) error {
5757
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
5858
ctx := context.Background()
5959
log := output.NewLogger(os.Stdout, os.Stderr, false)
@@ -74,10 +74,10 @@ func Delete(codespaceName string) error {
7474

7575
log.Println("Codespace deleted.")
7676

77-
return List(&ListOptions{})
77+
return list(&listOptions{})
7878
}
7979

80-
func DeleteAll() error {
80+
func deleteAll() error {
8181
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
8282
ctx := context.Background()
8383
log := output.NewLogger(os.Stdout, os.Stderr, false)
@@ -105,10 +105,10 @@ func DeleteAll() error {
105105
log.Printf("Codespace deleted: %s\n", c.Name)
106106
}
107107

108-
return List(&ListOptions{})
108+
return list(&listOptions{})
109109
}
110110

111-
func DeleteByRepo(repo string) error {
111+
func deleteByRepo(repo string) error {
112112
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
113113
ctx := context.Background()
114114
log := output.NewLogger(os.Stdout, os.Stderr, false)
@@ -146,5 +146,5 @@ func DeleteByRepo(repo string) error {
146146
return fmt.Errorf("No codespace was found for repository: %s", repo)
147147
}
148148

149-
return List(&ListOptions{})
149+
return list(&listOptions{})
150150
}

cmd/ghcs/list.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
type ListOptions struct {
14-
AsJSON bool
13+
type listOptions struct {
14+
asJSON bool
1515
}
1616

17-
func NewListCmd() *cobra.Command {
18-
opts := &ListOptions{}
17+
func newListCmd() *cobra.Command {
18+
opts := &listOptions{}
1919

2020
listCmd := &cobra.Command{
2121
Use: "list",
2222
Short: "List your Codespaces",
2323
Args: cobra.NoArgs,
2424
RunE: func(cmd *cobra.Command, args []string) error {
25-
return List(opts)
25+
return list(opts)
2626
},
2727
}
2828

29-
listCmd.Flags().BoolVar(&opts.AsJSON, "json", false, "Output as JSON")
29+
listCmd.Flags().BoolVar(&opts.asJSON, "json", false, "Output as JSON")
3030

3131
return listCmd
3232
}
3333

3434
func init() {
35-
rootCmd.AddCommand(NewListCmd())
35+
rootCmd.AddCommand(newListCmd())
3636
}
3737

38-
func List(opts *ListOptions) error {
38+
func list(opts *listOptions) error {
3939
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
4040
ctx := context.Background()
4141

@@ -49,7 +49,7 @@ func List(opts *ListOptions) error {
4949
return fmt.Errorf("error getting codespaces: %v", err)
5050
}
5151

52-
table := output.NewTable(os.Stdout, opts.AsJSON)
52+
table := output.NewTable(os.Stdout, opts.asJSON)
5353
table.SetHeader([]string{"Name", "Repository", "Branch", "State", "Created At"})
5454
for _, codespace := range codespaces {
5555
table.Append([]string{

cmd/ghcs/logs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
func NewLogsCmd() *cobra.Command {
15+
func newLogsCmd() *cobra.Command {
1616
var tail bool
1717

1818
logsCmd := &cobra.Command{
@@ -24,7 +24,7 @@ func NewLogsCmd() *cobra.Command {
2424
if len(args) > 0 {
2525
codespaceName = args[0]
2626
}
27-
return Logs(tail, codespaceName)
27+
return logs(tail, codespaceName)
2828
},
2929
}
3030

@@ -34,10 +34,10 @@ func NewLogsCmd() *cobra.Command {
3434
}
3535

3636
func init() {
37-
rootCmd.AddCommand(NewLogsCmd())
37+
rootCmd.AddCommand(newLogsCmd())
3838
}
3939

40-
func Logs(tail bool, codespaceName string) error {
40+
func logs(tail bool, codespaceName string) error {
4141
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
4242
ctx := context.Background()
4343
log := output.NewLogger(os.Stdout, os.Stderr, false)

cmd/ghcs/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package main
22

3+
// TODO(adonovan): write 'help' commands, in manner of the 'go' tool.
4+
// Document GITHUB_TOKEN.
5+
36
import (
47
"errors"
58
"fmt"
@@ -16,15 +19,15 @@ func main() {
1619
}
1720
}
1821

19-
var Version = "DEV"
22+
var version = "DEV"
2023

2124
var rootCmd = &cobra.Command{
2225
Use: "ghcs",
2326
Long: `Unofficial CLI tool to manage GitHub Codespaces.
2427
2528
Running commands requires the GITHUB_TOKEN environment variable to be set to a
2629
token to access the GitHub API with.`,
27-
Version: Version,
30+
Version: version,
2831

2932
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
3033
if os.Getenv("GITHUB_TOKEN") == "" {
@@ -42,5 +45,5 @@ func explainError(w io.Writer, err error) {
4245
fmt.Fprintln(w, "Make sure to enable SSO for your organizations after creating the token.")
4346
return
4447
}
45-
// fmt.Fprintf(w, "%v\n", err)
48+
fmt.Fprintf(w, "%v\n", err)
4649
}

0 commit comments

Comments
 (0)