netcaptain/cmd/ncpt/app_quest_cmds.go

93 lines
1.8 KiB
Go

package main
import (
"fmt"
"os"
"os/exec"
"git.bullercodeworks.com/brian/netcaptain/internal/cli"
"git.bullercodeworks.com/brian/netcaptain/internal/data"
)
func (a *AppState) listQuests(parms []string) int {
files := cli.ListFiles(a.QuestsDir)
for _, v := range files {
fmt.Println(v)
}
return 0
}
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 {
if len(parms) == 0 {
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.")
return 1
}
cmd := exec.Command(os.Getenv("EDITOR"), a.Quest.FilePath())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
cli.PrintErr(fmt.Sprintf("Error editing Quest (%s)", a.Quest.Name))
}
return 0
}