mirror of
https://github.com/docker/compose.git
synced 2025-07-23 13:45:00 +02:00
Store config dir in CLI context
Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
parent
b7fb7e2e84
commit
058e6203a7
@ -31,12 +31,10 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/docker/api/cli/cmd/context/login"
|
"github.com/docker/api/cli/cmd/context/login"
|
||||||
|
|
||||||
cliopts "github.com/docker/api/cli/options"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command manages contexts
|
// Command manages contexts
|
||||||
func Command(opts *cliopts.GlobalOpts) *cobra.Command {
|
func Command() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "context",
|
Use: "context",
|
||||||
Short: "Manage contexts",
|
Short: "Manage contexts",
|
||||||
@ -47,7 +45,7 @@ func Command(opts *cliopts.GlobalOpts) *cobra.Command {
|
|||||||
listCommand(),
|
listCommand(),
|
||||||
removeCommand(),
|
removeCommand(),
|
||||||
showCommand(),
|
showCommand(),
|
||||||
useCommand(opts),
|
useCommand(),
|
||||||
login.Command(),
|
login.Command(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,22 +34,21 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
cliconfig "github.com/docker/api/cli/config"
|
cliconfig "github.com/docker/api/cli/config"
|
||||||
cliopts "github.com/docker/api/cli/options"
|
|
||||||
"github.com/docker/api/context/store"
|
"github.com/docker/api/context/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func useCommand(opts *cliopts.GlobalOpts) *cobra.Command {
|
func useCommand() *cobra.Command {
|
||||||
return &cobra.Command{
|
return &cobra.Command{
|
||||||
Use: "use CONTEXT",
|
Use: "use CONTEXT",
|
||||||
Short: "Set the default context",
|
Short: "Set the default context",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runUse(cmd.Context(), opts.Config, args[0])
|
return runUse(cmd.Context(), args[0])
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runUse(ctx context.Context, configDir string, name string) error {
|
func runUse(ctx context.Context, name string) error {
|
||||||
s := store.ContextStore(ctx)
|
s := store.ContextStore(ctx)
|
||||||
// Match behavior of existing CLI
|
// Match behavior of existing CLI
|
||||||
if name != store.DefaultContextName {
|
if name != store.DefaultContextName {
|
||||||
@ -57,7 +56,7 @@ func runUse(ctx context.Context, configDir string, name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := cliconfig.WriteCurrentContext(configDir, name); err != nil {
|
if err := cliconfig.WriteCurrentContext(cliconfig.Dir(ctx), name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(name)
|
fmt.Println(name)
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@ -38,6 +39,19 @@ import (
|
|||||||
"github.com/docker/api/context/store"
|
"github.com/docker/api/context/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type dirKey struct{}
|
||||||
|
|
||||||
|
// WithDir sets the config directory path in the context
|
||||||
|
func WithDir(ctx context.Context, path string) context.Context {
|
||||||
|
return context.WithValue(ctx, dirKey{}, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dir returns the config directory path
|
||||||
|
func Dir(ctx context.Context) string {
|
||||||
|
cd, _ := ctx.Value(dirKey{}).(string)
|
||||||
|
return cd
|
||||||
|
}
|
||||||
|
|
||||||
// LoadFile loads the docker configuration
|
// LoadFile loads the docker configuration
|
||||||
func LoadFile(dir string) (*File, error) {
|
func LoadFile(dir string) (*File, error) {
|
||||||
f := &File{}
|
f := &File{}
|
||||||
|
@ -101,7 +101,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
root.AddCommand(
|
root.AddCommand(
|
||||||
contextcmd.Command(&opts),
|
contextcmd.Command(),
|
||||||
cmd.PsCommand(),
|
cmd.PsCommand(),
|
||||||
cmd.ServeCommand(),
|
cmd.ServeCommand(),
|
||||||
run.Command(),
|
run.Command(),
|
||||||
@ -136,13 +136,15 @@ func main() {
|
|||||||
if opts.Config == "" {
|
if opts.Config == "" {
|
||||||
fatal(errors.New("config path cannot be empty"))
|
fatal(errors.New("config path cannot be empty"))
|
||||||
}
|
}
|
||||||
|
configDir := opts.Config
|
||||||
|
ctx = cliconfig.WithDir(ctx, configDir)
|
||||||
|
|
||||||
currentContext, err := determineCurrentContext(opts.Context, opts.Config)
|
currentContext, err := determineCurrentContext(opts.Context, configDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(errors.New("unable to determine current context"))
|
fatal(errors.New("unable to determine current context"))
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := store.New(store.WithRoot(opts.Config))
|
s, err := store.New(store.WithRoot(configDir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(errors.Wrap(err, "unable to create context store"))
|
fatal(errors.Wrap(err, "unable to create context store"))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user