@@ -2,44 +2,44 @@ package main
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "os"
7+ "strings"
88
99 "github.com/github/ghcs/api"
10+ "github.com/github/ghcs/cmd/ghcs/output"
11+ "github.com/github/ghcs/internal/codespaces"
1012 "github.com/spf13/cobra"
1113)
1214
1315func NewDeleteCmd () * cobra.Command {
1416 deleteCmd := & cobra.Command {
15- Use : "delete CODESPACE_NAME" ,
16- Short : "Delete a GitHub Codespace." ,
17+ Use : "delete [<codespace>]" ,
18+ Short : "Delete a Codespace" ,
19+ Args : cobra .MaximumNArgs (1 ),
1720 RunE : func (cmd * cobra.Command , args []string ) error {
18- if len (args ) == 0 {
19- return errors .New ("A Codespace name is required." )
21+ var codespaceName string
22+ if len (args ) > 0 {
23+ codespaceName = args [0 ]
2024 }
21- return Delete (args [ 0 ] )
25+ return Delete (codespaceName )
2226 },
2327 }
2428
2529 deleteAllCmd := & cobra.Command {
2630 Use : "all" ,
27- Short : "delete all codespaces " ,
28- Long : "delete all codespaces for the user with the current token" ,
31+ Short : "Delete all Codespaces for the current user " ,
32+ Args : cobra . NoArgs ,
2933 RunE : func (cmd * cobra.Command , args []string ) error {
3034 return DeleteAll ()
3135 },
3236 }
3337
3438 deleteByRepoCmd := & cobra.Command {
35- Use : "repo REPO_NAME" ,
36- Short : "delete all codespaces for the repo" ,
37- Long : `delete all the codespaces that the user with the current token has in this repo.
38- This includes all codespaces in all states.` ,
39+ Use : "repo <repo>" ,
40+ Short : "Delete all Codespaces for a repository" ,
41+ Args : cobra .ExactArgs (1 ),
3942 RunE : func (cmd * cobra.Command , args []string ) error {
40- if len (args ) == 0 {
41- return errors .New ("A Repository name is required." )
42- }
4343 return DeleteByRepo (args [0 ])
4444 },
4545 }
@@ -56,29 +56,31 @@ func init() {
5656func Delete (codespaceName string ) error {
5757 apiClient := api .New (os .Getenv ("GITHUB_TOKEN" ))
5858 ctx := context .Background ()
59+ log := output .NewLogger (os .Stdout , os .Stderr , false )
5960
6061 user , err := apiClient .GetUser (ctx )
6162 if err != nil {
6263 return fmt .Errorf ("error getting user: %v" , err )
6364 }
6465
65- token , err := apiClient . GetCodespaceToken (ctx , user . Login , codespaceName )
66+ codespace , token , err := codespaces . GetOrChooseCodespace (ctx , apiClient , user , codespaceName )
6667 if err != nil {
67- return fmt .Errorf ("error getting codespace token : %v" , err )
68+ return fmt .Errorf ("get or choose codespace : %v" , err )
6869 }
6970
70- if err := apiClient .DeleteCodespace (ctx , user , token , codespaceName ); err != nil {
71+ if err := apiClient .DeleteCodespace (ctx , user , token , codespace . Name ); err != nil {
7172 return fmt .Errorf ("error deleting codespace: %v" , err )
7273 }
7374
74- fmt .Println ("Codespace deleted." )
75+ log .Println ("Codespace deleted." )
7576
76- return List ()
77+ return List (& ListOptions {} )
7778}
7879
7980func DeleteAll () error {
8081 apiClient := api .New (os .Getenv ("GITHUB_TOKEN" ))
8182 ctx := context .Background ()
83+ log := output .NewLogger (os .Stdout , os .Stderr , false )
8284
8385 user , err := apiClient .GetUser (ctx )
8486 if err != nil {
@@ -100,15 +102,16 @@ func DeleteAll() error {
100102 return fmt .Errorf ("error deleting codespace: %v" , err )
101103 }
102104
103- fmt .Printf ("Codespace deleted: %s\n " , c .Name )
105+ log .Printf ("Codespace deleted: %s\n " , c .Name )
104106 }
105107
106- return List ()
108+ return List (& ListOptions {} )
107109}
108110
109111func DeleteByRepo (repo string ) error {
110112 apiClient := api .New (os .Getenv ("GITHUB_TOKEN" ))
111113 ctx := context .Background ()
114+ log := output .NewLogger (os .Stdout , os .Stderr , false )
112115
113116 user , err := apiClient .GetUser (ctx )
114117 if err != nil {
@@ -122,7 +125,7 @@ func DeleteByRepo(repo string) error {
122125
123126 var deleted bool
124127 for _ , c := range codespaces {
125- if c .RepositoryNWO != repo {
128+ if ! strings . EqualFold ( c .RepositoryNWO , repo ) {
126129 continue
127130 }
128131 deleted = true
@@ -136,12 +139,12 @@ func DeleteByRepo(repo string) error {
136139 return fmt .Errorf ("error deleting codespace: %v" , err )
137140 }
138141
139- fmt .Printf ("Codespace deleted: %s\n " , c .Name )
142+ log .Printf ("Codespace deleted: %s\n " , c .Name )
140143 }
141144
142145 if ! deleted {
143- fmt .Printf ("No codespace was found for repository: %s\n " , repo )
146+ return fmt .Errorf ("No codespace was found for repository: %s" , repo )
144147 }
145148
146- return List ()
149+ return List (& ListOptions {} )
147150}
0 commit comments