Skip to content

Commit

Permalink
Added support for username/password based authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Aug 2, 2019
1 parent 61f1062 commit e7cf208
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ Monitor a repository for changes and automatically pull & push changes:
gitomatic <path>
```

Available parameters:
Auth methods:

```
gitomatic -interval 30m
gitomatic -privkey ~/.ssh/id_rsa
gitomatic -username "someone" -password "mypass"
```

Other parameters:

```
gitomatic -interval 30m
gitomatic -author "John Doe"
gitomatic -email "[email protected]"
```
33 changes: 24 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import (
"os"
"time"

"github.com/mitchellh/go-homedir"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)

var (
author = flag.String("author", "gitomatic", "author name for git commits")
email = flag.String("email", "[email protected]", "email address for git commits")
interval = flag.String("interval", "1m", "how often to check for changes")
privkey = flag.String("privkey", "", "location of private key used for auth")
privkey = flag.String("privkey", "~/.ssh/id_rsa", "location of private key used for auth")
username = flag.String("username", "", "username used for auth")
password = flag.String("password", "", "password used for auth")
)

func gitAdd(w *git.Worktree, path string) error {
Expand Down Expand Up @@ -81,6 +85,22 @@ func gitHasRemote(r *git.Repository) bool {
return len(remotes) > 0
}

func parseAuthArgs() (transport.AuthMethod, error) {
if len(*username) > 0 {
return &http.BasicAuth{
Username: *username,
Password: *password,
}, nil
}

*privkey, _ = homedir.Expand(*privkey)
auth, err := ssh.NewPublicKeysFromFile("git", *privkey, "")
if err != nil {
return nil, err
}
return auth, nil
}

func main() {
fmt.Println("git-o-matic")
flag.Parse()
Expand All @@ -94,14 +114,9 @@ func main() {
if err != nil {
panic(err)
}

var auth transport.AuthMethod
if len(*privkey) > 0 {
var err error
auth, err = ssh.NewPublicKeysFromFile("git", *privkey, "")
if err != nil {
panic(err)
}
auth, err := parseAuthArgs()
if err != nil {
panic(err)
}

path := flag.Args()[0]
Expand Down

0 comments on commit e7cf208

Please sign in to comment.