Skip to content

Commit

Permalink
Add merge functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Loupeznik committed Jul 21, 2023
1 parent ef55fce commit 962424c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19-alpine as builder
FROM golang:1.20-alpine as builder

WORKDIR /build

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ Replaces existing .gitignore based on given language in defined location (either
ignoreinit replace <language> <location>
```

### Merge existing gitignore

Merges a gitignore for given language into existing .gitignore in defined location (either relative or absolute).

```bash
ignoreinit merge <language> <location>
```

## Build from source

```bash
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <language> <location>" 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 <language> <location>" nonempty:"true"`
}
48 changes: 45 additions & 3 deletions src/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
const (
fncInit = "Init"
fncReplace = "Replace"
fncMerge = "Merge"
gitOwner = "github"
gitRepo = "gitignore"
)
Expand All @@ -33,7 +34,7 @@ func InitHandlers() {
return nil
}

err = getIgnore(language, location, true)
err = getIgnore(language, location, true, false)

if err != nil {
return err
Expand All @@ -56,7 +57,7 @@ func InitHandlers() {
return nil
}

err = getIgnore(language, location, false)
err = getIgnore(language, location, false, false)

if err != nil {
return err
Expand All @@ -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) {
Expand All @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 962424c

Please sign in to comment.