Skip to content

Commit 92d3ffc

Browse files
Merge pull request #53 from depot/kyle/cache-command
Add a reset cache command
2 parents 405df4f + 7c550a4 commit 92d3ffc

File tree

7 files changed

+325
-30
lines changed

7 files changed

+325
-30
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Official CLI for [Depot](https://depot.dev) - you can use the CLI to build Docke
1414
- [Quick Start](#quick-start)
1515
- [Usage](#usage)
1616
- [`depot build`](#depot-build)
17+
- [`depot cache`](#depot-cache)
18+
- [`depot cache reset`](#depot-cache-reset)
1719
- [`depot init`](#depot-init)
1820
- [`depot login`](#depot-login)
1921
- [Contributing](#contributing)
@@ -69,6 +71,28 @@ depot build -t repo/image:tag . --load
6971
depot build -t repo/image:tag . --push
7072
```
7173

74+
### `depot cache`
75+
76+
Interact with the cache associated with a Depot project. The `cache` command consists of subcommands for each operation.
77+
78+
#### `depot cache reset`
79+
80+
Reset the cache of the Depot project to force a new empty cache volume to be created.
81+
82+
**Example**
83+
84+
Reset the cache of the current project ID in the root `depot.json`
85+
86+
```
87+
depot cache reset .
88+
```
89+
90+
Reset the cache of a specific project ID
91+
92+
```
93+
depot cache reset --project 12345678910
94+
```
95+
7296
### `depot init`
7397

7498
Initialize an existing Depot project in the current directory. The CLI will display an interactive list of your Depot projects for you to choose from, then write a `depot.json` file in the current directory with the contents `{"projectID": "xxxxxxxxxx"}`.

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))

pkg/proto/depot/cli/v1beta1/cliv1beta1connect/projects.connect.go

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)