(c *Command)
| 681 | } |
| 682 | |
| 683 | func 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 | |
| 726 | func instanceFromEnv(args []string) []string { |
| 727 | // This supports naming the first instance first with: |
no test coverage detected