Initial Commit
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
MODE_RUN = iota
|
||||
MODE_EDIT_QUEST
|
||||
)
|
||||
|
||||
type AppState struct {
|
||||
Name string
|
||||
Version int
|
||||
ConfDir string
|
||||
Parms []string
|
||||
Ships map[string]Ship
|
||||
Quests map[string]Quest
|
||||
|
||||
Mode int
|
||||
|
||||
// CLI Variables
|
||||
Environment string
|
||||
Request 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")
|
||||
editMode := flag.Bool("e", false, "Edit the named Quest")
|
||||
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
|
||||
}
|
||||
|
||||
// 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_EDIT_QUEST:
|
||||
return a.editQuest(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 strings.ToLower(ans) == "y" {
|
||||
if err := os.Mkdir(a.ConfDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("Configuration directory doesn't exist")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user