Basically Functional

This commit is contained in:
2020-10-14 16:59:41 -05:00
parent c253c13942
commit c1f7e8ae91
14 changed files with 571 additions and 50 deletions

65
internal/cli/helpers.go Normal file
View File

@@ -0,0 +1,65 @@
package cli
import (
"bufio"
"fmt"
"os"
"path/filepath"
"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 ListFiles(path string) []string {
var files []string
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
p = strings.TrimPrefix(p, path)
if p != "" {
files = append(files, strings.TrimPrefix(p, "/"))
}
return nil
})
if err != nil {
return []string{}
}
return files
}
func PrintErr(o string) {
fmt.Fprintln(os.Stderr, o)
}

128
internal/data/quest.go Normal file
View File

@@ -0,0 +1,128 @@
package data
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"text/template"
"git.bullercodeworks.com/brian/netcaptain/internal/cli"
"git.bullercodeworks.com/brian/netcaptain/internal/web"
)
type Quest struct {
Name string
Dir string
Verbose bool
Headers map[string]string
Method string
Url string
Body string
Raw string
Processed string
Args map[string]string
loaded bool
}
func NewQuest(dir, nm string) *Quest {
return &Quest{
Name: nm,
Dir: dir,
Headers: make(map[string]string),
Args: make(map[string]string),
}
}
func (q *Quest) Load() error {
if !cli.FileExists(q.FilePath()) {
return errors.New("File Not Found")
}
data, err := ioutil.ReadFile(q.FilePath())
if err != nil {
return err
}
q.Raw = string(data)
t, err := template.New(q.Name).Parse(q.Raw)
if err != nil {
return errors.New(fmt.Sprintf("Error parsing Quest as template: %s", err.Error()))
}
var tpl bytes.Buffer
err = t.Execute(&tpl, q)
if err != nil {
return errors.New(fmt.Sprintf("Error executing Quest template: %s\n->%s", q.Name, err.Error()))
}
q.Processed = tpl.String()
q.loaded = true
return nil
}
func (q *Quest) LoadArgs(parms []string) {
for k, v := range parms {
q.Args[fmt.Sprintf("Arg%d", k)] = v
}
}
func (q *Quest) FilePath() string {
return fmt.Sprintf("%s/quests/%s", q.Dir, q.Name)
}
func (q *Quest) IsValid() bool {
return q.loaded
}
func (q *Quest) request() *http.Request {
headers := make(map[string][]string)
var method, url, body string
for _, v := range strings.Split(q.Processed, "\n") {
flds := strings.Fields(v)
if len(flds) > 0 {
if web.IsHttpMethod(flds[0]) {
// This is the request line
method = flds[0]
url = flds[1]
} else if method == "" && len(flds) > 1 {
// Headers
k := strings.TrimSuffix(flds[0], ":")
v := strings.Join(flds[1:], " ")
headers[k] = append(headers[k], v)
} else {
// Body
if body != "" {
body = body + "\n"
}
body = body + v
}
}
}
request, _ := http.NewRequest(method, url, bytes.NewBuffer([]byte(body)))
for k, v := range headers {
request.Header[k] = v
}
return request
}
func (q *Quest) Completer() (*http.Response, error) {
client := &http.Client{}
return client.Do(q.request())
}
func (q *Quest) Complete() int {
resp, _ := q.Completer()
if q.Verbose {
for k, v := range resp.Header {
fmt.Println(k, v)
}
}
defer resp.Body.Close()
rbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
cli.PrintErr(err.Error())
return 1
}
fmt.Println(string(rbody))
return 0
}

189
internal/data/ship.go Normal file
View File

