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

Function loadConfig

cmd/root.go:626–681  ·  view source on GitHub ↗
(c *Command, args []string, opts []Option)

Source from the content-addressed store, hash-verified

624}
625
626func loadConfig(c *Command, args []string, opts []Option) error {
627 v, err := initViper(c)
628 if err != nil {
629 return err
630 }
631
632 c.Flags().VisitAll(func(f *pflag.Flag) {
633 // Override any unset flags with Viper values to use the pflags
634 // object as a single source of truth.
635 if !f.Changed {
636 if v.IsSet(f.Name) {
637 val := v.Get(f.Name)
638 _ = c.Flags().Set(f.Name, fmt.Sprintf("%v", val))
639 } else if f.Name == "sqldata-api-endpoint" && v.IsSet("sql-data-endpoint") {
640 val := v.Get("sql-data-endpoint")
641 _ = c.Flags().Set(f.Name, fmt.Sprintf("%v", val))
642 }
643 }
644 })
645
646 // If args is not already populated, try to read from the environment.
647 if len(args) == 0 {
648 args = instanceFromEnv(args)
649 }
650
651 // If no environment args are present, try to read from the config file.
652 if len(args) == 0 {
653 args = instanceFromConfigFile(v)
654 }
655
656 for _, o := range opts {
657 o(c)
658 }
659
660 // Handle logger separately from config
661 if c.conf.StructuredLogs {
662 c.logger = log.NewStructuredLogger(c.conf.Quiet)
663 }
664
665 if c.conf.Quiet {
666 c.logger = log.NewStdLogger(io.Discard, os.Stderr)
667 }
668
669 err = parseConfig(c, c.conf, args)
670 if err != nil {
671 return err
672 }
673
674 // The arguments are parsed. Usage is no longer needed.
675 c.SilenceUsage = true
676
677 // Errors will be handled by logging from here on.
678 c.SilenceErrors = true
679
680 return nil
681}
682
683func initViper(c *Command) (*viper.Viper, error) {

Callers 1

NewCommandFunction · 0.85

Calls 6

NewStructuredLoggerFunction · 0.92
NewStdLoggerFunction · 0.92
initViperFunction · 0.85
instanceFromEnvFunction · 0.85
instanceFromConfigFileFunction · 0.85
parseConfigFunction · 0.85

Tested by

no test coverage detected