Skip to content

Commit ad9aea6

Browse files
committed
add cache with reset subcommand
1 parent 6e22073 commit ad9aea6

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

pkg/cmd/cache/cache.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package init
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func NewCmdCache() *cobra.Command {
10+
cmd := &cobra.Command{
11+
Use: "cache",
12+
Short: "Operations for the Depot project cache",
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return fmt.Errorf("missing subcommand, please run `depot cache --help`")
15+
},
16+
}
17+
18+
cmd.AddCommand(NewCmdResetCache())
19+
20+
return cmd
21+
}

pkg/cmd/cache/reset.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

pkg/cmd/root/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/spf13/cobra"
55

66
buildCmd "github.com/depot/cli/pkg/cmd/build"
7+
cacheCmd "github.com/depot/cli/pkg/cmd/cache"
78
initCmd "github.com/depot/cli/pkg/cmd/init"
89
loginCmd "github.com/depot/cli/pkg/cmd/login"
910
versionCmd "github.com/depot/cli/pkg/cmd/version"
@@ -31,6 +32,7 @@ func NewCmdRoot(version, buildDate string) *cobra.Command {
3132

3233
// Child commands
3334
cmd.AddCommand(buildCmd.NewCmdBuild())
35+
cmd.AddCommand(cacheCmd.NewCmdCache())
3436
cmd.AddCommand(initCmd.NewCmdInit())
3537
cmd.AddCommand(loginCmd.NewCmdLogin())
3638
cmd.AddCommand(versionCmd.NewCmdVersion(version, buildDate))

0 commit comments

Comments
 (0)