2022-02-03 04:37:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-09-29 16:35:36 -04:00
|
|
|
pluginmanager "github.com/docker/cli/cli-plugins/manager"
|
2022-02-03 04:37:55 -05:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyBuilderAlias = "builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
var allowedAliases = map[string]struct{}{
|
|
|
|
keyBuilderAlias: {},
|
|
|
|
}
|
|
|
|
|
2022-11-03 17:30:27 -04:00
|
|
|
func processAliases(dockerCli command.Cli, cmd *cobra.Command, args, osArgs []string) ([]string, []string, []string, error) {
|
2022-02-03 04:37:55 -05:00
|
|
|
var err error
|
2022-11-03 17:30:27 -04:00
|
|
|
var envs []string
|
2022-02-03 04:37:55 -05:00
|
|
|
aliasMap := dockerCli.ConfigFile().Aliases
|
|
|
|
aliases := make([][2][]string, 0, len(aliasMap))
|
|
|
|
|
|
|
|
for k, v := range aliasMap {
|
|
|
|
if _, ok := allowedAliases[k]; !ok {
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osArgs, envs, errors.Errorf("not allowed to alias %q (allowed: %#v)", k, allowedAliases)
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
2022-09-29 16:35:36 -04:00
|
|
|
if c, _, err := cmd.Find(strings.Split(v, " ")); err == nil {
|
2022-09-29 16:43:47 -04:00
|
|
|
if !pluginmanager.IsPluginCommand(c) {
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osArgs, envs, errors.Errorf("not allowed to alias with builtin %q as target", v)
|
2022-09-29 16:35:36 -04:00
|
|
|
}
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
aliases = append(aliases, [2][]string{{k}, {v}})
|
|
|
|
}
|
|
|
|
|
2022-11-03 17:30:27 -04:00
|
|
|
args, osArgs, envs, err = processBuilder(dockerCli, cmd, args, os.Args)
|
2022-02-03 04:37:55 -05:00
|
|
|
if err != nil {
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, os.Args, envs, err
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, al := range aliases {
|
|
|
|
var didChange bool
|
|
|
|
args, didChange = command.StringSliceReplaceAt(args, al[0], al[1], 0)
|
|
|
|
if didChange {
|
|
|
|
osArgs, _ = command.StringSliceReplaceAt(osArgs, al[0], al[1], -1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osArgs, envs, nil
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|