Git Ignore flags

This commit is contained in:
Brian Buller 2020-11-02 11:53:17 -06:00
parent c13d1daa87
commit 3c20c097be
5 changed files with 131 additions and 15 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 ncpt 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_QUESTS
MODE_LIST_SHIPS MODE_LIST_SHIPS
MODE_OUTPUT_HOLD_VALUE MODE_OUTPUT_HOLD_VALUE
MODE_GIT_CONFIG
) )
type AppState struct { type AppState struct {
@ -50,6 +51,7 @@ func NewApp() *AppState {
func (a *AppState) initialize() error { func (a *AppState) initialize() error {
flag.StringVar(&a.ConfDir, "d", ".ncpt", "The configuration directory to use") flag.StringVar(&a.ConfDir, "d", ".ncpt", "The configuration directory to use")
flag.BoolVar(&a.Verbose, "v", false, "Run in Verbose mode") 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") validateQuest := flag.String("c", "", "Validate that the given quest is _c_orrect")
validateShip := flag.String("C", "", "Validate that the given ship is _C_orrect") validateShip := flag.String("C", "", "Validate that the given ship is _C_orrect")
editQuest := flag.String("e", "", "Edit the given quest") editQuest := flag.String("e", "", "Edit the given quest")
@ -93,6 +95,9 @@ func (a *AppState) initialize() error {
} else if *outputHoldValue != "" { } else if *outputHoldValue != "" {
a.Mode = MODE_OUTPUT_HOLD_VALUE a.Mode = MODE_OUTPUT_HOLD_VALUE
a.Ship = data.NewShip(a.ConfDir, *outputHoldValue) a.Ship = data.NewShip(a.ConfDir, *outputHoldValue)
} else if *gitFlag != "" {
a.Parms = append([]string{*gitFlag}, a.Parms...)
a.Mode = MODE_GIT_CONFIG
} else { } else {
a.Mode = MODE_RUN a.Mode = MODE_RUN
} }
@ -119,6 +124,8 @@ func (a *AppState) run() int {
return a.listShips(a.Parms) return a.listShips(a.Parms)
case MODE_OUTPUT_HOLD_VALUE: case MODE_OUTPUT_HOLD_VALUE:
return a.outputHoldValue(a.Parms) return a.outputHoldValue(a.Parms)
case MODE_GIT_CONFIG:
return a.gitMode(a.Parms)
default: default:
return a.runQuest(a.Parms) return a.runQuest(a.Parms)
@ -145,6 +152,14 @@ func (a *AppState) validateConfDir() error {
cli.PrintErr(fmt.Sprintf("Error creating Holds directory (%s)", a.HoldsDir)) cli.PrintErr(fmt.Sprintf("Error creating Holds directory (%s)", a.HoldsDir))
return err 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 { } else {
return errors.New("Configuration directory doesn't exist") 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

@ -2,12 +2,35 @@ package cli
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "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 { func PromptWDefault(label, def string) string {
ret := Prompt(label, false) ret := Prompt(label, false)
if ret == "" { if ret == "" {