Refactor error handling

This commit is contained in:
Tim Schubert 2023-11-18 15:53:50 +01:00 committed by Tim Schubert
parent 44807299b1
commit 2a1b7e8cdd
7 changed files with 108 additions and 80 deletions

22
args.go
View file

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

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"sync" "sync"
"time" "time"
@ -10,13 +11,16 @@ import (
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
) )
var cm sync.Mutex var (
cm sync.Mutex
nothingToDo = errors.New("Nothing to do for unmodified file")
)
func commit( func commit(
tree *git.Worktree, tree *git.Worktree,
padfile string, padfile string,
url string, url string,
) (plumbing.Hash, error) { ) (commit plumbing.Hash, err error) {
cm.Lock() cm.Lock()
defer cm.Unlock() defer cm.Unlock()
@ -34,7 +38,7 @@ func commit(
return plumbing.ZeroHash, nothingToDo return plumbing.ZeroHash, nothingToDo
} }
commit, err := tree.Commit( commit, err = tree.Commit(
fmt.Sprintf("Updated %s from %s", padfile, url), fmt.Sprintf("Updated %s from %s", padfile, url),
&git.CommitOptions{ &git.CommitOptions{
All: false, All: false,
@ -50,5 +54,5 @@ func commit(
return plumbing.ZeroHash, fmt.Errorf("Failed to commit %s: %w", padfile, err) return plumbing.ZeroHash, fmt.Errorf("Failed to commit %s: %w", padfile, err)
} }
return commit, nil return
} }

View file

@ -12,7 +12,7 @@ import (
func download( func download(
gitdir string, gitdir string,
url string, url string,
) (string, error) { ) (padfile string, err error) {
res, err := http.Get(url + "/export/txt") res, err := http.Get(url + "/export/txt")
if err != nil { if err != nil {
return "", fmt.Errorf("Failed to get pad at %s: %w", url, err) return "", fmt.Errorf("Failed to get pad at %s: %w", url, err)
@ -20,7 +20,7 @@ func download(
defer res.Body.Close() defer res.Body.Close()
padfile := path.Base(url) + ".txt" padfile = path.Base(url) + ".txt"
padpath := filepath.Join(gitdir, padfile) padpath := filepath.Join(gitdir, padfile)
out, err := os.Create(padpath) out, err := os.Create(padpath)
@ -34,5 +34,5 @@ func download(
return "", fmt.Errorf("Skipping update of %s, because pad has likely been removed from %s", padfile, url) return "", fmt.Errorf("Skipping update of %s, because pad has likely been removed from %s", padfile, url)
} }
return padfile, nil return
} }

73
main.go
View file

@ -3,77 +3,40 @@ package main
import ( import (
"bufio" "bufio"
"crypto/tls" "crypto/tls"
"errors"
"log" "log"
"net/http" "net/http"
"os" "os"
"sync"
"github.com/go-git/go-git/v5"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
)
const (
defaultRemoteName = "pad-archiver"
)
var (
nothingToDo = errors.New("Nothing to do for unmodified file")
) )
func main() { func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
gitdir, doPush, username, password, remoteUrl := getArgs() workingdir, err := os.Getwd()
repo, err := git.PlainOpen(*gitdir)
if err != nil { if err != nil {
log.Fatalf("Failed to get working directory %s", err)
}
gitdir, doPush, username, password, remoteUrl := getArgs(&workingdir)
repo, tree, err := openRepo(gitdir)
if err != nil {
if repo == nil {
log.Fatalf("Failed to open git repo %s: %s", *gitdir, err) log.Fatalf("Failed to open git repo %s: %s", *gitdir, err)
} }
if tree == nil {
tree, err := repo.Worktree()
if err != nil {
log.Fatalf("Failed to open git worktree %s", err) log.Fatalf("Failed to open git worktree %s", err)
} }
defer tree.Clean(&git.CleanOptions{Dir: true})
filesystemRoot := tree.Filesystem.Root()
scanner := bufio.NewScanner(os.Stdin)
var wg sync.WaitGroup
for scanner.Scan() {
wg.Add(1)
padurl := scanner.Text()
go func() {
defer wg.Done()
padfile, err := download(filesystemRoot, padurl)
if err != nil {
log.Printf("%s", err)
return
} }
log.Printf("Downloaded %s", padurl)
if _, err := commit(tree, padfile, padurl); err != nil {
if err == nothingToDo {
log.Printf("Nothing to do for %s", padfile)
} else {
log.Printf("%s", err)
}
} else {
log.Printf("Committed %s", padfile)
}
}()
}
wg.Wait()
auth := &githttp.BasicAuth{ padstxt := bufio.NewScanner(os.Stdin)
Username: *username,
Password: *password, updatePads(padstxt, tree)
}
auth := auth(username, password)
if *doPush == true { if *doPush == true {
pushRepo(repo, remoteUrl, auth) if err := pushRepo(repo, remoteUrl, auth); err != nil {
log.Fatalf("Failed to push repo: %s", err)
}
} }
} }

View file

@ -8,19 +8,31 @@ import (
githttp "github.com/go-git/go-git/v5/plumbing/transport/http" githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
) )
func pushRepo(repo *git.Repository, remoteUrl *string, auth *githttp.BasicAuth) { const (
if _, err := createRemote(repo, defaultRemoteName, *remoteUrl); err != nil { defaultRemoteName = "pad-archiver"
log.Fatalf("%s", err) )
func auth(username *string, password *string) (auth *githttp.BasicAuth) {
return &githttp.BasicAuth{
Username: *username,
Password: *password,
} }
if err := push(auth, repo, defaultRemoteName); err != nil { }
func pushRepo(repo *git.Repository, remoteUrl *string, auth *githttp.BasicAuth) (err error) {
if _, err = createRemote(repo, defaultRemoteName, *remoteUrl); err != nil {
return
}
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 {
log.Fatalf("%s", err) return
} }
} else { } else {
log.Println("Pushed changes to remote") log.Println("Pushed changes to remote")
} }
return
} }
func createRemote( func createRemote(

12
repo.go Normal file
View file

@ -0,0 +1,12 @@
package main
import "github.com/go-git/go-git/v5"
func openRepo(gitdir *string) (repo *git.Repository, tree *git.Worktree, err error) {
repo, err = git.PlainOpen(*gitdir)
if err != nil {
return
}
tree, err = repo.Worktree()
return
}

43
update.go Normal file
View file

@ -0,0 +1,43 @@
package main
import (
"bufio"
"log"
"sync"
"github.com/go-git/go-git/v5"
)
func updatePads(pads *bufio.Scanner, tree *git.Worktree) {
defer tree.Clean(&git.CleanOptions{Dir: true})
filesystemRoot := tree.Filesystem.Root()
var wg sync.WaitGroup
for pads.Scan() {
wg.Add(1)
padurl := pads.Text()
go func() {
defer wg.Done()
if _, err := updatePad(filesystemRoot, padurl, tree); err != nil {
log.Printf("%s", err)
}
}()
}
wg.Wait()
}
func updatePad(filesystemRoot string, padurl string, tree *git.Worktree) (padfile string, err error) {
padfile, err = download(filesystemRoot, padurl)
if err != nil {
return
}
log.Printf("Downloaded %s", padurl)
if _, err = commit(tree, padfile, padurl); err != nil {
return
} else {
log.Printf("Committed %s", padfile)
return
}
}