mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Pull command flags out of main() and into its own file
This commit is contained in:
45
wtf/command_flags.go
Normal file
45
wtf/command_flags.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
flags "github.com/jessevdk/go-flags"
|
||||
)
|
||||
|
||||
type CommandFlags struct {
|
||||
Config string `short:"c" long:"config" optional:"yes" description:"Path to config file"`
|
||||
Version bool `short:"v" long:"version" description:"Show Version Info"`
|
||||
}
|
||||
|
||||
func NewCommandFlags() *CommandFlags {
|
||||
cmdFlags := CommandFlags{}
|
||||
return &cmdFlags
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (cmdFlags *CommandFlags) Parse(version string) {
|
||||
parser := flags.NewParser(cmdFlags, flags.Default)
|
||||
if _, err := parser.Parse(); err != nil {
|
||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
if len(cmdFlags.Config) == 0 {
|
||||
homeDir, err := Home()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cmdFlags.Config = filepath.Join(homeDir, ".wtf", "config.yml")
|
||||
fmt.Printf(">> A: %s\n", cmdFlags.Config)
|
||||
}
|
||||
|
||||
if cmdFlags.Version {
|
||||
fmt.Printf("Version: %s\n", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,8 @@ func CreateFile(fileName string) (string, error) {
|
||||
|
||||
// LoadConfigFile loads the config.yml file to configure the app
|
||||
func LoadConfigFile(filePath string) *config.Config {
|
||||
fmt.Printf(">> B: %s\n", filePath)
|
||||
|
||||
absPath, _ := ExpandHomeDir(filePath)
|
||||
|
||||
cfg, err := config.ParseYamlFile(absPath)
|
||||
|
||||
Reference in New Issue
Block a user