Skip to content

Commit

Permalink
cmd/docker: add docker "app" command and install/launch/remove subcom…
Browse files Browse the repository at this point in the history
…mands

This change adds support for managing native applications on the host machine.

To maximize reuse of existing code, minor refactoring has been done:

+ build/container/cp options are exported, i.e. renamed and a few setters introduced for changing the values:

	image.BuildOptions
	container.RunOptions
	container.ContainerOptions
	container.CopyOptions

+ runBuild, runRun, runCopy are also renamed to RunBuild, RunRun, and RunCopy accordingly
+ All build flags and a small subset of relevant run flags and cp flags are supported.
+ The following new flags are added:

egress: Set container path to export
destination: Set local host path for app
launch: Start app after installation

in addition, the following existing flags are intercepted and handled by the app command:

iidfile
cidfile
detach

+ New environment variables are introduced for the benefit of app developers:

HOSTOS, HOSTARCH which are assigned the values of runtime.GOOS and runtime.GOARCH from go.

Signed-off-by: Qiang Li <[email protected]>
  • Loading branch information
qiangli committed Jul 11, 2024
1 parent cfbf88f commit 1d3047e
Show file tree
Hide file tree
Showing 24 changed files with 2,231 additions and 68 deletions.
9 changes: 9 additions & 0 deletions cli/command/app/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app

import (
"github.com/docker/docker/client"
)

type fakeClient struct {
client.Client
}
77 changes: 77 additions & 0 deletions cli/command/app/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package app

import (
"context"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/container"
"github.com/docker/cli/cli/command/image"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// cliAdapter adds convenience methods to the command.Cli interface
// for building images, running containers, and copying files from containers
type cliAdapter interface {
command.Cli
//
RunBuild(context.Context, *image.BuildOptions) error
RunRun(context.Context, *pflag.FlagSet, *container.RunOptions, *container.ContainerOptions) error
RunCopy(context.Context, *container.CopyOptions) error
}

type dockerCliAdapter struct {
command.Cli
}

func newDockerCliAdapter(c command.Cli) cliAdapter {
return &dockerCliAdapter{
c,
}
}

func (r *dockerCliAdapter) RunBuild(ctx context.Context, buildOpts *image.BuildOptions) error {
return image.RunBuild(ctx, r, buildOpts)
}

func (r *dockerCliAdapter) RunRun(ctx context.Context, flags *pflag.FlagSet, runOpts *container.RunOptions, containerOpts *container.ContainerOptions) error {
return container.RunRun(ctx, r, flags, runOpts, containerOpts)
}

func (r *dockerCliAdapter) RunCopy(ctx context.Context, copyOpts *container.CopyOptions) error {
return container.RunCopy(ctx, r, copyOpts)
}

// NewAppCommand returns a cobra command for `app` subcommands
func NewAppCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "app",
Short: "Manage application with Docker",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
}
cmd.AddCommand(
NewInstallCommand(dockerCli),
NewLaunchCommand(dockerCli),
NewRemoveCommand(dockerCli),
)
return cmd
}

func markFlagsHiddenExcept(cmd *cobra.Command, unhidden ...string) {
contains := func(n string) bool {
for _, v := range unhidden {
if v == n {
return true
}
}
return false
}
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
name := flag.Name
if !contains(name) {
flag.Hidden = true
}
})
}
Loading

0 comments on commit 1d3047e

Please sign in to comment.