package config import ( "errors" "fmt" "os" "github.com/akamensky/argparse" ) const ( defaultConfigPath = ".config/grm/config.yaml" ) func getDefaultConfigDir() string { // Only systems like Unix, Linux, and Windows systems are supported userHomeDir, _ := os.UserHomeDir() return fmt.Sprintf("%v/%v", userHomeDir, defaultConfigPath) } func ParseCliArguments(name, description string, arguments []string) (CliArguments, error) { parser := argparse.NewParser(name, description) syncCMD := parser.NewCommand("sync", "Synchronize repositories with remote branches, if the repository does not exist, clone it. (If pulling is not possible, the repository will be fetched)") statusCMD := parser.NewCommand("status", "Get information about repositories") configFile := parser.String("c", "config-file", &argparse.Options{ Default: getDefaultConfigDir(), Help: "Path to the configuration file", }) version := parser.Flag("v", "version", &argparse.Options{ Default: false, Help: "Print version", }) color := parser.Flag("", "no-color", &argparse.Options{ Default: false, Help: "Turn off color printing", }) limitName := parser.String("n", "name", &argparse.Options{ Help: "Limit action to the specified repository name", }) limitTags := parser.StringList("t", "tag", &argparse.Options{ Help: "Limit actions to repositories that contain specific tags", }) limitRoutines := parser.Int("", "max-concurrent-process", &argparse.Options{ Default: 10, Help: "Determine how many tasks can run simultaneously", }) ignoreSkipped := parser.Flag("", "ignore-skip-flag", &argparse.Options{ Help: "Run selected command for all repositories with ignoring the skip flag", }) if err := parser.Parse(arguments); err != nil { return CliArguments{}, errors.New(parser.Usage("Please follow this help")) } if *limitName != "" && len(*limitTags) != 0 { return CliArguments{}, errors.New(errNameAndTagsTogether) } return CliArguments{ ConfigurationFile: *configFile, Sync: syncCMD.Happened(), Status: statusCMD.Happened(), Version: *version, Color: !(*color), LimitToName: *limitName, LimitToTags: *limitTags, Routines: *limitRoutines, IgnoreSkipped: *ignoreSkipped, }, nil }