|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + multierror "github.com/hashicorp/go-multierror" |
| 8 | + "github.com/pkg/errors" |
| 9 | +) |
| 10 | + |
| 11 | +func init() { |
| 12 | + flagSet := flag.NewFlagSet("delete", flag.ExitOnError) |
| 13 | + apiFlags := newAPIFlags(flagSet) |
| 14 | + |
| 15 | + printUsage := func() { |
| 16 | + fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src repos %s'\n", flagSet.Name()) |
| 17 | + |
| 18 | + flagSet.PrintDefaults() |
| 19 | + |
| 20 | + examples := ` |
| 21 | +Examples: |
| 22 | +
|
| 23 | + Delete one or more repositories: |
| 24 | +
|
| 25 | + $ src repos delete github.com/my/repo github.com/my/repo2 |
| 26 | +` |
| 27 | + fmt.Fprint(flag.CommandLine.Output(), examples) |
| 28 | + } |
| 29 | + |
| 30 | + deleteRepository := func(repoName string) error { |
| 31 | + repoID, err := fetchRepositoryID(repoName) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + query := `mutation DeleteRepository($repoID: ID!){ |
| 37 | + deleteRepository(repository: $repoID) { |
| 38 | + alwaysNil |
| 39 | + } |
| 40 | + }` |
| 41 | + var result struct{} |
| 42 | + return (&apiRequest{ |
| 43 | + query: query, |
| 44 | + vars: map[string]interface{}{ |
| 45 | + "repoID": repoID, |
| 46 | + }, |
| 47 | + result: &result, |
| 48 | + done: func() error { |
| 49 | + fmt.Fprintf(flag.CommandLine.Output(), "Repository %q deleted\n", repoName) |
| 50 | + return nil |
| 51 | + }, |
| 52 | + flags: apiFlags, |
| 53 | + }).do() |
| 54 | + } |
| 55 | + |
| 56 | + deleteRepositories := func(args []string) error { |
| 57 | + flagSet.Parse(args) |
| 58 | + var errs *multierror.Error |
| 59 | + for _, repoName := range flagSet.Args() { |
| 60 | + err := deleteRepository(repoName) |
| 61 | + if err != nil { |
| 62 | + err = errors.Wrapf(err, "Failed to delete repository %q", repoName) |
| 63 | + errs = multierror.Append(errs, err) |
| 64 | + } |
| 65 | + } |
| 66 | + return errs.ErrorOrNil() |
| 67 | + } |
| 68 | + |
| 69 | + // Register the command. |
| 70 | + reposCommands = append(reposCommands, &command{ |
| 71 | + flagSet: flagSet, |
| 72 | + handler: deleteRepositories, |
| 73 | + usageFunc: printUsage, |
| 74 | + }) |
| 75 | +} |
0 commit comments