@@ -0,0 +1,189 @@
package data
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"text/template"
"git.bullercodeworks.com/brian/netcaptain/internal/cli"
)
type Ship struct {
Name string `json:"-"`
Dir string `json:"-"`
Verbose bool `json:"-"`
Crew map[string]string `json:"crew"` // Static Members of the Ship
PlunderRules map[string]map[string]string `json:"plunder"` // Definitions for storing dynamic values
Hold map[string]string `json:"-"` // Where dynamic values are stored
Args map[string]string `json:"-"` // Arguments passed in from the command line
Raw []byte `json:"-"`
loaded bool `json:"-"`
}
func NewShip(dir, nm string) *Ship {
return &Ship{
Name: nm,
Dir: dir,
Crew: make(map[string]string),
PlunderRules: make(map[string]map[string]string),
Args: make(map[string]string),
Hold: make(map[string]string),
}
}
func (s *Ship) Load() error {
if !s.Exists() {
return errors.New("Ship File not Found")
}
var err error
s.Raw, err = ioutil.ReadFile(s.FilePath())
if err != nil {
return err
}
// Parse 'Raw' into Crew and Plunder
json.Unmarshal(s.Raw, &s)
for quest, questRules := range s.PlunderRules {
for key, val := range questRules {
ruleName := quest + "." + key
t, err := template.New(ruleName).Parse(val)
if err != nil {
return errors.New(fmt.Sprintf("Error parsing plunder rule: %s", ruleName))
}
var tpl bytes.Buffer
err = t.Execute(&tpl, s)
if err != nil {
return errors.New(fmt.Sprintf("Error executing plunder rule: %s\n->%s", ruleName, err.Error()))
}
}
}
err = s.LoadHold()
if err != nil {
return err
}
s.loaded = true
return nil
}
func (s *Ship) LoadArgs(parms []string) {
for k, v := range parms {
s.Args[fmt.Sprintf("Arg%d", k)] = v
}
}
func (s *Ship) Exists() bool {
return cli.FileExists(s.FilePath())
}
func (s *Ship) HoldExists() bool {
return cli.FileExists(s.HoldFilePath())
}
func (s *Ship) FilePath() string {
return fmt.Sprintf("%s/ships/%s.json", s.Dir, s.Name)
}
func (s *Ship) HoldFilePath() string {
return fmt.Sprintf("%s/holds/%s.json", s.Dir, s.Name)
}
func (s *Ship) IsValid() bool {
return s.loaded
}
func (s *Ship) Sail(q *Quest) int {
// Set up the Quest
if !cli.FileExists(q.FilePath()) {
cli.PrintErr(fmt.Sprintf("Quest file (%s) not found", q.Name))
return 1
}
data, err := ioutil.ReadFile(q.FilePath())
if err != nil {
cli.PrintErr(fmt.Sprintf("Error reading quest file (%s)", q.Name))
return 1
}
q.Raw = string(data)
t, err := template.New(s.Name + "." + q.Name).Parse(q.Raw)
if err != nil {
cli.PrintErr(fmt.Sprintf("Error parsing Quest as template: %s", err.Error()))
return 1
}
var tpl bytes.Buffer
err = t.Execute(&tpl, s)
if err != nil {
cli.PrintErr(fmt.Sprintf("Error executing Quest template: %s\n->%s", q.Name, err.Error()))
return 1
}
q.Processed = tpl.String()
q.loaded = true
resp, err := q.Completer()
if err != nil {
cli.PrintErr(err.Error())
return 1
}
defer resp.Body.Close()
rbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
cli.PrintErr(err.Error())
return 1
}
// Check if we need to plunder from this quest
if err = s.Plunder(q, rbody); err != nil {
cli.PrintErr(err.Error())
return 1
}
fmt.Println(string(rbody))
err = s.SaveHold()
if err != nil {
cli.PrintErr(err.Error())
return 1
}
return 0
}
func (s *Ship) Plunder(q *Quest, body []byte) error {
plunder, ok := s.PlunderRules[q.Name]
if !ok {
return nil
}
for k, v := range plunder {
buff := bytes.NewBuffer(body)
var result map[string]interface{}
json.NewDecoder(buff).Decode(&result)
switch val := result[v].(type) {
case string:
s.Hold[k] = val
}
}
return nil
}
/* Hold Functions */
func (s *Ship) LoadHold() error {
hold, err := ioutil.ReadFile(s.HoldFilePath())
if err != nil {
f, err := os.OpenFile(s.HoldFilePath(), os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
return err
}
json.NewDecoder(bytes.NewBuffer(hold)).Decode(&s.Hold)
return nil
}
func (s *Ship) SaveHold() error {
f, err := os.OpenFile(s.HoldFilePath(), os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.Encode(s.Hold)
return nil
}

12
internal/web/helpers.go Normal file
View File

@@ -0,0 +1,12 @@
package web
import "net/http"
func IsHttpMethod(val string) bool {
switch val {
case http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodConnect, http.MethodOptions, http.MethodTrace:
return true
default:
return false
}
}