mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 20:38:08 +08:00
lint: upgrade golangci-lint
Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
This commit is contained in:
committed by
Ravi Chamarthy
parent
5f04092e71
commit
ac3801ea2d
+66
-43
@@ -14,10 +14,13 @@ import (
|
||||
"text/tabwriter"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
zotErrors "zotregistry.io/zot/errors"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
zerr "zotregistry.io/zot/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultConfigPerms = 0o644
|
||||
defaultFilePerms = 0o600
|
||||
)
|
||||
|
||||
func NewConfigCommand() *cobra.Command {
|
||||
@@ -25,7 +28,7 @@ func NewConfigCommand() *cobra.Command {
|
||||
|
||||
var isReset bool
|
||||
|
||||
var configCmd = &cobra.Command{
|
||||
configCmd := &cobra.Command{
|
||||
Use: "config <config-name> [variable] [value]",
|
||||
Example: examples,
|
||||
Short: "Configure zot CLI",
|
||||
@@ -51,7 +54,7 @@ func NewConfigCommand() *cobra.Command {
|
||||
return nil
|
||||
}
|
||||
|
||||
return zotErrors.ErrInvalidArgs
|
||||
return zerr.ErrInvalidArgs
|
||||
case oneArg:
|
||||
// zot config <name> -l
|
||||
if isListing {
|
||||
@@ -65,7 +68,7 @@ func NewConfigCommand() *cobra.Command {
|
||||
return nil
|
||||
}
|
||||
|
||||
return zotErrors.ErrInvalidArgs
|
||||
return zerr.ErrInvalidArgs
|
||||
case twoArgs:
|
||||
if isReset { // zot config <name> <key> --reset
|
||||
return resetConfigValue(configPath, args[0], args[1])
|
||||
@@ -77,13 +80,13 @@ func NewConfigCommand() *cobra.Command {
|
||||
}
|
||||
fmt.Fprintln(cmd.OutOrStdout(), res)
|
||||
case threeArgs:
|
||||
//zot config <name> <key> <value>
|
||||
// zot config <name> <key> <value>
|
||||
if err := setConfigValue(configPath, args[0], args[1], args[2]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
default:
|
||||
return zotErrors.ErrInvalidArgs
|
||||
return zerr.ErrInvalidArgs
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -99,7 +102,7 @@ func NewConfigCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
func NewConfigAddCommand() *cobra.Command {
|
||||
var configAddCmd = &cobra.Command{
|
||||
configAddCmd := &cobra.Command{
|
||||
Use: "add <config-name> <url>",
|
||||
Short: "Add configuration for a zot URL",
|
||||
Long: `Configure CLI for interaction with a zot server`,
|
||||
@@ -125,7 +128,7 @@ func NewConfigAddCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
func getConfigMapFromFile(filePath string) ([]interface{}, error) {
|
||||
file, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644)
|
||||
file, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, defaultConfigPerms)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -139,29 +142,29 @@ func getConfigMapFromFile(filePath string) ([]interface{}, error) {
|
||||
|
||||
var jsonMap map[string]interface{}
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
_ = json.Unmarshal(data, &jsonMap)
|
||||
|
||||
if jsonMap["configs"] == nil {
|
||||
return nil, ErrEmptyJSON
|
||||
return nil, zerr.ErrEmptyJSON
|
||||
}
|
||||
|
||||
return jsonMap["configs"].([]interface{}), nil
|
||||
}
|
||||
|
||||
func saveConfigMapToFile(filePath string, configMap []interface{}) error {
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
listMap := make(map[string]interface{})
|
||||
listMap["configs"] = configMap
|
||||
marshalled, err := json.Marshal(&listMap)
|
||||
|
||||
marshalled, err := json.Marshal(&listMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(filePath, marshalled, 0600); err != nil {
|
||||
if err := ioutil.WriteFile(filePath, marshalled, defaultFilePerms); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -171,7 +174,7 @@ func saveConfigMapToFile(filePath string, configMap []interface{}) error {
|
||||
func getConfigNames(configPath string) (string, error) {
|
||||
configs, err := getConfigMapFromFile(configPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrEmptyJSON) {
|
||||
if errors.Is(err, zerr.ErrEmptyJSON) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -180,10 +183,14 @@ func getConfigNames(configPath string) (string, error) {
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
writer := tabwriter.NewWriter(&builder, 0, 8, 1, '\t', tabwriter.AlignRight)
|
||||
writer := tabwriter.NewWriter(&builder, 0, 8, 1, '\t', tabwriter.AlignRight) //nolint:gomnd
|
||||
|
||||
for _, val := range configs {
|
||||
configMap := val.(map[string]interface{})
|
||||
configMap, ok := val.(map[string]interface{})
|
||||
if !ok {
|
||||
return "", zerr.ErrBadConfig
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "%s\t%s\n", configMap[nameKey], configMap["url"])
|
||||
}
|
||||
|
||||
@@ -197,16 +204,16 @@ func getConfigNames(configPath string) (string, error) {
|
||||
|
||||
func addConfig(configPath, configName, url string) error {
|
||||
configs, err := getConfigMapFromFile(configPath)
|
||||
if err != nil && !errors.Is(err, ErrEmptyJSON) {
|
||||
if err != nil && !errors.Is(err, zerr.ErrEmptyJSON) {
|
||||
return err
|
||||
}
|
||||
|
||||
if !isURL(url) {
|
||||
return zotErrors.ErrInvalidURL
|
||||
return zerr.ErrInvalidURL
|
||||
}
|
||||
|
||||
if configNameExists(configs, configName) {
|
||||
return zotErrors.ErrDuplicateConfigName
|
||||
return zerr.ErrDuplicateConfigName
|
||||
}
|
||||
|
||||
configMap := make(map[string]interface{})
|
||||
@@ -236,15 +243,19 @@ func addDefaultConfigs(config map[string]interface{}) {
|
||||
func getConfigValue(configPath, configName, key string) (string, error) {
|
||||
configs, err := getConfigMapFromFile(configPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrEmptyJSON) {
|
||||
return "", zotErrors.ErrConfigNotFound
|
||||
if errors.Is(err, zerr.ErrEmptyJSON) {
|
||||
return "", zerr.ErrConfigNotFound
|
||||
}
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, val := range configs {
|
||||
configMap := val.(map[string]interface{})
|
||||
configMap, ok := val.(map[string]interface{})
|
||||
if !ok {
|
||||
return "", zerr.ErrBadConfig
|
||||
}
|
||||
|
||||
addDefaultConfigs(configMap)
|
||||
|
||||
name := configMap[nameKey]
|
||||
@@ -257,25 +268,29 @@ func getConfigValue(configPath, configName, key string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return "", zotErrors.ErrConfigNotFound
|
||||
return "", zerr.ErrConfigNotFound
|
||||
}
|
||||
|
||||
func resetConfigValue(configPath, configName, key string) error {
|
||||
if key == "url" || key == nameKey {
|
||||
return zotErrors.ErrCannotResetConfigKey
|
||||
return zerr.ErrCannotResetConfigKey
|
||||
}
|
||||
|
||||
configs, err := getConfigMapFromFile(configPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrEmptyJSON) {
|
||||
return zotErrors.ErrConfigNotFound
|
||||
if errors.Is(err, zerr.ErrEmptyJSON) {
|
||||
return zerr.ErrConfigNotFound
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
for _, val := range configs {
|
||||
configMap := val.(map[string]interface{})
|
||||
configMap, ok := val.(map[string]interface{})
|
||||
if !ok {
|
||||
return zerr.ErrBadConfig
|
||||
}
|
||||
|
||||
addDefaultConfigs(configMap)
|
||||
|
||||
name := configMap[nameKey]
|
||||
@@ -291,25 +306,29 @@ func resetConfigValue(configPath, configName, key string) error {
|
||||
}
|
||||
}
|
||||
|
||||
return zotErrors.ErrConfigNotFound
|
||||
return zerr.ErrConfigNotFound
|
||||
}
|
||||
|
||||
func setConfigValue(configPath, configName, key, value string) error {
|
||||
if key == nameKey {
|
||||
return zotErrors.ErrIllegalConfigKey
|
||||
return zerr.ErrIllegalConfigKey
|
||||
}
|
||||
|
||||
configs, err := getConfigMapFromFile(configPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrEmptyJSON) {
|
||||
return zotErrors.ErrConfigNotFound
|
||||
if errors.Is(err, zerr.ErrEmptyJSON) {
|
||||
return zerr.ErrConfigNotFound
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
for _, val := range configs {
|
||||
configMap := val.(map[string]interface{})
|
||||
configMap, ok := val.(map[string]interface{})
|
||||
if !ok {
|
||||
return zerr.ErrBadConfig
|
||||
}
|
||||
|
||||
addDefaultConfigs(configMap)
|
||||
|
||||
name := configMap[nameKey]
|
||||
@@ -330,13 +349,13 @@ func setConfigValue(configPath, configName, key, value string) error {
|
||||
}
|
||||
}
|
||||
|
||||
return zotErrors.ErrConfigNotFound
|
||||
return zerr.ErrConfigNotFound
|
||||
}
|
||||
|
||||
func getAllConfig(configPath, configName string) (string, error) {
|
||||
configs, err := getConfigMapFromFile(configPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrEmptyJSON) {
|
||||
if errors.Is(err, zerr.ErrEmptyJSON) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -346,7 +365,11 @@ func getAllConfig(configPath, configName string) (string, error) {
|
||||
var builder strings.Builder
|
||||
|
||||
for _, value := range configs {
|
||||
configMap := value.(map[string]interface{})
|
||||
configMap, ok := value.(map[string]interface{})
|
||||
if !ok {
|
||||
return "", zerr.ErrBadConfig
|
||||
}
|
||||
|
||||
addDefaultConfigs(configMap)
|
||||
|
||||
name := configMap[nameKey]
|
||||
@@ -363,12 +386,16 @@ func getAllConfig(configPath, configName string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return "", zotErrors.ErrConfigNotFound
|
||||
return "", zerr.ErrConfigNotFound
|
||||
}
|
||||
|
||||
func configNameExists(configs []interface{}, configName string) bool {
|
||||
for _, val := range configs {
|
||||
configMap := val.(map[string]interface{})
|
||||
configMap, ok := val.(map[string]interface{})
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if configMap[nameKey] == configName {
|
||||
return true
|
||||
}
|
||||
@@ -399,7 +426,3 @@ Useful variables:
|
||||
showspinnerConfig = "showspinner"
|
||||
verifyTLSConfig = "verify-tls"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrEmptyJSON = errors.New("cli: config json is empty")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user