netcaptain/cmd/ncpt/app_git_cmds.go

85 lines
1.8 KiB
Go

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
}