GitRepositoryManager/config/cmd.go

55 lines
1.4 KiB
Go
Raw Normal View History

2021-11-02 18:19:31 +00:00
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)")
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",
})
if err := parser.Parse(arguments); err != nil {
return CliArguments{}, err
}
if !syncCMD.Happened() && !(*version) {
return CliArguments{}, errors.New(errNoCommand)
}
return CliArguments{
ConfigurationFile: *configFile,
Sync: syncCMD.Happened(),
Version: *version,
Color: !(*color),
}, nil
}