Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying DOCKER_CONFIG via --config #303

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/buildx/commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/depot/cli/pkg/buildx/build"
"github.com/depot/cli/pkg/buildx/builder"
"github.com/depot/cli/pkg/compose"
"github.com/depot/cli/pkg/dockerclient"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/load"
"github.com/depot/cli/pkg/progresshelper"
Expand Down Expand Up @@ -208,14 +209,19 @@ func RunBake(dockerCli command.Cli, in BakeOptions, validator BakeValidator, pri
return nil
}

func BakeCmd(dockerCli command.Cli) *cobra.Command {
func BakeCmd() *cobra.Command {
var options BakeOptions

cmd := &cobra.Command{
Use: "bake [OPTIONS] [TARGET...]",
Aliases: []string{"f"},
Short: "Build from a file",
RunE: func(cmd *cobra.Command, args []string) error {
dockerCli, err := dockerclient.NewDockerCLI()
if err != nil {
return err
}

// TODO: remove when upgrading to buildx 0.12
for idx, file := range options.files {
if strings.HasPrefix(file, "cwd://") {
Expand Down
8 changes: 7 additions & 1 deletion pkg/buildx/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/depot/cli/pkg/ci"
"github.com/depot/cli/pkg/cmd/docker"
"github.com/depot/cli/pkg/debuglog"
"github.com/depot/cli/pkg/dockerclient"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/load"
"github.com/depot/cli/pkg/progresshelper"
Expand Down Expand Up @@ -604,7 +605,7 @@ func validateBuildOptions(in *buildOptions) (map[string]build.Options, error) {
return map[string]build.Options{defaultTargetName: opts}, nil
}

func BuildCmd(dockerCli command.Cli) *cobra.Command {
func BuildCmd() *cobra.Command {
options := newBuildOptions()

cmd := &cobra.Command{
Expand All @@ -613,6 +614,11 @@ func BuildCmd(dockerCli command.Cli) *cobra.Command {
Short: "Start a build",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dockerCli, err := dockerclient.NewDockerCLI()
if err != nil {
return err
}

options.contextPath = args[0]
cmd.Flags().VisitAll(checkWarnedFlags)

Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package init

import (
"github.com/depot/cli/pkg/buildx/commands"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)

func NewCmdBake(dockerCli command.Cli) *cobra.Command {
return commands.BakeCmd(dockerCli)
func NewCmdBake() *cobra.Command {
return commands.BakeCmd()
}
5 changes: 2 additions & 3 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package build
import (
"github.com/depot/cli/pkg/buildx/commands"
_ "github.com/depot/cli/pkg/buildxdriver"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)

func NewCmdBuild(dockerCli command.Cli) *cobra.Command {
return commands.BuildCmd(dockerCli)
func NewCmdBuild() *cobra.Command {
return commands.BuildCmd()
}
8 changes: 7 additions & 1 deletion pkg/cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/depot/cli/internal/build"
"github.com/depot/cli/pkg/buildx/imagetools"
depotdockerclient "github.com/depot/cli/pkg/dockerclient"
"github.com/depot/cli/pkg/helpers"
"github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil"
Expand All @@ -31,7 +32,7 @@ import (
"github.com/spf13/cobra"
)

func NewCmdConfigureDocker(dockerCli command.Cli) *cobra.Command {
func NewCmdConfigureDocker() *cobra.Command {
uninstall := false
var (
project string
Expand All @@ -42,6 +43,11 @@ func NewCmdConfigureDocker(dockerCli command.Cli) *cobra.Command {
Use: "configure-docker",
Short: "Configure Docker to use Depot for builds",
RunE: func(cmd *cobra.Command, args []string) error {
dockerCli, err := depotdockerclient.NewDockerCLI()
if err != nil {
return err
}

dir := config.Dir()
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Wrap(err, "could not create docker config")
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import (
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)

func NewCmdExec(dockerCli command.Cli) *cobra.Command {
func NewCmdExec() *cobra.Command {
var (
envVar string
token string
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"connectrpc.com/connect"
depotapi "github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/ci"
"github.com/depot/cli/pkg/dockerclient"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/load"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
Expand All @@ -17,7 +18,7 @@ import (
"golang.org/x/sync/errgroup"
)

func NewCmdPull(dockerCli command.Cli) *cobra.Command {
func NewCmdPull() *cobra.Command {
var (
token string
projectID string
Expand All @@ -33,6 +34,11 @@ func NewCmdPull(dockerCli command.Cli) *cobra.Command {
Short: "Pull a project's build from the Depot ephemeral registry",
Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dockerCli, err := dockerclient.NewDockerCLI()
if err != nil {
return err
}

if len(args) > 0 {
buildID = args[0]
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/pulltoken/pulltoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"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 {
func NewCmdPullToken() *cobra.Command {
var (
token string
projectID string
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/depot/cli/pkg/api"
depotapi "github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/ci"
"github.com/depot/cli/pkg/dockerclient"
"github.com/depot/cli/pkg/helpers"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
prog "github.com/docker/buildx/util/progress"
Expand All @@ -19,7 +20,7 @@ import (
)

// NewCmdPush pushes a previously saved build to a registry from the Depot ephemeral registry.
func NewCmdPush(dockerCli command.Cli) *cobra.Command {
func NewCmdPush() *cobra.Command {
var (
token string
projectID string
Expand All @@ -34,6 +35,11 @@ func NewCmdPush(dockerCli command.Cli) *cobra.Command {
Short: "Push a project's build from the Depot ephemeral registry to a destination registry",
Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dockerCli, err := dockerclient.NewDockerCLI()
if err != nil {
return err
}

if len(args) > 0 {
buildID = args[0]
}
Expand Down
31 changes: 17 additions & 14 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package root

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -22,10 +21,11 @@ import (
"github.com/depot/cli/pkg/cmd/registry"
versionCmd "github.com/depot/cli/pkg/cmd/version"
"github.com/depot/cli/pkg/config"
"github.com/depot/cli/pkg/docker"
)

func NewCmdRoot(version, buildDate string) *cobra.Command {
var dockerConfig string

var cmd = &cobra.Command{
Use: "depot <command> [flags]",
Short: "Depot CLI",
Expand All @@ -34,6 +34,12 @@ func NewCmdRoot(version, buildDate string) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Usage()
},

PersistentPreRun: func(cmd *cobra.Command, args []string) {
if dockerConfig != "" {
os.Setenv("DOCKER_CONFIG", dockerConfig)
}
},
}

// Initialize config
Expand All @@ -44,28 +50,25 @@ func NewCmdRoot(version, buildDate string) *cobra.Command {
cmd.Version = formattedVersion
cmd.Flags().Bool("version", false, "Print the version and exit")

dockerCli, err := docker.NewDockerCLI()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
cmd.PersistentFlags().StringVar(&dockerConfig, "config", "", "Override the location of Docker client config files")
_ = cmd.PersistentFlags().MarkHidden("config")

// Child commands
cmd.AddCommand(bakeCmd.NewCmdBake(dockerCli))
cmd.AddCommand(buildCmd.NewCmdBuild(dockerCli))
cmd.AddCommand(bakeCmd.NewCmdBake())
cmd.AddCommand(buildCmd.NewCmdBuild())
cmd.AddCommand(cacheCmd.NewCmdCache())
cmd.AddCommand(initCmd.NewCmdInit())
cmd.AddCommand(list.NewCmdList())
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(pull.NewCmdPull())
cmd.AddCommand(pulltoken.NewCmdPullToken())
cmd.AddCommand(push.NewCmdPush())
cmd.AddCommand(versionCmd.NewCmdVersion(version, buildDate))
cmd.AddCommand(dockerCmd.NewCmdConfigureDocker(dockerCli))
cmd.AddCommand(dockerCmd.NewCmdConfigureDocker())
cmd.AddCommand(registry.NewCmdRegistry())
cmd.AddCommand(projects.NewCmdProjects())
cmd.AddCommand(exec.NewCmdExec(dockerCli))
cmd.AddCommand(exec.NewCmdExec())

return cmd
}
23 changes: 23 additions & 0 deletions pkg/dockerclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dockerclient

import (
"github.com/depot/cli/pkg/docker"
"github.com/docker/cli/cli/command"
)

var dockerCli *command.DockerCli

func NewDockerCLI() (*command.DockerCli, error) {
if dockerCli != nil {
return dockerCli, nil
}

var err error
cli, err := docker.NewDockerCLI()
if err != nil {
return nil, err
}

dockerCli = cli
return dockerCli, nil
}
Loading