11package delete
22
33import (
4+ "errors"
5+ "fmt"
46 "net/http"
7+ "strings"
58
9+ "github.com/cli/cli/v2/api"
10+ "github.com/cli/cli/v2/internal/ghinstance"
11+ "github.com/cli/cli/v2/internal/ghrepo"
612 "github.com/cli/cli/v2/pkg/cmdutil"
13+ "github.com/cli/cli/v2/pkg/prompt"
714
15+ "github.com/AlecAivazis/survey/v2"
816 "github.com/cli/cli/v2/pkg/iostreams"
917 "github.com/spf13/cobra"
1018)
@@ -27,7 +35,8 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
2735 Short : "Delete a repository" ,
2836 Long : `Delete a GitHub repository.
2937
30- Ensure that you have authorized the \"delete_repo\" scope: gh auth refresh -h github.com -s delete_repo"` ,
38+ Deletion requires authorization with the "delete_repo" scope.
39+ To authorize, run "gh auth refresh -h github.com -s delete_repo"` ,
3140 Args : cmdutil .ExactArgs (1 , "cannot delete: repository argument required" ),
3241 RunE : func (cmd * cobra.Command , args []string ) error {
3342 opts .RepoArg = args [0 ]
@@ -38,11 +47,68 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
3847 },
3948 }
4049
41- cmd .Flags ().BoolVar (& opts .Confirmed , "yes" , false , "Confirm deletion without prompting" )
50+ cmd .Flags ().BoolVar (& opts .Confirmed , "yes" , false , "confirm deletion without prompting" )
4251 return cmd
4352}
4453
4554func deleteRun (opts * DeleteOptions ) error {
55+ httpClient , err := opts .HttpClient ()
56+ if err != nil {
57+ return err
58+ }
59+ apiClient := api .NewClientFromHTTP (httpClient )
60+
61+ deleteURL := opts .RepoArg
62+ var toDelete ghrepo.Interface
63+
64+ if ! strings .Contains (deleteURL , "/" ) {
65+ currentUser , err := api .CurrentLoginName (apiClient , ghinstance .Default ())
66+ if err != nil {
67+ return err
68+ }
69+ deleteURL = currentUser + "/" + deleteURL
70+ }
71+ toDelete , err = ghrepo .FromFullName (deleteURL )
72+ if err != nil {
73+ return fmt .Errorf ("argument error: %w" , err )
74+ }
75+
76+ fullName := ghrepo .FullName (toDelete )
77+
78+ doPrompt := opts .IO .CanPrompt ()
79+ if ! opts .Confirmed && ! doPrompt {
80+ return errors .New ("could not prompt: confirmation with prompt or --yes flag required" )
81+ }
82+
83+ if ! opts .Confirmed && doPrompt {
84+ var valid string
85+ err := prompt .SurveyAskOne (
86+ & survey.Input {Message : fmt .Sprintf ("Type %s to confirm deletion:" , fullName )},
87+ & valid ,
88+ survey .WithValidator (
89+ func (val interface {}) error {
90+ if str := val .(string ); str != fullName {
91+ return fmt .Errorf ("You entered %s" , str )
92+ }
93+ return nil
94+ }))
95+ if err != nil {
96+ return fmt .Errorf ("could not prompt: %w" , err )
97+ }
98+ }
99+
100+ err = deleteRepo (httpClient , toDelete )
101+ if err != nil {
102+ return fmt .Errorf ("API call failed: %w" , err )
103+ }
104+
105+ if opts .IO .IsStdoutTTY () {
106+ cs := opts .IO .ColorScheme ()
107+ fmt .Fprintf (opts .IO .Out ,
108+ "%s Deleted repository %s\n " ,
109+ cs .SuccessIcon (),
110+ fullName )
111+ }
46112
47113 return nil
48114}
0 commit comments