Skip to content

Commit

Permalink
Merge pull request #259 from depot/pull-token-create
Browse files Browse the repository at this point in the history
Add `pull-token` command to generate a pull token for the ephemeral registry
  • Loading branch information
kylegalbraith authored Mar 15, 2024
2 parents 9d3911c + 8953919 commit f54437a
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 160 deletions.
59 changes: 59 additions & 0 deletions pkg/cmd/pulltoken/pulltoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pulltoken

import (
"fmt"

"connectrpc.com/connect"
depotapi "github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/helpers"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)

func NewCmdPullToken(dockerCli command.Cli) *cobra.Command {
var (
token string
projectID string
buildID string
)

cmd := &cobra.Command{
Use: "pull-token [flags] ([buildID])",
Short: "Create a new pull token for the ephemeral registry",
Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
buildID = args[0]
}

ctx := cmd.Context()

token, err := helpers.ResolveToken(ctx, token)
if err != nil {
return err
}
projectID = helpers.ResolveProjectID(projectID)

if token == "" {
return fmt.Errorf("missing API token, please run `depot login`")
}

client := depotapi.NewBuildClient()
req := &cliv1.GetPullTokenRequest{BuildId: &buildID, ProjectId: &projectID}
res, err := client.GetPullToken(ctx, depotapi.WithAuthentication(connect.NewRequest(req), token))
if err != nil {
return err
}

fmt.Println(res.Msg.Token)
return nil
},
}

cmd.Flags().StringVar(&projectID, "project", "", "Depot project ID")
cmd.Flags().StringVar(&token, "token", "", "Depot token")

return cmd
}
2 changes: 2 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
logout "github.com/depot/cli/pkg/cmd/logout"
"github.com/depot/cli/pkg/cmd/projects"
"github.com/depot/cli/pkg/cmd/pull"
"github.com/depot/cli/pkg/cmd/pulltoken"
"github.com/depot/cli/pkg/cmd/push"
"github.com/depot/cli/pkg/cmd/registry"
versionCmd "github.com/depot/cli/pkg/cmd/version"
Expand Down Expand Up @@ -57,6 +58,7 @@ func NewCmdRoot(version, buildDate string) *cobra.Command {
cmd.AddCommand(loginCmd.NewCmdLogin())
cmd.AddCommand(logout.NewCmdLogout())
cmd.AddCommand(pull.NewCmdPull(dockerCli))
cmd.AddCommand(pulltoken.NewCmdPullToken(dockerCli))
cmd.AddCommand(push.NewCmdPush(dockerCli))
cmd.AddCommand(versionCmd.NewCmdVersion(version, buildDate))
cmd.AddCommand(dockerCmd.NewCmdConfigureDocker(dockerCli))
Expand Down
4 changes: 4 additions & 0 deletions pkg/progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ func (m *mockBuildServiceClient) ReportBuildContext(context.Context, *connect.Re
func (m *mockBuildServiceClient) GetPullInfo(context.Context, *connect.Request[cliv1.GetPullInfoRequest]) (*connect.Response[cliv1.GetPullInfoResponse], error) {
return nil, nil
}

func (m *mockBuildServiceClient) GetPullToken(context.Context, *connect.Request[cliv1.GetPullTokenRequest]) (*connect.Response[cliv1.GetPullTokenResponse], error) {
return nil, nil
}
Loading

0 comments on commit f54437a

Please sign in to comment.