Merge branch 'main' into dependabot/github_actions/docker/login-action-3

This commit is contained in:
Tim Schubert 2023-11-18 15:06:38 +01:00 committed by GitHub
commit dedfc3d142
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 199 additions and 162 deletions

44
args.go Normal file
View file

@ -0,0 +1,44 @@
package main
import (
"flag"
"log"
"os"
)
func getArgs() (*string, *bool, *string, *string, *string) {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get working directory %s", err)
}
gitdir := flag.String(
"C",
cwd,
"The directory containing the git repository in which to archive the pads.",
)
doPush := flag.Bool(
"push",
false,
"Push the changes to the remote specified by remoteUrl.",
)
username := flag.String(
"username",
"",
"The username for authenticating to the remote.",
)
password := flag.String(
"password",
os.Getenv("GIT_PASSWORD"),
"The password for authenticating to the remote. Can also be specified via the environment variable GIT_PASSWORD.",
)
remoteUrl := flag.String(
"url",
"",
"URL to push changes to.",
)
flag.Parse()
return gitdir, doPush, username, password, remoteUrl
}

54
commit.go Normal file
View file

@ -0,0 +1,54 @@
package main
import (
"fmt"
"sync"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
var cm sync.Mutex
func commit(
tree *git.Worktree,
padfile string,
url string,
) (plumbing.Hash, error) {
cm.Lock()
defer cm.Unlock()
if _, err := tree.Add(padfile); err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to stage %s: %w", padfile, err)
}
status, err := tree.Status()
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to get status of %s", padfile)
}
fileStatus := status.File(padfile)
if fileStatus.Staging != git.Added && fileStatus.Staging != git.Modified {
return plumbing.ZeroHash, nothingToDo
}
commit, err := tree.Commit(
fmt.Sprintf("Updated %s from %s", padfile, url),
&git.CommitOptions{
All: false,
Author: &object.Signature{
Name: "Pad Archiver[bot]",
Email: "dadada+pad-archiver@dadada.li@",
When: time.Now(),
},
},
)
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to commit %s: %w", padfile, err)
}
return commit, nil
}

38
download.go Normal file
View file

@ -0,0 +1,38 @@
package main
import (
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
)
func download(
gitdir string,
url string,
) (string, error) {
res, err := http.Get(url + "/export/txt")
if err != nil {
return "", fmt.Errorf("Failed to get pad at %s: %w", url, err)
}
defer res.Body.Close()
padfile := path.Base(url) + ".txt"
padpath := filepath.Join(gitdir, padfile)
out, err := os.Create(padpath)
written, err := io.Copy(out, res.Body)
if err != nil {
return "", fmt.Errorf("Failed to write pad to file at %s: %w", padfile, err)
}
if written < 100 {
return "", fmt.Errorf("Skipping update of %s, because pad has likely been removed from %s", padfile, url)
}
return padfile, nil
}

14
flake.lock generated
View file

@ -5,11 +5,11 @@
"systems": "systems" "systems": "systems"
}, },
"locked": { "locked": {
"lastModified": 1681202837, "lastModified": 1694529238,
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "cfacdce06f30d2b68473a46042957675eebb3401", "rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -20,10 +20,10 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1681041438, "lastModified": 1699291058,
"narHash": "sha256-NmRGMklxBZ8Ol47CKMQxAU1F+v8ySpsHAAiC7ZL4vxY=", "narHash": "sha256-5ggduoaAMPHUy4riL+OrlAZE14Kh7JWX4oLEs22ZqfU=",
"path": "/nix/store/bbi5840s1wrrg0lf7b63vddfppjbqpyg-source", "path": "/nix/store/3s69yxbbl116zwga3i6cy7prplywq0bn-source",
"rev": "48dcbaf7fa799509cbec85d55b8d62dcf1477d57", "rev": "41de143fda10e33be0f47eab2bfe08a50f234267",
"type": "path" "type": "path"
}, },
"original": { "original": {

View file

@ -31,7 +31,6 @@
gopls gopls
gotools gotools
go-tools go-tools
gopls
]; ];
}; };
} }

156
main.go
View file

