netcaptain/cmd/ncpt/app.go

149 lines
3.6 KiB
Go
Raw Normal View History

2020-10-09 20:45:04 +00:00
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
2020-10-14 21:59:41 +00:00
"git.bullercodeworks.com/brian/netcaptain/internal/cli"
"git.bullercodeworks.com/brian/netcaptain/internal/data"
2020-10-09 20:45:04 +00:00
)
const (
MODE_RUN = iota
2020-10-14 21:59:41 +00:00
MODE_VALIDATE_QUEST
MODE_VALIDATE_SHIP
2020-10-09 20:45:04 +00:00
MODE_EDIT_QUEST
2020-10-14 21:59:41 +00:00
MODE_EDIT_SHIP
MODE_EDIT_HOLD
MODE_LIST_QUESTS
MODE_LIST_SHIPS
2020-10-16 20:39:07 +00:00
MODE_OUTPUT_HOLD_VALUE
2020-10-09 20:45:04 +00:00
)
type AppState struct {
Name string
Version int
ConfDir string
Parms []string
2020-10-14 21:59:41 +00:00
Verbose bool
2020-10-09 20:45:04 +00:00
Mode int
2020-10-14 21:59:41 +00:00
QuestsDir string
Quest *data.Quest
ShipsDir string
Ship *data.Ship
HoldsDir string
2020-10-09 20:45:04 +00:00
Error error
}
func NewApp() *AppState {
app := &AppState{Name: AppName, Version: AppVersion}
app.Error = app.initialize()
return app
}
func (a *AppState) initialize() error {
flag.StringVar(&a.ConfDir, "d", ".ncpt", "The configuration directory to use")
2020-10-14 21:59:41 +00:00
flag.BoolVar(&a.Verbose, "v", false, "Run in Verbose mode")
2020-10-16 20:39:07 +00:00
validateQuest := flag.String("c", "", "Validate that the given quest is _c_orrect")
validateShip := flag.String("C", "", "Validate that the given ship is _C_orrect")
2020-10-14 21:59:41 +00:00
editQuest := flag.String("e", "", "Edit the given quest")
editShip := flag.String("E", "", "Edit the given ship")
2020-10-16 20:39:07 +00:00
listQuestsMode := flag.Bool("l", false, "List available Quests")
listShipsMode := flag.Bool("L", false, "List available Ships")
2020-10-14 21:59:41 +00:00
editHold := flag.String("H", "", "Edit the hold for the given ship")
2020-10-16 20:39:07 +00:00
outputHoldValue := flag.String("h", "", "Output the given value from the given ship's hold")
2020-10-14 21:59:41 +00:00
2020-10-09 20:45:04 +00:00
flag.Parse()
a.Parms = flag.Args()
2020-10-14 21:59:41 +00:00
if err := a.validateConfDir(); err != nil {
return err
}
a.QuestsDir = a.ConfDir + "/quests"
a.ShipsDir = a.ConfDir + "/ships"
a.HoldsDir = a.ConfDir + "/holds"
2020-10-09 20:45:04 +00:00
// Figure out what 'mode' we're running in
2020-10-14 21:59:41 +00:00
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 != "" {
2020-10-09 20:45:04 +00:00
a.Mode = MODE_EDIT_QUEST
2020-10-14 21:59:41 +00:00
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)
2020-10-16 20:39:07 +00:00
} else if *outputHoldValue != "" {
a.Mode = MODE_OUTPUT_HOLD_VALUE
a.Ship = data.NewShip(a.ConfDir, *outputHoldValue)
2020-10-09 20:45:04 +00:00
} else {
a.Mode = MODE_RUN
}
// Load Global Configs
return nil
}
func (a *AppState) run() int {
switch a.Mode {
2020-10-14 21:59:41 +00:00
case MODE_VALIDATE_QUEST:
return a.validateQuest(a.Parms)
case MODE_VALIDATE_SHIP:
return a.validateShip(a.Parms)
2020-10-09 20:45:04 +00:00
case MODE_EDIT_QUEST:
return a.editQuest(a.Parms)
2020-10-14 21:59:41 +00:00
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)
2020-10-16 20:39:07 +00:00
case MODE_OUTPUT_HOLD_VALUE:
return a.outputHoldValue(a.Parms)
2020-10-14 21:59:41 +00:00
2020-10-09 20:45:04 +00:00
default:
return a.runQuest(a.Parms)
}
}
func (a *AppState) validateConfDir() error {
2020-10-14 21:59:41 +00:00
if !cli.DirExists(a.ConfDir) {
ans := cli.PromptWDefault(fmt.Sprintf("Create configuration directories (%s)? [n]", a.ConfDir), "n")
2020-10-09 20:45:04 +00:00
if strings.ToLower(ans) == "y" {
if err := os.Mkdir(a.ConfDir, 0700); err != nil {
return err
}
2020-10-14 21:59:41 +00:00
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
}
2020-10-09 20:45:04 +00:00
} else {
return errors.New("Configuration directory doesn't exist")
}
}
return nil
}