replace passing parameters by context with singletons

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-03-11 11:43:40 +01:00
parent 9301c298ef
commit 48402585fd
No known key found for this signature in database
GPG key ID: 9858809D6F8F6E7E
47 changed files with 169 additions and 205 deletions

View file

@ -63,7 +63,7 @@ func buildCommand(p *projectOptions) *cobra.Command {
}
func runBuild(ctx context.Context, opts buildOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -77,7 +77,7 @@ func convertCommand(p *projectOptions) *cobra.Command {
func runConvert(ctx context.Context, opts convertOptions, services []string) error {
var json []byte
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}
@ -88,7 +88,7 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err
}
if opts.resolve {
configFile, err := cliconfig.Load(config.Dir(ctx))
configFile, err := cliconfig.Load(config.Dir())
if err != nil {
return err
}

View file

@ -69,7 +69,7 @@ func downCommand(p *projectOptions, contextType string) *cobra.Command {
}
func runDown(ctx context.Context, opts downOptions) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -51,7 +51,7 @@ func eventsCommand(p *projectOptions) *cobra.Command {
}
func runEvents(ctx context.Context, opts eventsOpts, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -75,7 +75,7 @@ func execCommand(p *projectOptions) *cobra.Command {
}
func runExec(ctx context.Context, opts execOpts) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -49,7 +49,7 @@ func killCommand(p *projectOptions) *cobra.Command {
}
func runKill(ctx context.Context, opts killOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -69,7 +69,7 @@ func runList(ctx context.Context, opts lsOptions) error {
return err
}
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -62,7 +62,7 @@ func logsCommand(p *projectOptions, contextType string) *cobra.Command {
}
func runLogs(ctx context.Context, opts logsOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -44,7 +44,7 @@ func pauseCommand(p *projectOptions) *cobra.Command {
}
func runPause(ctx context.Context, opts pauseOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}
@ -79,7 +79,7 @@ func unpauseCommand(p *projectOptions) *cobra.Command {
}
func runUnPause(ctx context.Context, opts unpauseOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -58,7 +58,7 @@ func psCommand(p *projectOptions) *cobra.Command {
}
func runPs(ctx context.Context, opts psOptions) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -49,7 +49,7 @@ func pullCommand(p *projectOptions) *cobra.Command {
}
func runPull(ctx context.Context, opts pullOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -50,7 +50,7 @@ func pushCommand(p *projectOptions) *cobra.Command {
}
func runPush(ctx context.Context, opts pushOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -61,7 +61,7 @@ Any data which is not in a volume will be lost.`,
}
func runRemove(ctx context.Context, opts removeOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -45,7 +45,7 @@ func startCommand(p *projectOptions) *cobra.Command {
}
func runStart(ctx context.Context, opts startOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -52,7 +52,7 @@ func stopCommand(p *projectOptions) *cobra.Command {
}
func runStop(ctx context.Context, opts stopOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -49,7 +49,7 @@ func topCommand(p *projectOptions) *cobra.Command {
}
func runTop(ctx context.Context, opts topOptions, services []string) error {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return err
}

View file

@ -338,7 +338,7 @@ func setServiceScale(project *types.Project, name string, replicas int) error {
}
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
c, err := client.NewWithDefaultLocalBackend(ctx)
c, err := client.New(ctx)
if err != nil {
return nil, nil, err
}

View file

@ -17,7 +17,6 @@
package context
import (
"context"
"fmt"
"strings"
@ -103,15 +102,15 @@ func createLocalCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return createDockerContext(cmd.Context(), args[0], store.LocalContextType, opts.description, store.LocalContext{})
return createDockerContext(args[0], store.LocalContextType, opts.description, store.LocalContext{})
},
}
addDescriptionFlag(cmd, &opts.description)
return cmd
}
func createDockerContext(ctx context.Context, name string, contextType string, description string, data interface{}) error {
s := store.ContextStore(ctx)
func createDockerContext(name string, contextType string, description string, data interface{}) error {
s := store.Instance()
result := s.Create(
name,
contextType,
@ -122,8 +121,8 @@ func createDockerContext(ctx context.Context, name string, contextType string, d
return result
}
func contextExists(ctx context.Context, name string) bool {
s := store.ContextStore(ctx)
func contextExists(name string) bool {
s := store.Instance()
return s.ContextExists(name)
}

View file

@ -57,7 +57,7 @@ func createAciCommand() *cobra.Command {
}
func runCreateAci(ctx context.Context, contextName string, opts aci.ContextParams) error {
if contextExists(ctx, contextName) {
if contextExists(contextName) {
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName)
}
contextData, description, err := getAciContextData(ctx, opts)
@ -67,7 +67,7 @@ func runCreateAci(ctx context.Context, contextName string, opts aci.ContextParam
}
return err
}
return createDockerContext(ctx, contextName, store.AciContextType, description, contextData)
return createDockerContext(contextName, store.AciContextType, description, contextData)
}

View file

@ -109,7 +109,7 @@ func parseAccessKeysFile(file string, opts *ecs.ContextParams) error {
}
func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.ContextParams) error {
if contextExists(ctx, contextName) {
if contextExists(contextName) {
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
}
cs, err := client.GetCloudService(ctx, store.EcsLocalSimulationContextType)
@ -120,18 +120,18 @@ func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.
if err != nil {
return err
}
return createDockerContext(ctx, contextName, store.EcsLocalSimulationContextType, description, data)
return createDockerContext(contextName, store.EcsLocalSimulationContextType, description, data)
}
func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParams) error {
if contextExists(ctx, contextName) {
if contextExists(contextName) {
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
}
contextData, description, err := getEcsContextData(ctx, opts)
if err != nil {
return err
}
return createDockerContext(ctx, contextName, store.EcsContextType, description, contextData)
return createDockerContext(contextName, store.EcsContextType, description, contextData)
}

View file

@ -19,8 +19,6 @@
package context
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -45,7 +43,7 @@ func createKubeCommand() *cobra.Command {
Short: "Create context for a Kubernetes Cluster",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runCreateKube(cmd.Context(), args[0], opts)
return runCreateKube(args[0], opts)
},
}
@ -56,8 +54,8 @@ func createKubeCommand() *cobra.Command {
return cmd
}
func runCreateKube(ctx context.Context, contextName string, opts kube.ContextParams) error {
if contextExists(ctx, contextName) {
func runCreateKube(contextName string, opts kube.ContextParams) error {
if contextExists(contextName) {
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
}
@ -65,5 +63,5 @@ func runCreateKube(ctx context.Context, contextName string, opts kube.ContextPar
if err != nil {
return err
}
return createDockerContext(ctx, contextName, store.KubeContextType, description, contextData)
return createDockerContext(contextName, store.KubeContextType, description, contextData)
}

View file

@ -73,9 +73,8 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
return nil
}
ctx := cmd.Context()
currentContext := apicontext.CurrentContext(ctx)
s := store.ContextStore(ctx)
currentContext := apicontext.Current()
s := store.Instance()
contexts, err := s.List()
if err != nil {
return err

View file

@ -17,7 +17,6 @@
package context
import (
"context"
"errors"
"fmt"
@ -41,7 +40,7 @@ func removeCommand() *cobra.Command {
Aliases: []string{"remove"},
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runRemove(cmd.Context(), args, opts.force)
return runRemove(args, opts.force)
},
}
cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force removing current context")
@ -49,15 +48,15 @@ func removeCommand() *cobra.Command {
return cmd
}
func runRemove(ctx context.Context, args []string, force bool) error {
currentContext := apicontext.CurrentContext(ctx)
s := store.ContextStore(ctx)
func runRemove(args []string, force bool) error {
currentContext := apicontext.Current()
s := store.Instance()
var errs *multierror.Error
for _, contextName := range args {
if currentContext == contextName {
if force {
if err := runUse(ctx, "default"); err != nil {
if err := runUse("default"); err != nil {
errs = multierror.Append(errs, errors.New("cannot delete current context"))
} else {
errs = removeContext(s, contextName, errs)

View file

@ -17,7 +17,6 @@
package context
import (
"context"
"fmt"
"github.com/spf13/cobra"
@ -32,16 +31,16 @@ func showCommand() *cobra.Command {
Short: "Print the current context",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runShow(cmd.Context())
return runShow()
},
}
}
func runShow(ctx context.Context) error {
name := apicontext.CurrentContext(ctx)
func runShow() error {
name := apicontext.Current()
// Match behavior of existing CLI
if name != store.DefaultContextName {
s := store.ContextStore(ctx)
s := store.Instance()
if _, err := s.Get(name); err != nil {
return err
}

View file

@ -72,7 +72,7 @@ $ docker context update my-context --description "some description" --docker "ho
}
func runUpdate(cmd *cobra.Command, name string) error {
s := store.ContextStore(cmd.Context())
s := store.Instance()
dockerContext, err := s.Get(name)
if err == nil && dockerContext != nil {
if dockerContext.Type() != store.DefaultContextType {

View file

@ -17,7 +17,6 @@
package context
import (
"context"
"fmt"
"github.com/spf13/cobra"
@ -32,20 +31,20 @@ func useCommand() *cobra.Command {
Short: "Set the default context",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runUse(cmd.Context(), args[0])
return runUse(args[0])
},
}
}
func runUse(ctx context.Context, name string) error {
s := store.ContextStore(ctx)
func runUse(name string) error {
s := store.Instance()
// Match behavior of existing CLI
if name != store.DefaultContextName {
if _, err := s.Get(name); err != nil {
return err
}
}
if err := config.WriteCurrentContext(config.Dir(ctx), name); err != nil {
if err := config.WriteCurrentContext(config.Dir(), name); err != nil {
return err
}
fmt.Println(name)