feat: ghcs delete repo REPO_NAME
This commit is contained in:
parent
7a0a8fa39c
commit
5ca2fa5562
1 changed files with 54 additions and 1 deletions
|
|
@ -32,7 +32,20 @@ func NewDeleteCmd() *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
deleteCmd.AddCommand(deleteAllCmd)
|
||||
deleteByRepoCmd := &cobra.Command{
|
||||
Use: "repo REPO_NAME",
|
||||
Short: "delete all codespaces for the repo",
|
||||
Long: `delete all the codespaces that the user with the current token has in this repo.
|
||||
This includes all codespaces in all states.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("A Repository name is required.")
|
||||
}
|
||||
return DeleteByRepo(args[0])
|
||||
},
|
||||
}
|
||||
|
||||
deleteCmd.AddCommand(deleteAllCmd, deleteByRepoCmd)
|
||||
|
||||
return deleteCmd
|
||||
}
|
||||
|
|
@ -93,3 +106,43 @@ func DeleteAll() error {
|
|||
|
||||
return List()
|
||||
}
|
||||
|
||||
func DeleteByRepo(repo string) error {
|
||||
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
|
||||
ctx := context.Background()
|
||||
|
||||
user, err := apiClient.GetUser(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting user: %v", err)
|
||||
}
|
||||
|
||||
codespaces, err := apiClient.ListCodespaces(ctx, user)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting codespaces: %v", err)
|
||||
}
|
||||
|
||||
var deleted bool
|
||||
for _, c := range codespaces {
|
||||
if c.RepositoryNWO != repo {
|
||||
continue
|
||||
}
|
||||
deleted = true
|
||||
|
||||
token, err := apiClient.GetCodespaceToken(ctx, user.Login, c.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting codespace token: %v", err)
|
||||
}
|
||||
|
||||
if err := apiClient.DeleteCodespace(ctx, user, token, c.Name); err != nil {
|
||||
return fmt.Errorf("error deleting codespace: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Codespace deleted: %s\n", c.Name)
|
||||
}
|
||||
|
||||
if !deleted {
|
||||
fmt.Printf("No codespace was found for repository: %s\n", repo)
|
||||
}
|
||||
|
||||
return List()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue