GitRepositoryManager/config/cmd.go

59 lines
1.6 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)")
2021-11-05 17:19:06 +00:00
statusCMD := parser.NewCommand("status", "Get information about repositories")
2021-11-02 18:19:31 +00:00
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",
})
2021-11-05 17:19:06 +00:00
2021-11-02 18:19:31 +00:00
if err := parser.Parse(arguments); err != nil {
2021-11-05 17:19:06 +00:00
return CliArguments{}, errors.New(parser.Usage("Please follow this help"))
2021-11-02 18:19:31 +00:00
}
2021-11-05 17:19:06 +00:00
if !syncCMD.Happened() && !(*version) && !statusCMD.Happened() {
2021-11-02 18:19:31 +00:00
return CliArguments{}, errors.New(errNoCommand)
}
return CliArguments{
ConfigurationFile: *configFile,
Sync: syncCMD.Happened(),
2021-11-05 17:19:06 +00:00
Status: statusCMD.Happened(),
2021-11-02 18:19:31 +00:00
Version: *version,
Color: !(*color),
}, nil
}