Git Ignore flags
This commit is contained in:
@@ -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")
|
||||
@@ -93,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
|
||||
}
|
||||
@@ -119,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)
|
||||
@@ -145,6 +152,14 @@ func (a *AppState) validateConfDir() error {
|
||||
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
84
cmd/ncpt/app_git_cmds.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user