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: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
func processAliases(dockerCli command.Cli, cmd *cobra.Command, args, osArgs []string) ([]string, []string, error) {
|
|
|
|
var err error
|
|
|
|
aliasMap := dockerCli.ConfigFile().Aliases
|
|
|
|
aliases := make([][2][]string, 0, len(aliasMap))
|
|
|
|
|
|
|
|
for k, v := range aliasMap {
|
|
|
|
if _, ok := allowedAliases[k]; !ok {
|
|
|
|
return args, osArgs, errors.Errorf("not allowed to alias %q (allowed: %#v)", k, allowedAliases)
|
|
|
|
}
|
2022-09-29 16:35:36 -04:00
|
|
|
if c, _, err := cmd.Find(strings.Split(v, " ")); err == nil {
|
|
|
|
if c.Annotations[pluginmanager.CommandAnnotationPlugin] != "true" {
|
|
|
|
return args, osArgs, errors.Errorf("not allowed to alias with builtin %q as target", v)
|
|
|
|
}
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
aliases = append(aliases, [2][]string{{k}, {v}})
|
|
|
|
}
|
|
|
|
|
|
|
|
args, osArgs, err = processBuilder(dockerCli, cmd, args, os.Args)
|
|
|
|
if err != nil {
|
|
|
|
return args, os.Args, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return args, osArgs, nil
|
|
|
|
}
|