allow builder flag to switch to default instance

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
pull/425/head
Tonis Tiigi 4 years ago
parent d2bf42f8b4
commit 4f7517115c

@ -93,7 +93,7 @@ func stop(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, rm bo
} }
func stopCurrent(ctx context.Context, dockerCli command.Cli, rm bool) error { func stopCurrent(ctx context.Context, dockerCli command.Cli, rm bool) error {
dis, err := getDefaultDrivers(ctx, dockerCli, "") dis, err := getDefaultDrivers(ctx, dockerCli, false, "")
if err != nil { if err != nil {
return err return err
} }

@ -281,10 +281,29 @@ func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClie
} }
func getInstanceOrDefault(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) { func getInstanceOrDefault(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) {
var defaultOnly bool
if instance == "default" && instance != dockerCli.CurrentContext() {
return nil, errors.Errorf("use `docker --context=default buildx` to switch to default context")
}
if instance == "default" || instance == dockerCli.CurrentContext() {
instance = ""
defaultOnly = true
}
list, err := dockerCli.ContextStore().List()
if err != nil {
return nil, err
}
for _, l := range list {
if l.Name == instance {
return nil, errors.Errorf("use `docker --context=%s buildx` to switch to context %s", instance, instance)
}
}
if instance != "" { if instance != "" {
return getInstanceByName(ctx, dockerCli, instance, contextPathHash) return getInstanceByName(ctx, dockerCli, instance, contextPathHash)
} }
return getDefaultDrivers(ctx, dockerCli, contextPathHash) return getDefaultDrivers(ctx, dockerCli, defaultOnly, contextPathHash)
} }
func getInstanceByName(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) { func getInstanceByName(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) {
@ -302,20 +321,22 @@ func getInstanceByName(ctx context.Context, dockerCli command.Cli, instance, con
} }
// getDefaultDrivers returns drivers based on current cli config // getDefaultDrivers returns drivers based on current cli config
func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, contextPathHash string) ([]build.DriverInfo, error) { func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, defaultOnly bool, contextPathHash string) ([]build.DriverInfo, error) {
txn, release, err := getStore(dockerCli) txn, release, err := getStore(dockerCli)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer release() defer release()
ng, err := getCurrentInstance(txn, dockerCli) if !defaultOnly {
if err != nil { ng, err := getCurrentInstance(txn, dockerCli)
return nil, err if err != nil {
} return nil, err
}
if ng != nil { if ng != nil {
return driversForNodeGroup(ctx, dockerCli, ng, contextPathHash) return driversForNodeGroup(ctx, dockerCli, ng, contextPathHash)
}
} }
d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil, nil, "", nil, contextPathHash) d, err := driver.GetDriver(ctx, "buildx_buildkit_default", nil, dockerCli.Client(), nil, nil, "", nil, contextPathHash)

Loading…
Cancel
Save