Add compose up and down

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-05-05 10:58:24 +02:00
parent f32235b8ba
commit 03e418cbbb
12 changed files with 135 additions and 21 deletions

View file

@ -0,0 +1,58 @@
package compose
import (
"github.com/spf13/cobra"
"github.com/docker/api/client"
"github.com/docker/api/compose"
)
func Command() *cobra.Command {
command := &cobra.Command{
Short: "Docker Compose",
Use: "compose",
}
command.AddCommand(
upCommand(),
downCommand(),
)
return command
}
func upCommand() *cobra.Command {
opts := &compose.ProjectOptions{}
upCmd := &cobra.Command{
Use: "up",
RunE: func(cmd *cobra.Command, args []string) error {
c, err := client.New(cmd.Context())
if err != nil {
return err
}
return c.AciService().Up(cmd.Context(), *opts)
},
}
upCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
upCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
return upCmd
}
func downCommand() *cobra.Command {
opts := &compose.ProjectOptions{}
downCmd := &cobra.Command{
Use: "down",
RunE: func(cmd *cobra.Command, args []string) error {
c, err := client.New(cmd.Context())
if err != nil {
return err
}
return c.AciService().Down(cmd.Context(), *opts)
},
}
downCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
downCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
return downCmd
}

View file

@ -62,5 +62,5 @@ func runExec(ctx context.Context, opts execOpts, name string, command string) er
stdout = con
}
return c.ContainerService().Exec(ctx, name, command, os.Stdin, stdout)
return c.AciService().Exec(ctx, name, command, os.Stdin, stdout)
}

View file

@ -46,5 +46,5 @@ func runLogs(ctx context.Context, containerName string, opts logsOpts) error {
Writer: os.Stdout,
}
return c.ContainerService().Logs(ctx, containerName, req)
return c.AciService().Logs(ctx, containerName, req)
}

View file

@ -23,7 +23,7 @@ var PsCommand = cobra.Command{
return errors.Wrap(err, "cannot connect to backend")
}
containers, err := c.ContainerService().List(ctx)
containers, err := c.AciService().List(ctx)
if err != nil {
return errors.Wrap(err, "fetch containers")
}

View file

@ -65,5 +65,5 @@ func runRun(ctx context.Context, image string, opts runOpts) error {
return err
}
return c.ContainerService().Run(ctx, project)
return c.AciService().Run(ctx, project)
}

View file

@ -38,6 +38,7 @@ import (
// Backend registrations
_ "github.com/docker/api/azure"
"github.com/docker/api/cli/cmd/compose"
_ "github.com/docker/api/example"
"github.com/sirupsen/logrus"
@ -102,6 +103,7 @@ func main() {
run.Command(),
cmd.ExecCommand(),
cmd.LogsCommand(),
compose.Command(),
)
helpFunc := root.HelpFunc()