2020-10-09 20:45:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-10-14 21:59:41 +00:00
|
|
|
|
|
|
|
"git.bullercodeworks.com/brian/netcaptain/internal/cli"
|
|
|
|
"git.bullercodeworks.com/brian/netcaptain/internal/data"
|
2020-10-09 20:45:04 +00:00
|
|
|
)
|
|
|
|
|
2020-10-14 21:59:41 +00:00
|
|
|
func (a *AppState) listQuests(parms []string) int {
|
|
|
|
files := cli.ListFiles(a.QuestsDir)
|
|
|
|
for _, v := range files {
|
|
|
|
fmt.Println(v)
|
|
|
|
}
|
2020-10-09 20:45:04 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-10-14 21:59:41 +00:00
|
|
|
func (a *AppState) validateQuest(parms []string) int {
|
|
|
|
if a.Quest == nil {
|
|
|
|
cli.PrintErr("No quest given")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
a.Quest.LoadArgs(parms)
|
|
|
|
if err := a.Quest.Load(); err != nil {
|
|
|
|
cli.PrintErr(fmt.Sprintf("Error loading quest: %s", err.Error()))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if !a.Quest.IsValid() {
|
|
|
|
cli.PrintErr("Quest is not valid")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) runQuest(parms []string) int {
|
2020-10-09 20:45:04 +00:00
|
|
|
if len(parms) == 0 {
|
2020-10-14 21:59:41 +00:00
|
|
|
cli.PrintErr("No Quest Given")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
a.Quest = data.NewQuest(a.ConfDir, parms[0])
|
|
|
|
a.Quest.Verbose = a.Verbose
|
|
|
|
parms = parms[1:]
|
|
|
|
var ships []string
|
|
|
|
if len(parms) > 1 {
|
|
|
|
ships = append(ships, parms[0])
|
|
|
|
}
|
|
|
|
ships = append(ships, "default")
|
|
|
|
for _, v := range ships {
|
|
|
|
s := data.NewShip(a.ConfDir, v)
|
|
|
|
if s.Exists() {
|
|
|
|
a.Ship = s
|
|
|
|
a.Ship.Verbose = a.Verbose
|
|
|
|
if len(parms) > 0 && parms[0] == v {
|
|
|
|
parms = parms[1:]
|
|
|
|
}
|
|
|
|
a.Ship.LoadArgs(parms)
|
|
|
|
err := a.Ship.Load()
|
|
|
|
if err != nil {
|
|
|
|
cli.PrintErr(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.Ship != nil {
|
|
|
|
return a.Ship.Sail(a.Quest)
|
|
|
|
}
|
|
|
|
a.Quest.LoadArgs(parms)
|
|
|
|
err := a.Quest.Load()
|
|
|
|
if err != nil {
|
|
|
|
cli.PrintErr(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return a.Quest.Complete()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppState) editQuest(parms []string) int {
|
|
|
|
if a.Quest == nil {
|
|
|
|
cli.PrintErr("Quest name must be provided.")
|
2020-10-09 20:45:04 +00:00
|
|
|
return 1
|
|
|
|
}
|
2020-10-14 21:59:41 +00:00
|
|
|
cmd := exec.Command(os.Getenv("EDITOR"), a.Quest.FilePath())
|
2020-10-09 20:45:04 +00:00
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2020-10-14 21:59:41 +00:00
|
|
|
cli.PrintErr(fmt.Sprintf("Error editing Quest (%s)", a.Quest.Name))
|
2020-10-09 20:45:04 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|