-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathroot.go
More file actions
89 lines (76 loc) · 3.06 KB
/
Copy pathroot.go
File metadata and controls
89 lines (76 loc) · 3.06 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package main
import (
"errors"
"os"
"github.com/Azure/acr-cli/internal/logger"
"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
logLevel string
logFormat string
}
func newRootCmd(args []string) *cobra.Command {
// for _, arg := range args {
// fmt.Printf("arg = %s\n", arg)
// }
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,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Setup logger with configured level and format
logger.Setup(logger.Config{
Level: rootParams.logLevel,
Format: rootParams.logFormat,
})
},
}
flags := cmd.PersistentFlags()
cmd.AddCommand(
newPurgeCmd(&rootParams),
newVersionCmd(),
newLoginCmd(),
newLogoutCmd(),
newTagCmd(&rootParams),
newManifestCmd(&rootParams),
)
// If environment variable ACR_EXPERIMENTAL_CSSC is set to true, add the cssc command to the command list
if isExperimentalCssc, exists := os.LookupEnv("ACR_EXPERIMENTAL_CSSC"); exists && isExperimentalCssc == "true" {
cmd.AddCommand(newCsscCmd(&rootParams))
}
// If environment variable ACR_EXPERIMENTAL_ANNOTATE is set to true, add the annotate command to the command list
if isExperimentalAnnotate, exists := os.LookupEnv("ACR_EXPERIMENTAL_ANNOTATE"); exists && isExperimentalAnnotate == "true" {
cmd.AddCommand(newAnnotateCmd(&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.PersistentFlags().StringVar(&rootParams.logLevel, "log-level", "info", "Log level (debug, info, warn, error)")
cmd.PersistentFlags().StringVar(&rootParams.logFormat, "log-format", "console", "Log format (console, json)")
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")
}