1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/flags/flags.go
Chris Cummer 0e141e03c8 Closes #217. Use XDG-compatible config directory
This change is largely experimental and it's entirely possible it could
wipe out your existing configuration. Be warned.

Old config path was: ~/.wtf/
New config path is:  ~/.config/wtf/

If an existing config directory already exists, this change attempts to
copy it to the new location.

Note that if your config file contains paths to files in the old config
directory, they won't work. You'll need to change them by hand.
2018-06-20 16:46:02 -07:00

74 lines
1.6 KiB
Go

package flags
import (
"fmt"
"os"
"path/filepath"
goFlags "github.com/jessevdk/go-flags"
"github.com/senorprogrammer/wtf/help"
"github.com/senorprogrammer/wtf/wtf"
)
type Flags struct {
Config string `short:"c" long:"config" optional:"yes" description:"Path to config file"`
Module string `short:"m" long:"module" optional:"yes" description:"Display info about a specific module, i.e.: 'wtf -m=todo'"`
Version bool `short:"v" long:"version" description:"Show version info"`
}
func NewFlags() *Flags {
flags := Flags{}
return &flags
}
/* -------------------- Exported Functions -------------------- */
func (flags *Flags) ConfigFilePath() string {
return flags.Config
}
func (flags *Flags) Display(version string) {
if flags.HasModule() {
help.Display(flags.Module)
os.Exit(0)
}
if flags.HasVersion() {
fmt.Println(version)
os.Exit(0)
}
}
func (flags *Flags) HasConfig() bool {
return len(flags.Config) > 0
}
func (flags *Flags) HasModule() bool {
return len(flags.Module) > 0
}
func (flags *Flags) HasVersion() bool {
return flags.Version == true
}
func (flags *Flags) Parse() {
parser := goFlags.NewParser(flags, goFlags.Default)
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*goFlags.Error); ok && flagsErr.Type == goFlags.ErrHelp {
os.Exit(0)
}
}
// If no config file is explicitly passed in as a param,
// set the flag to the default config file
if !flags.HasConfig() {
homeDir, err := wtf.Home()
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
flags.Config = filepath.Join(homeDir, ".config", "wtf", "config.yml")
}
}