Basically Functional
This commit is contained in:
+75
-21
@@ -6,11 +6,20 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.bullercodeworks.com/brian/netcaptain/internal/cli"
|
||||
"git.bullercodeworks.com/brian/netcaptain/internal/data"
|
||||
)
|
||||
|
||||
const (
|
||||
MODE_RUN = iota
|
||||
MODE_VALIDATE_QUEST
|
||||
MODE_VALIDATE_SHIP
|
||||
MODE_EDIT_QUEST
|
||||
MODE_EDIT_SHIP
|
||||
MODE_EDIT_HOLD
|
||||
MODE_LIST_QUESTS
|
||||
MODE_LIST_SHIPS
|
||||
)
|
||||
|
||||
type AppState struct {
|
||||
@@ -18,14 +27,15 @@ type AppState struct {
|
||||
Version int
|
||||
ConfDir string
|
||||
Parms []string
|
||||
Ships map[string]Ship
|
||||
Quests map[string]Quest
|
||||
Verbose bool
|
||||
|
||||
Mode int
|
||||
|
||||
// CLI Variables
|
||||
Environment string
|
||||
Request string
|
||||
QuestsDir string
|
||||
Quest *data.Quest
|
||||
ShipsDir string
|
||||
Ship *data.Ship
|
||||
HoldsDir string
|
||||
|
||||
Error error
|
||||
}
|
||||
@@ -38,47 +48,91 @@ func NewApp() *AppState {
|
||||
|
||||
func (a *AppState) initialize() error {
|
||||
flag.StringVar(&a.ConfDir, "d", ".ncpt", "The configuration directory to use")
|
||||
editMode := flag.Bool("e", false, "Edit the named Quest")
|
||||
flag.BoolVar(&a.Verbose, "v", false, "Run in Verbose mode")
|
||||
validateQuest := flag.String("c", "", "Validate the given quest is _c_orrect")
|
||||
validateShip := flag.String("C", "", "Validate the given ship is _C_orrect")
|
||||
editQuest := flag.String("e", "", "Edit the given quest")
|
||||
editShip := flag.String("E", "", "Edit the given ship")
|
||||
listQuestsMode := flag.Bool("l", false, "List all available Quests")
|
||||
listShipsMode := flag.Bool("L", false, "List all available Ships")
|
||||
editHold := flag.String("H", "", "Edit the hold for the given ship")
|
||||
|
||||
flag.Parse()
|
||||
a.Parms = flag.Args()
|
||||
|
||||
// Figure out what 'mode' we're running in
|
||||
if *editMode {
|
||||
a.Mode = MODE_EDIT_QUEST
|
||||
} else {
|
||||
a.Mode = MODE_RUN
|
||||
}
|
||||
|
||||
a.Ships = make(map[string]Ship)
|
||||
a.Quests = make(map[string]Quest)
|
||||
if err := a.validateConfDir(); err != nil {
|
||||
return err
|
||||
}
|
||||
a.QuestsDir = a.ConfDir + "/quests"
|
||||
a.ShipsDir = a.ConfDir + "/ships"
|
||||
a.HoldsDir = a.ConfDir + "/holds"
|
||||
|
||||
// Figure out what 'mode' we're running in
|
||||
if *validateQuest != "" {
|
||||
a.Mode = MODE_VALIDATE_QUEST
|
||||
a.Quest = data.NewQuest(a.ConfDir, *validateQuest)
|
||||
} else if *validateShip != "" {
|
||||
a.Mode = MODE_VALIDATE_SHIP
|
||||
a.Ship = data.NewShip(a.ConfDir, *validateShip)
|
||||
} else if *editQuest != "" {
|
||||
a.Mode = MODE_EDIT_QUEST
|
||||
a.Quest = data.NewQuest(a.ConfDir, *editQuest)
|
||||
} else if *editShip != "" {
|
||||
a.Mode = MODE_EDIT_SHIP
|
||||
a.Ship = data.NewShip(a.ConfDir, *editShip)
|
||||
} else if *listQuestsMode {
|
||||
a.Mode = MODE_LIST_QUESTS
|
||||
} else if *listShipsMode {
|
||||
a.Mode = MODE_LIST_SHIPS
|
||||
} else if *editHold != "" {
|
||||
a.Mode = MODE_EDIT_HOLD
|
||||
a.Ship = data.NewShip(a.ConfDir, *editHold)
|
||||
} else {
|
||||
a.Mode = MODE_RUN
|
||||
}
|
||||
|
||||
// Load Global Configs
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *AppState) run() int {
|
||||
if len(a.Parms) == 0 {
|
||||
fmt.Fprint(os.Stderr, "No request provided ('list' to list available requests)\n")
|
||||
return 1
|
||||
}
|
||||
switch a.Mode {
|
||||
case MODE_VALIDATE_QUEST:
|
||||
return a.validateQuest(a.Parms)
|
||||
case MODE_VALIDATE_SHIP:
|
||||
return a.validateShip(a.Parms)
|
||||
case MODE_EDIT_QUEST:
|
||||
return a.editQuest(a.Parms)
|
||||
case MODE_EDIT_SHIP:
|
||||
return a.editShip(a.Parms)
|
||||
case MODE_EDIT_HOLD:
|
||||
return a.editHold(a.Parms)
|
||||
case MODE_LIST_QUESTS:
|
||||
return a.listQuests(a.Parms)
|
||||
case MODE_LIST_SHIPS:
|
||||
return a.listShips(a.Parms)
|
||||
|
||||
default:
|
||||
return a.runQuest(a.Parms)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AppState) validateConfDir() error {
|
||||
if !DirExists(a.ConfDir) {
|
||||
ans := PromptWDefault(fmt.Sprintf("Create configuration directory (%s)? [n]", a.ConfDir), "n")
|
||||
if !cli.DirExists(a.ConfDir) {
|
||||
ans := cli.PromptWDefault(fmt.Sprintf("Create configuration directories (%s)? [n]", a.ConfDir), "n")
|
||||
if strings.ToLower(ans) == "y" {
|
||||
if err := os.Mkdir(a.ConfDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Mkdir(a.QuestsDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Mkdir(a.ShipsDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Mkdir(a.HoldsDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("Configuration directory doesn't exist")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user