mirror of
https://github.com/docker/compose.git
synced 2025-07-27 15:44:08 +02:00
showcase simpler command design by using API options as cobra flags target
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
82f35d1bac
commit
924d7925d6
@ -87,6 +87,20 @@ type projectOptions struct {
|
|||||||
EnvFile string
|
EnvFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProjectFunc does stuff within a types.Project
|
||||||
|
type ProjectFunc func(ctx context.Context, project *types.Project) error
|
||||||
|
|
||||||
|
// WithServices creates a cobra run command from a ProjectFunc based on configured project options and selected services
|
||||||
|
func (o *projectOptions) WithServices(services []string, fn ProjectFunc) func(cmd *cobra.Command, args []string) error {
|
||||||
|
return Adapt(func(ctx context.Context, strings []string) error {
|
||||||
|
project, err := o.toProject(services)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fn(ctx, project)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
|
func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
|
||||||
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
|
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
|
||||||
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
|
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
|
||||||
|
@ -18,26 +18,21 @@ package compose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/compose-spec/compose-go/types"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
)
|
)
|
||||||
|
|
||||||
type killOptions struct {
|
|
||||||
*projectOptions
|
|
||||||
Signal string
|
|
||||||
}
|
|
||||||
|
|
||||||
func killCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
func killCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
||||||
opts := killOptions{
|
var opts compose.KillOptions
|
||||||
projectOptions: p,
|
|
||||||
}
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "kill [options] [SERVICE...]",
|
Use: "kill [options] [SERVICE...]",
|
||||||
Short: "Force stop service containers.",
|
Short: "Force stop service containers.",
|
||||||
RunE: Adapt(func(ctx context.Context, args []string) error {
|
RunE: p.WithServices(os.Args, func(ctx context.Context, project *types.Project) error {
|
||||||
return runKill(ctx, backend, opts, args)
|
return backend.Kill(ctx, project, opts)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,13 +41,3 @@ func killCommand(p *projectOptions, backend compose.Service) *cobra.Command {
|
|||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func runKill(ctx context.Context, backend compose.Service, opts killOptions, services []string) error {
|
|
||||||
project, err := opts.toProject(services)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return backend.Kill(ctx, project, compose.KillOptions{
|
|
||||||
Signal: opts.Signal,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user