|
| 1 | +package init |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/bufbuild/connect-go" |
| 10 | + "github.com/depot/cli/pkg/api" |
| 11 | + "github.com/depot/cli/pkg/config" |
| 12 | + "github.com/depot/cli/pkg/project" |
| 13 | + cliv1beta1 "github.com/depot/cli/pkg/proto/depot/cli/v1beta1" |
| 14 | + "github.com/docker/cli/cli" |
| 15 | + "github.com/pkg/errors" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +func NewCmdResetCache() *cobra.Command { |
| 20 | + var projectID string |
| 21 | + var token string |
| 22 | + |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "reset", |
| 25 | + Short: "Reset the cache for a project", |
| 26 | + Args: cli.RequiresMaxArgs(1), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + if projectID == "" { |
| 29 | + projectID = os.Getenv("DEPOT_PROJECT_ID") |
| 30 | + } |
| 31 | + if projectID == "" { |
| 32 | + cwd, _ := filepath.Abs(args[0]) |
| 33 | + config, _, err := project.ReadConfig(cwd) |
| 34 | + if err == nil { |
| 35 | + projectID = config.ID |
| 36 | + } |
| 37 | + } |
| 38 | + if projectID == "" { |
| 39 | + return errors.Errorf("unknown project ID (run `depot init` or use --project or $DEPOT_PROJECT_ID)") |
| 40 | + } |
| 41 | + |
| 42 | + // TODO: make this a helper |
| 43 | + if token == "" { |
| 44 | + token = os.Getenv("DEPOT_TOKEN") |
| 45 | + } |
| 46 | + if token == "" { |
| 47 | + token = config.GetApiToken() |
| 48 | + } |
| 49 | + if token == "" { |
| 50 | + return fmt.Errorf("missing API token, please run `depot login`") |
| 51 | + } |
| 52 | + |
| 53 | + client := api.NewProjectsClient() |
| 54 | + req := cliv1beta1.ResetProjectCacheRequest{ProjectId: projectID} |
| 55 | + resp, err := client.ResetProjectCache(context.TODO(), api.WithAuthentication(connect.NewRequest(&req), token)) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + fmt.Printf("Cache reset for %s (%s)\n", resp.Msg.Name, resp.Msg.OrgName) |
| 61 | + |
| 62 | + return nil |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + cmd.Flags().StringVar(&projectID, "project", "", "Depot project ID for the cache to reset") |
| 67 | + cmd.Flags().StringVar(&token, "token", "", "Depot API token") |
| 68 | + |
| 69 | + return cmd |
| 70 | +} |
0 commit comments