package main import ( "errors" "flag" "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 MODE_OUTPUT_HOLD_VALUE ) type AppState struct { Name string Version int ConfDir string Parms []string Verbose bool Mode int QuestsDir string Quest *data.Quest ShipsDir string Ship *data.Ship HoldsDir string 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") flag.BoolVar(&a.Verbose, "v", false, "Run in Verbose mode") validateQuest := flag.String("c", "", "Validate that the given quest is _c_orrect") validateShip := flag.String("C", "", "Validate that 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 available Quests") listShipsMode := flag.Bool("L", false, "List available Ships") editHold := flag.String("H", "", "Edit the hold for the given ship") outputHoldValue := flag.String("h", "", "Output the given value from the given ship's hold") flag.Parse() a.Parms = flag.Args() 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 if *outputHoldValue != "" { a.Mode = MODE_OUTPUT_HOLD_VALUE a.Ship = data.NewShip(a.ConfDir, *outputHoldValue) } else { a.Mode = MODE_RUN } // Load Global Configs return nil } func (a *AppState) run() int { 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) case MODE_OUTPUT_HOLD_VALUE: return a.outputHoldValue(a.Parms) default: return a.runQuest(a.Parms) } } func (a *AppState) validateConfDir() error { 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") } } return nil }