func configPathFromArgs()

in astro/cli/astro/cmd/config.go [38:70]


func configPathFromArgs(args []string) (configFilePath string, err error) {
	// this is a special cobra command so that we can parse just the config
	// flag early in the program lifecycle.
	findConfig := &cobra.Command{
		SilenceUsage:  true,
		SilenceErrors: true,
		FParseErrWhitelist: cobra.FParseErrWhitelist{
			UnknownFlags: true,
		},
	}

	// Strip the help options from args so that the pre-loading of the config
	// doesn't fail with pflag.ErrHelp
	finalArgs := []string{}
	for _, arg := range args {
		if arg == "-h" || arg == "--help" || arg == "-help" {
			continue
		}
		finalArgs = append(finalArgs, arg)
	}

	// Do an early first parse of the config flag before the main command,
	findConfig.PersistentFlags().StringVar(&configFilePath, "config", "", "config file")
	if err := findConfig.ParseFlags(finalArgs); err != nil {
		return "", err
	}

	if configFilePath != "" && !utils.FileExists(configFilePath) {
		return "", fmt.Errorf("%v: file does not exist", configFilePath)
	}

	return configFilePath, nil
}