2021-12-03 22:50:58 -05:00
|
|
|
//go:build extended
|
2020-10-14 16:47:20 -05:00
|
|
|
// +build extended
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-07-14 12:11:01 -05:00
|
|
|
"path"
|
2020-06-16 20:52:40 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/spf13/cobra"
|
2021-12-13 14:23:31 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultConfigPerms = 0o644
|
|
|
|
defaultFilePerms = 0o600
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
func NewConfigCommand() *cobra.Command {
|
2020-06-16 20:52:40 -05:00
|
|
|
var isListing bool
|
|
|
|
|
|
|
|
var isReset bool
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
configCmd := &cobra.Command{
|
2020-06-16 20:52:40 -05:00
|
|
|
Use: "config <config-name> [variable] [value]",
|
|
|
|
Example: examples,
|
|
|
|
Short: "Configure zot CLI",
|
|
|
|
Long: `Configure default parameters for CLI`,
|
|
|
|
Args: cobra.ArbitraryArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-07-14 12:11:01 -05:00
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := path.Join(home + "/.zot")
|
2020-06-16 20:52:40 -05:00
|
|
|
switch len(args) {
|
|
|
|
case noArgs:
|
|
|
|
if isListing { // zot config -l
|
|
|
|
res, err := getConfigNames(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(cmd.OutOrStdout(), res)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrInvalidArgs
|
2020-06-16 20:52:40 -05:00
|
|
|
case oneArg:
|
|
|
|
// zot config <name> -l
|
|
|
|
if isListing {
|
|
|
|
res, err := getAllConfig(configPath, args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(cmd.OutOrStdout(), res)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrInvalidArgs
|
2020-06-16 20:52:40 -05:00
|
|
|
case twoArgs:
|
|
|
|
if isReset { // zot config <name> <key> --reset
|
|
|
|
return resetConfigValue(configPath, args[0], args[1])
|
|
|
|
}
|
|
|
|
// zot config <name> <key>
|
|
|
|
res, err := getConfigValue(configPath, args[0], args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(cmd.OutOrStdout(), res)
|
|
|
|
case threeArgs:
|
2021-12-13 14:23:31 -05:00
|
|
|
// zot config <name> <key> <value>
|
2020-06-16 20:52:40 -05:00
|
|
|
if err := setConfigValue(configPath, args[0], args[1], args[2]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrInvalidArgs
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configCmd.Flags().BoolVarP(&isListing, "list", "l", false, "List configurations")
|
|
|
|
configCmd.Flags().BoolVar(&isReset, "reset", false, "Reset a variable value")
|
|
|
|
configCmd.SetUsageTemplate(configCmd.UsageTemplate() + supportedOptions)
|
2020-07-14 12:11:01 -05:00
|
|
|
configCmd.AddCommand(NewConfigAddCommand())
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
return configCmd
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:11:01 -05:00
|
|
|
func NewConfigAddCommand() *cobra.Command {
|
2021-12-13 14:23:31 -05:00
|
|
|
configAddCmd := &cobra.Command{
|
2020-06-16 20:52:40 -05:00
|
|
|
Use: "add <config-name> <url>",
|
|
|
|
Short: "Add configuration for a zot URL",
|
|
|
|
Long: `Configure CLI for interaction with a zot server`,
|
|
|
|
Args: cobra.ExactArgs(twoArgs),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-07-14 12:11:01 -05:00
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := path.Join(home + "/.zot")
|
2020-06-16 20:52:40 -05:00
|
|
|
// zot config add <config-name> <url>
|
2020-07-14 12:11:01 -05:00
|
|
|
err = addConfig(configPath, args[0], args[1])
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return configAddCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigMapFromFile(filePath string) ([]interface{}, error) {
|
2021-12-13 14:23:31 -05:00
|
|
|
file, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, defaultConfigPerms)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonMap map[string]interface{}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
_ = json.Unmarshal(data, &jsonMap)
|
|
|
|
|
|
|
|
if jsonMap["configs"] == nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, zerr.ErrEmptyJSON
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonMap["configs"].([]interface{}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveConfigMapToFile(filePath string, configMap []interface{}) error {
|
2021-12-13 14:23:31 -05:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
listMap := make(map[string]interface{})
|
|
|
|
listMap["configs"] = configMap
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
marshalled, err := json.Marshal(&listMap)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := ioutil.WriteFile(filePath, marshalled, defaultFilePerms); err != nil {
|
2020-06-16 20:52:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigNames(configPath string) (string, error) {
|
|
|
|
configs, err := getConfigMapFromFile(configPath)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrEmptyJSON) {
|
2020-06-16 20:52:40 -05:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var builder strings.Builder
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
writer := tabwriter.NewWriter(&builder, 0, 8, 1, '\t', tabwriter.AlignRight) //nolint:gomnd
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
for _, val := range configs {
|
2021-12-13 14:23:31 -05:00
|
|
|
configMap, ok := val.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", zerr.ErrBadConfig
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
fmt.Fprintf(writer, "%s\t%s\n", configMap[nameKey], configMap["url"])
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writer.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addConfig(configPath, configName, url string) error {
|
|
|
|
configs, err := getConfigMapFromFile(configPath)
|
2021-12-13 14:23:31 -05:00
|
|
|
if err != nil && !errors.Is(err, zerr.ErrEmptyJSON) {
|
2020-06-16 20:52:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isURL(url) {
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrInvalidURL
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-10-29 07:47:27 -05:00
|
|
|
if configNameExists(configs, configName) {
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrDuplicateConfigName
|
2020-10-29 07:47:27 -05:00
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
configMap := make(map[string]interface{})
|
|
|
|
configMap["url"] = url
|
|
|
|
configMap[nameKey] = configName
|
2020-07-17 14:42:22 -05:00
|
|
|
addDefaultConfigs(configMap)
|
2020-06-16 20:52:40 -05:00
|
|
|
configs = append(configs, configMap)
|
|
|
|
|
|
|
|
err = saveConfigMapToFile(configPath, configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
func addDefaultConfigs(config map[string]interface{}) {
|
|
|
|
if _, ok := config[showspinnerConfig]; !ok {
|
|
|
|
config[showspinnerConfig] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := config[verifyTLSConfig]; !ok {
|
|
|
|
config[verifyTLSConfig] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
func getConfigValue(configPath, configName, key string) (string, error) {
|
|
|
|
configs, err := getConfigMapFromFile(configPath)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrEmptyJSON) {
|
|
|
|
return "", zerr.ErrConfigNotFound
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range configs {
|
2021-12-13 14:23:31 -05:00
|
|
|
configMap, ok := val.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", zerr.ErrBadConfig
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
addDefaultConfigs(configMap)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
name := configMap[nameKey]
|
|
|
|
if name == configName {
|
|
|
|
if configMap[key] == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v", configMap[key]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return "", zerr.ErrConfigNotFound
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func resetConfigValue(configPath, configName, key string) error {
|
|
|
|
if key == "url" || key == nameKey {
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrCannotResetConfigKey
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
configs, err := getConfigMapFromFile(configPath)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrEmptyJSON) {
|
|
|
|
return zerr.ErrConfigNotFound
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range configs {
|
2021-12-13 14:23:31 -05:00
|
|
|
configMap, ok := val.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return zerr.ErrBadConfig
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
addDefaultConfigs(configMap)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
name := configMap[nameKey]
|
|
|
|
if name == configName {
|
|
|
|
delete(configMap, key)
|
|
|
|
|
|
|
|
err = saveConfigMapToFile(configPath, configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrConfigNotFound
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func setConfigValue(configPath, configName, key, value string) error {
|
|
|
|
if key == nameKey {
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrIllegalConfigKey
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
configs, err := getConfigMapFromFile(configPath)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrEmptyJSON) {
|
|
|
|
return zerr.ErrConfigNotFound
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range configs {
|
2021-12-13 14:23:31 -05:00
|
|
|
configMap, ok := val.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return zerr.ErrBadConfig
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
addDefaultConfigs(configMap)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
name := configMap[nameKey]
|
|
|
|
if name == configName {
|
|
|
|
boolVal, err := strconv.ParseBool(value)
|
|
|
|
if err == nil {
|
|
|
|
configMap[key] = boolVal
|
|
|
|
} else {
|
|
|
|
configMap[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
err = saveConfigMapToFile(configPath, configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrConfigNotFound
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAllConfig(configPath, configName string) (string, error) {
|
|
|
|
configs, err := getConfigMapFromFile(configPath)
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if errors.Is(err, zerr.ErrEmptyJSON) {
|
2020-06-16 20:52:40 -05:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var builder strings.Builder
|
|
|
|
|
|
|
|
for _, value := range configs {
|
2021-12-13 14:23:31 -05:00
|
|
|
configMap, ok := value.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", zerr.ErrBadConfig
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
addDefaultConfigs(configMap)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
name := configMap[nameKey]
|
|
|
|
if name == configName {
|
|
|
|
for key, val := range configMap {
|
|
|
|
if key == nameKey {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(&builder, "%s = %v\n", key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return "", zerr.ErrConfigNotFound
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2020-10-29 07:47:27 -05:00
|
|
|
func configNameExists(configs []interface{}, configName string) bool {
|
|
|
|
for _, val := range configs {
|
2021-12-13 14:23:31 -05:00
|
|
|
configMap, ok := val.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-29 07:47:27 -05:00
|
|
|
if configMap[nameKey] == configName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
const (
|
|
|
|
examples = ` zot config add main https://zot-foo.com:8080
|
|
|
|
zot config main url
|
|
|
|
zot config main --list
|
|
|
|
zot config --list`
|
|
|
|
|
|
|
|
supportedOptions = `
|
|
|
|
Useful variables:
|
|
|
|
url zot server URL
|
2020-07-17 14:42:22 -05:00
|
|
|
showspinner show spinner while loading data [true/false]
|
|
|
|
verify-tls verify TLS Certificate verification of the server [default: true]`
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
nameKey = "_name"
|
|
|
|
|
|
|
|
noArgs = 0
|
|
|
|
oneArg = 1
|
|
|
|
twoArgs = 2
|
|
|
|
threeArgs = 3
|
2020-07-17 14:42:22 -05:00
|
|
|
|
|
|
|
showspinnerConfig = "showspinner"
|
|
|
|
verifyTLSConfig = "verify-tls"
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|