Initial Commit
This commit is contained in:
commit
c253c13942
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# Dev Testing
|
||||
/.ncpt
|
||||
# Build Artifacts
|
||||
/ncpt
|
12
build.sh
Executable file
12
build.sh
Executable file
@ -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 ../../
|
87
cmd/ncpt/app.go
Normal file
87
cmd/ncpt/app.go
Normal file
@ -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
|
||||
}
|
4
cmd/ncpt/app_command.go
Normal file
4
cmd/ncpt/app_command.go
Normal file
@ -0,0 +1,4 @@
|
||||
package main
|
||||
|
||||
type Command interface {
|
||||
}
|
26
cmd/ncpt/app_quest_cmds.go
Normal file
26
cmd/ncpt/app_quest_cmds.go
Normal file
@ -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
|
||||
}
|
49
cmd/ncpt/helpers.go
Normal file
49
cmd/ncpt/helpers.go
Normal file
@ -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)
|
||||
}
|
23
cmd/ncpt/main.go
Normal file
23
cmd/ncpt/main.go
Normal file
@ -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())
|
||||
}
|
7
cmd/ncpt/quest.go
Normal file
7
cmd/ncpt/quest.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
type Quest struct {
|
||||
Method string
|
||||
Url string
|
||||
Headers map[string]string
|
||||
}
|
5
cmd/ncpt/ship.go
Normal file
5
cmd/ncpt/ship.go
Normal file
@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
type Ship struct {
|
||||
Name string
|
||||
}
|
Loading…
Reference in New Issue
Block a user