diff --git a/Dockerfile b/Dockerfile index a4f994c..4429edf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.19-alpine as builder +FROM golang:1.20-alpine as builder WORKDIR /build diff --git a/README.md b/README.md index aa80b6f..6cad578 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,14 @@ Replaces existing .gitignore based on given language in defined location (either ignoreinit replace ``` +### Merge existing gitignore + +Merges a gitignore for given language into existing .gitignore in defined location (either relative or absolute). + +```bash +ignoreinit merge +``` + ## Build from source ```bash diff --git a/main.go b/main.go index ec3ea9d..6c68260 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "github.com/loupeznik/ignoreinit/src" ) -const version = "1.0.2" +const version = "1.1.0" func main() { flags := src.Flags{} diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index bb94d88..41c9d00 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,6 +1,6 @@ name: ignoreinit base: core20 -version: '1.0.3' +version: '1.1.0' summary: A tool for creating .gitignore files from the command line license: MIT contact: contact@dzarsky.eu diff --git a/src/flags.go b/src/flags.go index 42d24cc..f6c8ce2 100644 --- a/src/flags.go +++ b/src/flags.go @@ -9,4 +9,7 @@ type Flags struct { Replace struct { Settings bool `settings:"true" allow-unknown-arg:"true"` } `command:"replace" description:"Replace current .gitignore - use ignoreinit replace " nonempty:"true"` + Merge struct { + Settings bool `settings:"true" allow-unknown-arg:"true"` + } `command:"merge" description:"Merge a new gitignore into current .gitignore - use ignoreinit merge " nonempty:"true"` } diff --git a/src/handlers.go b/src/handlers.go index a74f0d7..2a81be0 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -16,6 +16,7 @@ import ( const ( fncInit = "Init" fncReplace = "Replace" + fncMerge = "Merge" gitOwner = "github" gitRepo = "gitignore" ) @@ -33,7 +34,7 @@ func InitHandlers() { return nil } - err = getIgnore(language, location, true) + err = getIgnore(language, location, true, false) if err != nil { return err @@ -56,7 +57,7 @@ func InitHandlers() { return nil } - err = getIgnore(language, location, false) + err = getIgnore(language, location, false, false) if err != nil { return err @@ -66,6 +67,29 @@ func InitHandlers() { return nil }) + + gocmd.HandleFlag(fncMerge, func(cmd *gocmd.Cmd, args []string) error { + language, location, err := handleParams(cmd.FlagArgs(fncMerge)[1:]) + + if err != nil { + return err + } + + if _, err := os.Stat(path.Join(location, ".gitignore")); errors.Is(err, os.ErrNotExist) { + fmt.Printf(".gitignore does not exist in %s\n", location) + return nil + } + + err = getIgnore(language, location, false, true) + + if err != nil { + return err + } + + fmt.Printf("Merged .gitignore in %s\n", location) + + return nil + }) } func handleParams(params []string) (string, string, error) { @@ -80,7 +104,7 @@ func handleParams(params []string) (string, string, error) { return params[0], params[1], nil } -func getIgnore(language string, location string, isNew bool) error { +func getIgnore(language string, location string, isNew bool, isMerge bool) error { client := github.NewClient(nil) ctx := context.Background() options := &github.RepositoryContentGetOptions{} @@ -124,6 +148,24 @@ func getIgnore(language string, location string, isNew bool) error { var file *os.File fullPath := path.Join(location, ".gitignore") + if isMerge { + file, err = os.OpenFile(fullPath, os.O_RDWR|os.O_APPEND, 0755) + + if err != nil { + return err + } + + defer file.Close() + + _, err = file.Write(bytes) + + if err != nil { + return err + } + + return nil + } + if isNew { file, err = os.Create(fullPath) } else {