add functionality for pushing to remote
This commit is contained in:
parent
a09e188789
commit
e545d32aed
1 changed files with 61 additions and 2 deletions
63
main.go
63
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"crypto/tls"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -10,15 +11,18 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
|
||||||
"crypto/tls"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/config"
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
"github.com/go-git/go-git/v5/plumbing/object"
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
|
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const DefaultRemoteName = "pad-archiver"
|
||||||
|
|
||||||
var commitmu sync.Mutex
|
var commitmu sync.Mutex
|
||||||
|
|
||||||
func commit(
|
func commit(
|
||||||
|
@ -88,6 +92,29 @@ func update(
|
||||||
return commit(tree, padfile, url)
|
return commit(tree, padfile, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func PushChanges(
|
||||||
|
repo *git.Repository,
|
||||||
|
remote string,
|
||||||
|
auth githttp.AuthMethod,
|
||||||
|
) error {
|
||||||
|
if _, err := repo.Remote(DefaultRemoteName); err != nil {
|
||||||
|
log.Println("Creating new git remote " + DefaultRemoteName)
|
||||||
|
if _, err = repo.CreateRemote(&config.RemoteConfig{
|
||||||
|
Name: DefaultRemoteName,
|
||||||
|
URLs: []string{remote},
|
||||||
|
}); err != nil {
|
||||||
|
log.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo.Push(&git.PushOptions{
|
||||||
|
RemoteName: DefaultRemoteName,
|
||||||
|
Auth: auth,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
|
|
||||||
|
@ -101,6 +128,27 @@ func main() {
|
||||||
cwd,
|
cwd,
|
||||||
"git directory",
|
"git directory",
|
||||||
)
|
)
|
||||||
|
push := flag.Bool(
|
||||||
|
"push",
|
||||||
|
false,
|
||||||
|
"push repository to remote",
|
||||||
|
)
|
||||||
|
username := flag.String(
|
||||||
|
"username",
|
||||||
|
"",
|
||||||
|
"username",
|
||||||
|
)
|
||||||
|
password := flag.String(
|
||||||
|
"password",
|
||||||
|
"",
|
||||||
|
"password",
|
||||||
|
)
|
||||||
|
remote := flag.String(
|
||||||
|
"remote",
|
||||||
|
"",
|
||||||
|
"remote",
|
||||||
|
)
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
repo, err := git.PlainOpen(*gitdir)
|
repo, err := git.PlainOpen(*gitdir)
|
||||||
|
@ -132,4 +180,15 @@ func main() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
tree.Clean(&git.CleanOptions{})
|
tree.Clean(&git.CleanOptions{})
|
||||||
|
|
||||||
|
if *push == true {
|
||||||
|
auth := &githttp.BasicAuth{
|
||||||
|
Username: *username,
|
||||||
|
Password: *password,
|
||||||
|
}
|
||||||
|
if err := PushChanges(repo, *remote, auth); err != nil {
|
||||||
|
log.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
log.Println("Pushed changes to remote")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue