Compare commits

...

5 Commits

6 changed files with 140 additions and 19 deletions

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
BIN=$(CURDIR)/build
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || echo v0)
.PHONY: build clean
build:
go build -o build/ncpt -ldflags="-X main.version=${VERSION}" cmd/ncpt/*
clean:
rm -f build/ncpt

View File

@ -1,15 +0,0 @@
#!/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
if [ ! -d ../../build ]; then
mkdir ../../build
fi
mv ncpt ../../build

View File

@ -21,6 +21,7 @@ const (
MODE_LIST_QUESTS
MODE_LIST_SHIPS
MODE_OUTPUT_HOLD_VALUE
MODE_GIT_CONFIG
)
type AppState struct {
@ -50,6 +51,7 @@ func NewApp() *AppState {
func (a *AppState) initialize() error {
flag.StringVar(&a.ConfDir, "d", ".ncpt", "The configuration directory to use")
flag.BoolVar(&a.Verbose, "v", false, "Run in Verbose mode")
gitFlag := flag.String("gitignore", "", "Configure the gitignore file")
validateQuest := flag.String("c", "", "Validate that the given quest is _c_orrect")
validateShip := flag.String("C", "", "Validate that the given ship is _C_orrect")
editQuest := flag.String("e", "", "Edit the given quest")
@ -62,13 +64,14 @@ func (a *AppState) initialize() error {
flag.Parse()
a.Parms = flag.Args()
if err := a.validateConfDir(); err != nil {
return err
}
a.QuestsDir = a.ConfDir + "/quests"
a.ShipsDir = a.ConfDir + "/ships"
a.HoldsDir = a.ConfDir + "/holds"
if err := a.validateConfDir(); err != nil {
return err
}
// Figure out what 'mode' we're running in
if *validateQuest != "" {
a.Mode = MODE_VALIDATE_QUEST
@ -92,6 +95,9 @@ func (a *AppState) initialize() error {
} else if *outputHoldValue != "" {
a.Mode = MODE_OUTPUT_HOLD_VALUE
a.Ship = data.NewShip(a.ConfDir, *outputHoldValue)
} else if *gitFlag != "" {
a.Parms = append([]string{*gitFlag}, a.Parms...)
a.Mode = MODE_GIT_CONFIG
} else {
a.Mode = MODE_RUN
}
@ -118,6 +124,8 @@ func (a *AppState) run() int {
return a.listShips(a.Parms)
case MODE_OUTPUT_HOLD_VALUE:
return a.outputHoldValue(a.Parms)
case MODE_GIT_CONFIG:
return a.gitMode(a.Parms)
default:
return a.runQuest(a.Parms)
@ -129,17 +137,29 @@ func (a *AppState) validateConfDir() error {
ans := cli.PromptWDefault(fmt.Sprintf("Create configuration directories (%s)? [n]", a.ConfDir), "n")
if strings.ToLower(ans) == "y" {
if err := os.Mkdir(a.ConfDir, 0700); err != nil {
cli.PrintErr("Error creating main configuration directory")
return err
}
if err := os.Mkdir(a.QuestsDir, 0700); err != nil {
cli.PrintErr(fmt.Sprintf("Error creating Quests directory (%s)", a.QuestsDir))
return err
}
if err := os.Mkdir(a.ShipsDir, 0700); err != nil {
cli.PrintErr(fmt.Sprintf("Error creating Ships directory (%s)", a.ShipsDir))
return err
}
if err := os.Mkdir(a.HoldsDir, 0700); err != nil {
cli.PrintErr(fmt.Sprintf("Error creating Holds directory (%s)", a.HoldsDir))
return err
}
fmt.Println("Create gitignore in configuration directory? [y]")
ans = cli.PromptWDefault("By default, the 'holds' directory is ignored.", "y")
if strings.ToLower(ans) == "y" {
resp := a.gitIgnoreHold([]string{})
if resp != 0 {
return errors.New("Error creating gitignore")
}
}
} else {
return errors.New("Configuration directory doesn't exist")
}

84
cmd/ncpt/app_git_cmds.go Normal file
View File

@ -0,0 +1,84 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"git.bullercodeworks.com/brian/netcaptain/internal/cli"
)
func (a *AppState) gitMode(parms []string) int {
var p string
if len(parms) == 0 {
cli.PrintErr("Error parsing gitignore parameter")
return 1
}
p = parms[0]
switch p {
case "hold": // Ignore just the 'config/hold'
return a.gitIgnoreHold(parms)
case "all": // Ignore all of 'config'
return a.gitIgnoreAll(parms)
case "none": // Ignore nothing
return a.gitIgnoreNone(parms)
default: // Display the current gitignore mode
return a.gitIgnoreShow(parms)
}
}
func (a *AppState) gitIgnoreShow(parms []string) int {
hasGitIgnore := cli.FileExists(a.ConfDir + "/.gitignore")
// Just outputting the current gitignore status
if !hasGitIgnore {
fmt.Println("No git ignore.")
return 0
}
data, err := ioutil.ReadFile(a.ConfDir + "/.gitignore")
if err != nil {
cli.PrintErr("Error reading .gitignore")
return 1
}
fmt.Println(string(data))
return 0
}
func (a *AppState) gitIgnoreHold(parms []string) int {
f, err := os.OpenFile(a.ConfDir+"/.gitignore", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
cli.PrintErr(err.Error())
return 1
}
defer f.Close()
_, err = f.WriteString("holds/")
if err != nil {
cli.PrintErr(err.Error())
return 1
}
return 0
}
func (a *AppState) gitIgnoreAll(parms []string) int {
f, err := os.OpenFile(a.ConfDir+"/.gitignore", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
cli.PrintErr(err.Error())
return 1
}
defer f.Close()
_, err = f.WriteString("./")
if err != nil {
cli.PrintErr(err.Error())
return 1
}
return 0
}
func (a *AppState) gitIgnoreNone(parms []string) int {
data, err := ioutil.ReadFile(a.ConfDir + "/.gitignore")
if err != nil {
cli.PrintErr("Error reading .gitignore")
return 1
}
fmt.Println(string(data))
return 0
}

View File

@ -43,7 +43,7 @@ func (a *AppState) runQuest(parms []string) int {
a.Quest.Verbose = a.Verbose
parms = parms[1:]
var ships []string
if len(parms) > 1 {
if len(parms) > 0 {
ships = append(ships, parms[0])
}
ships = append(ships, "default")

View File

@ -2,12 +2,35 @@ package cli
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
func HasParameter(label string, parms []string) bool {
for _, v := range parms {
if strings.HasPrefix(v, "-"+label) || strings.HasPrefix(v, "--"+label) {
return true
}
}
return false
}
func GetParameter(label string, parms []string) (string, error) {
for _, v := range parms {
if strings.HasPrefix(v, "-"+label) || strings.HasPrefix(v, "--"+label) {
parts := strings.Split(v, "=")
if len(parts) > 1 {
return parts[1], nil
}
return "", nil
}
}
return "", errors.New("Parameter does not exist")
}
func PromptWDefault(label, def string) string {
ret := Prompt(label, false)
if ret == "" {