@ -4,21 +4,12 @@ import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"errors" "errors"
"flag"
"fmt"
"io"
"log" "log"
"net/http" "net/http"
"os" "os"
"path"
"path/filepath"
"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/object"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http" githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
) )
@ -30,142 +21,10 @@ var (
nothingToDo = errors.New("Nothing to do for unmodified file") nothingToDo = errors.New("Nothing to do for unmodified file")
) )
var cm sync.Mutex
func commit(
tree *git.Worktree,
padfile string,
url string,
) (plumbing.Hash, error) {
cm.Lock()
defer cm.Unlock()
if _, err := tree.Add(padfile); err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to stage %s: %w", padfile, err)
}
status, err := tree.Status()
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to get status of %s", padfile)
}
fileStatus := status.File(padfile)
if fileStatus.Staging != git.Added && fileStatus.Staging != git.Modified {
return plumbing.ZeroHash, nothingToDo
}
commit, err := tree.Commit(
fmt.Sprintf("Updated %s from %s", padfile, url),
&git.CommitOptions{
All: false,
Author: &object.Signature{
Name: "Pad Archiver[bot]",
Email: "dadada+pad-archiver@dadada.li@",
When: time.Now(),
},
},
)
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("Failed to commit %s: %w", padfile, err)
}
return commit, nil
}
func download(
gitdir string,
url string,
) (string, error) {
res, err := http.Get(url + "/export/txt")
if err != nil {
return "", fmt.Errorf("Failed to get pad at %s: %w", url, err)
}
defer res.Body.Close()
padfile := path.Base(url) + ".txt"
padpath := filepath.Join(gitdir, padfile)
out, err := os.Create(padpath)
written, err := io.Copy(out, res.Body)
if err != nil {
return "", fmt.Errorf("Failed to write pad to file at %s: %w", padfile, err)
}
if written < 100 {
return "", fmt.Errorf("Skipping update of %s, because pad has likely been removed from %s", padfile, url)
}
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(
auth *githttp.BasicAuth,
r *git.Repository,
remote string,
) error {
return r.Push(&git.PushOptions{
RemoteName: remote,
Auth: auth,
})
}
func main() { func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
cwd, err := os.Getwd() gitdir, doPush, username, password, remoteUrl := getArgs()
if err != nil {
log.Fatalf("Failed to get working directory %s", err)
}
gitdir := flag.String(
"C",
cwd,
"The directory containing the git repository in which to archive the pads.",
)
doPush := flag.Bool(
"push",
false,
"Push the changes to the remote specified by remoteUrl.",
)
username := flag.String(
"username",
"",
"The username for authenticating to the remote.",
)
password := flag.String(
"password",
os.Getenv("GIT_PASSWORD"),
"The password for authenticating to the remote. Can also be specified via the environment variable GIT_PASSWORD.",
)
remoteUrl := flag.String(
"url",
"",
"URL to push changes to.",
)
flag.Parse()
repo, err := git.PlainOpen(*gitdir) repo, err := git.PlainOpen(*gitdir)
if err != nil { if err != nil {
@ -215,17 +74,6 @@ func main() {
} }
if *doPush == true { if *doPush == true {
if _, err := createRemote(repo, defaultRemoteName, *remoteUrl); err != nil { pushRepo(repo, remoteUrl, auth)
log.Fatalf("%s", err)
}
if err := push(auth, repo, defaultRemoteName); err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Println("Already up-to-date")
} else {
log.Fatalf("%s", err)
}
} else {
log.Println("Pushed changes to remote")
}
} }
} }

54
remote.go Normal file
View file

@ -0,0 +1,54 @@
package main
import (
"log"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
)
func pushRepo(repo *git.Repository, remoteUrl *string, auth *githttp.BasicAuth) {
if _, err := createRemote(repo, defaultRemoteName, *remoteUrl); err != nil {
log.Fatalf("%s", err)
}
if err := push(auth, repo, defaultRemoteName); err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Println("Already up-to-date")
} else {
log.Fatalf("%s", err)
}
} else {
log.Println("Pushed changes to remote")
}
}
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(
auth *githttp.BasicAuth,
r *git.Repository,
remote string,
) error {
return r.Push(&git.PushOptions{
RemoteName: remote,
Auth: auth,
})
}