MCPcopy Index your code
hub / github.com/GoogleCloudPlatform/cloud-sql-proxy / initViper

Function initViper

cmd/root.go:683–724  ·  view source on GitHub ↗
(c *Command)

Source from the content-addressed store, hash-verified

681}
682
683func initViper(c *Command) (*viper.Viper, error) {
684 v := viper.New()
685
686 if c.conf.Filepath != "" {
687 ext := filepath.Ext(c.conf.Filepath)
688
689 badExtErr := newBadCommandError(
690 fmt.Sprintf("config file %v should have extension of "+
691 "toml, yaml, or json", c.conf.Filepath,
692 ))
693
694 if ext == "" {
695 return nil, badExtErr
696 }
697
698 if ext != ".toml" && ext != ".yaml" && ext != ".yml" && ext != ".json" {
699 return nil, badExtErr
700 }
701
702 v.SetConfigFile(c.conf.Filepath)
703
704 // Attempt to load configuration from a file. If no file is found,
705 // assume configuration is provided by flags or environment variables.
706 if err := v.ReadInConfig(); err != nil {
707 // If the error is a ConfigFileNotFoundError, then ignore it.
708 // Otherwise, report the error to the user.
709 var cErr viper.ConfigFileNotFoundError
710 if !errors.As(err, &cErr) {
711 return nil, newBadCommandError(fmt.Sprintf(
712 "failed to load configuration from %v: %v",
713 c.conf.Filepath, err,
714 ))
715 }
716 }
717 }
718
719 v.SetEnvPrefix(envPrefix)
720 v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
721 v.AutomaticEnv()
722
723 return v, nil
724}
725
726func instanceFromEnv(args []string) []string {
727 // This supports naming the first instance first with:

Callers 1

loadConfigFunction · 0.85

Calls 1

newBadCommandErrorFunction · 0.85

Tested by

no test coverage detected