From c253c13942a703389bb0a03916fca66844b0ad4b Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Fri, 9 Oct 2020 15:45:04 -0500 Subject: [PATCH] Initial Commit --- .gitignore | 4 ++ build.sh | 12 ++++++ cmd/ncpt/app.go | 87 ++++++++++++++++++++++++++++++++++++++ cmd/ncpt/app_command.go | 4 ++ cmd/ncpt/app_quest_cmds.go | 26 ++++++++++++ cmd/ncpt/helpers.go | 49 +++++++++++++++++++++ cmd/ncpt/main.go | 23 ++++++++++ cmd/ncpt/quest.go | 7 +++ cmd/ncpt/ship.go | 5 +++ 9 files changed, 217 insertions(+) create mode 100644 .gitignore create mode 100755 build.sh create mode 100644 cmd/ncpt/app.go create mode 100644 cmd/ncpt/app_command.go create mode 100644 cmd/ncpt/app_quest_cmds.go create mode 100644 cmd/ncpt/helpers.go create mode 100644 cmd/ncpt/main.go create mode 100644 cmd/ncpt/quest.go create mode 100644 cmd/ncpt/ship.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c420ab --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Dev Testing +/.ncpt +# Build Artifacts +/ncpt diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..8df5e59 --- /dev/null +++ b/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -e + +# Keep track of the last command executed +trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG +# Echo an error message before exiting +trap 'echo "\"${last_command}\" command finished with exit code $?."' EXIT + +# Build chroniclerd binary +cd cmd/ncpt +go build -o ncpt +mv ncpt ../../ diff --git a/cmd/ncpt/app.go b/cmd/ncpt/app.go new file mode 100644 index 0000000..9c63600 --- /dev/null +++ b/cmd/ncpt/app.go @@ -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 +} diff --git a/cmd/ncpt/app_command.go b/cmd/ncpt/app_command.go new file mode 100644 index 0000000..08bd98e --- /dev/null +++ b/cmd/ncpt/app_command.go @@ -0,0 +1,4 @@ +package main + +type Command interface { +} diff --git a/cmd/ncpt/app_quest_cmds.go b/cmd/ncpt/app_quest_cmds.go new file mode 100644 index 0000000..4fb2533 --- /dev/null +++ b/cmd/ncpt/app_quest_cmds.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "os" + "os/exec" +) + +func (a *AppState) runQuest(parms []string) int { + return 0 +} + +func (a *AppState) editQuest(parms []string) int { + if len(parms) == 0 { + fmt.Println("Quest name must be provided.") + return 1 + } + cmd := exec.Command(os.Getenv("EDITOR"), fmt.Sprintf("%s/%s", a.ConfDir, parms[0])) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + err := cmd.Run() + if err != nil { + fmt.Printf("Error editing Quest (%s)", parms[0]) + } + return 0 +} diff --git a/cmd/ncpt/helpers.go b/cmd/ncpt/helpers.go new file mode 100644 index 0000000..81bf40e --- /dev/null +++ b/cmd/ncpt/helpers.go @@ -0,0 +1,49 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func PromptWDefault(label, def string) string { + ret := Prompt(label, false) + if ret == "" { + return def + } + return ret +} + +func Prompt(label string, required bool) string { + reader := bufio.NewReader(os.Stdin) + var res string + fmt.Println(label + ": ") + res, _ = reader.ReadString('\n') + res = strings.TrimSpace(res) + if res == "" && required { + fmt.Println("Non-empty response is required") + return Prompt(label, required) + } + return res +} + +func FileExists(path string) bool { + info, err := os.Stat(path) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +func DirExists(path string) bool { + info, err := os.Stat(path) + if os.IsNotExist(err) { + return false + } + return info.IsDir() +} + +func PrintErr(o string) { + fmt.Fprint(os.Stderr, o) +} diff --git a/cmd/ncpt/main.go b/cmd/ncpt/main.go new file mode 100644 index 0000000..0ea7754 --- /dev/null +++ b/cmd/ncpt/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "os" +) + +const ( + AppName = "netcaptain" + AppVersion = 1 +) + +var app *AppState + +func main() { + fmt.Println("ncpt - NetCaptain") + app = NewApp() + if app.Error != nil { + PrintErr(app.Error.Error()) + os.Exit(1) + } + os.Exit(app.run()) +} diff --git a/cmd/ncpt/quest.go b/cmd/ncpt/quest.go new file mode 100644 index 0000000..d7897a4 --- /dev/null +++ b/cmd/ncpt/quest.go @@ -0,0 +1,7 @@ +package main + +type Quest struct { + Method string + Url string + Headers map[string]string +} diff --git a/cmd/ncpt/ship.go b/cmd/ncpt/ship.go new file mode 100644 index 0000000..417ba12 --- /dev/null +++ b/cmd/ncpt/ship.go @@ -0,0 +1,5 @@ +package main + +type Ship struct { + Name string +}