/
root.go
65 lines (53 loc) · 1.92 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package main
import (
"errors"
"os"
"github.com/spf13/cobra"
)
// rootParameters defines the parameters that will be used in all of the commands.
type rootParameters struct {
registryName string
username string
password string
configs []string
}
func newRootCmd(args []string) *cobra.Command {
var rootParams rootParameters
cmd := &cobra.Command{
Use: "acr",
Short: "The Azure Container Registry CLI",
Long: `Welcome to the Azure Container Registry CLI!
To start working with the CLI, run acr --help`,
SilenceUsage: true,
}
flags := cmd.PersistentFlags()
cmd.AddCommand(
newPurgeCmd(&rootParams),
newVersionCmd(),
newLoginCmd(),
newLogoutCmd(),
newTagCmd(&rootParams),
newManifestCmd(&rootParams),
)
cmd.PersistentFlags().StringVarP(&rootParams.registryName, "registry", "r", "", "Registry name")
cmd.PersistentFlags().StringVarP(&rootParams.username, "username", "u", "", "Registry username")
cmd.PersistentFlags().StringVarP(&rootParams.password, "password", "p", "", "Registry password")
cmd.Flags().BoolP("help", "h", false, "Print usage")
cmd.Flags().StringArrayVarP(&rootParams.configs, "config", "c", nil, "Auth config paths")
// No parameter is marked as required because the registry could be inferred from a task context, same with username and password
_ = flags.Parse(args)
return cmd
}
// GetRegistryName if the registry flag was not specified it tries to get the registry value from an environment
// variable, if that fails then an error is returned.
func (rootParams *rootParameters) GetRegistryName() (string, error) {
if len(rootParams.registryName) > 0 {
return rootParams.registryName, nil
}
if registryName, ok := os.LookupEnv("ACR_DEFAULT_REGISTRY"); ok {
return registryName, nil
}
return "", errors.New("unable to determine registry name, please use --registry flag")
}