github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/repo/archive/archive.go (about) 1 package archive 2 3 import ( 4 "fmt" 5 "net/http" 6 "strings" 7 8 "github.com/AlecAivazis/survey/v2" 9 "github.com/MakeNowJust/heredoc" 10 "github.com/ungtb10d/cli/v2/api" 11 "github.com/ungtb10d/cli/v2/internal/config" 12 "github.com/ungtb10d/cli/v2/internal/ghrepo" 13 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 14 "github.com/ungtb10d/cli/v2/pkg/prompt" 15 16 "github.com/ungtb10d/cli/v2/pkg/iostreams" 17 "github.com/spf13/cobra" 18 ) 19 20 type ArchiveOptions struct { 21 HttpClient func() (*http.Client, error) 22 Config func() (config.Config, error) 23 BaseRepo func() (ghrepo.Interface, error) 24 Confirmed bool 25 IO *iostreams.IOStreams 26 RepoArg string 27 } 28 29 func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra.Command { 30 opts := &ArchiveOptions{ 31 IO: f.IOStreams, 32 HttpClient: f.HttpClient, 33 Config: f.Config, 34 BaseRepo: f.BaseRepo, 35 } 36 37 cmd := &cobra.Command{ 38 Use: "archive [<repository>]", 39 Short: "Archive a repository", 40 Long: heredoc.Doc(`Archive a GitHub repository. 41 42 With no argument, archives the current repository.`), 43 Args: cobra.MaximumNArgs(1), 44 RunE: func(cmd *cobra.Command, args []string) error { 45 if len(args) > 0 { 46 opts.RepoArg = args[0] 47 } 48 49 if !opts.Confirmed && !opts.IO.CanPrompt() { 50 return cmdutil.FlagErrorf("--confirm required when not running interactively") 51 } 52 if runF != nil { 53 return runF(opts) 54 } 55 return archiveRun(opts) 56 }, 57 } 58 59 cmd.Flags().BoolVarP(&opts.Confirmed, "confirm", "y", false, "Skip the confirmation prompt") 60 return cmd 61 } 62 63 func archiveRun(opts *ArchiveOptions) error { 64 cs := opts.IO.ColorScheme() 65 httpClient, err := opts.HttpClient() 66 if err != nil { 67 return err 68 } 69 apiClient := api.NewClientFromHTTP(httpClient) 70 71 var toArchive ghrepo.Interface 72 73 if opts.RepoArg == "" { 74 toArchive, err = opts.BaseRepo() 75 if err != nil { 76 return err 77 } 78 } else { 79 repoSelector := opts.RepoArg 80 if !strings.Contains(repoSelector, "/") { 81 cfg, err := opts.Config() 82 if err != nil { 83 return err 84 } 85 86 hostname, _ := cfg.DefaultHost() 87 88 currentUser, err := api.CurrentLoginName(apiClient, hostname) 89 if err != nil { 90 return err 91 } 92 repoSelector = currentUser + "/" + repoSelector 93 } 94 95 toArchive, err = ghrepo.FromFullName(repoSelector) 96 if err != nil { 97 return err 98 } 99 } 100 101 fields := []string{"name", "owner", "isArchived", "id"} 102 repo, err := api.FetchRepository(apiClient, toArchive, fields) 103 if err != nil { 104 return err 105 } 106 107 fullName := ghrepo.FullName(toArchive) 108 if repo.IsArchived { 109 fmt.Fprintf(opts.IO.ErrOut, "%s Repository %s is already archived\n", cs.WarningIcon(), fullName) 110 return nil 111 } 112 113 if !opts.Confirmed { 114 p := &survey.Confirm{ 115 Message: fmt.Sprintf("Archive %s?", fullName), 116 Default: false, 117 } 118 //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter 119 err = prompt.SurveyAskOne(p, &opts.Confirmed) 120 if err != nil { 121 return fmt.Errorf("failed to prompt: %w", err) 122 } 123 if !opts.Confirmed { 124 return cmdutil.CancelError 125 } 126 } 127 128 err = archiveRepo(httpClient, repo) 129 if err != nil { 130 return err 131 } 132 133 if opts.IO.IsStdoutTTY() { 134 fmt.Fprintf(opts.IO.Out, 135 "%s Archived repository %s\n", 136 cs.SuccessIcon(), 137 fullName) 138 } 139 140 return nil 141 }