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-07 13:05:45 +00:00
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" ,
} )
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 )
}
2021-11-07 13:05:45 +00:00
if * limitName != "" && len ( * limitTags ) != 0 {
return CliArguments { } , errors . New ( errNameAndTagsTogether )
}
2021-11-02 18:19:31 +00:00
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 ) ,
2021-11-07 13:05:45 +00:00
LimitName : * limitName ,
LimitTags : * limitTags ,
2021-11-02 18:19:31 +00:00
} , nil
}