parent
8deec15978
commit
1fbca6145d
1 changed files with 27 additions and 5 deletions
32
main.go
32
main.go
|
@ -16,13 +16,14 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"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"
|
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultRemoteName = "origin"
|
defaultRemoteName = "pad-archiver"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -101,14 +102,32 @@ func download(
|
||||||
return padfile, nil
|
return padfile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createRemote(
|
||||||
|
repo *git.Repository,
|
||||||
|
remote string,
|
||||||
|
url string,
|
||||||
|
) (*git.Remote, error) {
|
||||||
|
newRemote, err := repo.Remote(remote)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Creating new git remote %s with URL %s", remote, url)
|
||||||
|
return repo.CreateRemote(&config.RemoteConfig{
|
||||||
|
Name: remote,
|
||||||
|
URLs: []string{url},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
log.Printf("Using remote %s with URL %s", remote, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newRemote, nil
|
||||||
|
}
|
||||||
|
|
||||||
func push(
|
func push(
|
||||||
auth *githttp.BasicAuth,
|
auth *githttp.BasicAuth,
|
||||||
r *git.Repository,
|
r *git.Repository,
|
||||||
remote *string,
|
remote string,
|
||||||
) error {
|
) error {
|
||||||
return r.Push(&git.PushOptions{
|
return r.Push(&git.PushOptions{
|
||||||
RemoteName: *remote,
|
RemoteName: remote,
|
||||||
Auth: auth,
|
Auth: auth,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -141,7 +160,7 @@ func main() {
|
||||||
"",
|
"",
|
||||||
"password",
|
"password",
|
||||||
)
|
)
|
||||||
remote := flag.String(
|
remoteUrl := flag.String(
|
||||||
"remote",
|
"remote",
|
||||||
defaultRemoteName,
|
defaultRemoteName,
|
||||||
"remote",
|
"remote",
|
||||||
|
@ -197,7 +216,10 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if *doPush == true {
|
if *doPush == true {
|
||||||
if err := push(auth, repo, remote); err != nil {
|
if _, err := createRemote(repo, defaultRemoteName, *remoteUrl); err != nil {
|
||||||
|
log.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
if err := push(auth, repo, defaultRemoteName); err != nil {
|
||||||
if err == git.NoErrAlreadyUpToDate {
|
if err == git.NoErrAlreadyUpToDate {
|
||||||
log.Println("Already up-to-date")
|
log.Println("Already up-to-date")
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue