go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/cli/viper.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package cli
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  
    21  	"github.com/mitchellh/mapstructure"
    22  	"github.com/spf13/viper"
    23  	"go.ligato.io/cn-infra/v2/logging"
    24  )
    25  
    26  const (
    27  	configFileDir  = ".agentctl"
    28  	configFileName = "config"
    29  )
    30  
    31  func init() {
    32  	viper.SupportedExts = append(viper.SupportedExts, "conf")
    33  }
    34  
    35  // viperSetConfigFile setups viper to handle config file.
    36  func viperSetConfigFile() {
    37  	// If "config" is set then use it and skip searching for config.
    38  	if cfgFile := viper.GetString("config"); cfgFile != "" {
    39  		// Set config type explicitely
    40  		if filepath.Ext(cfgFile) == ".conf" {
    41  			viper.SetConfigType("yaml")
    42  		}
    43  		viper.SetConfigFile(cfgFile)
    44  		return
    45  	}
    46  
    47  	viper.SetConfigName(configFileName)
    48  
    49  	// If "config-dir" is set use it as first config path.
    50  	if cfgDir := viper.GetString("config-dir"); cfgDir != "" {
    51  		viper.AddConfigPath(cfgDir)
    52  	}
    53  
    54  	// Add current working directory and dir in home directory as fallback.
    55  	viper.AddConfigPath(".")
    56  	if uhd, err := os.UserHomeDir(); err == nil {
    57  		viper.AddConfigPath(filepath.Join(uhd, configFileDir))
    58  	}
    59  }
    60  
    61  // viperReadInConfig wraps viper.ReadInConfig with more logs.
    62  func viperReadInConfig() {
    63  	if err := viper.ReadInConfig(); err != nil {
    64  		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
    65  			logging.Debugf("unable to find config file: %v", err)
    66  		} else {
    67  			logging.Errorf("config file was found but another error was produced: %v", err)
    68  		}
    69  		return
    70  	}
    71  
    72  	logging.Debugf("using config file: %q", viper.ConfigFileUsed())
    73  }
    74  
    75  // viperUnmarshal wraps viper.Unmarshal with providing "json" as tag name.
    76  func viperUnmarshal(c *Config) error {
    77  	return viper.Unmarshal(
    78  		c, func(c *mapstructure.DecoderConfig) { c.TagName = "json" },
    79  	)
    80  }