From 79bfbb96c0febf1ee9cc902ea6a1a8ab97621fa1 Mon Sep 17 00:00:00 2001 From: Catherine Date: Mon, 29 Sep 2025 01:16:59 +0000 Subject: [PATCH] Accept repository URL in `-update-site` CLI option. --- src/main.go | 56 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/main.go b/src/main.go index c7699f6..5b636c1 100644 --- a/src/main.go +++ b/src/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "flag" "fmt" "io" @@ -8,6 +9,7 @@ import ( "log/slog" "net" "net/http" + "net/url" "os" "runtime/debug" "strings" @@ -82,7 +84,7 @@ func main() { getBlob := flag.String("get-blob", "", "write `blob` ('sha256-xxxxxxx...xxx') to stdout") updateSite := flag.String("update-site", "", - "update site for `webroot` (either 'domain.tld' or 'domain.tld/dir') from archive") + "update site for `webroot` (either 'domain.tld' or 'domain.tld/dir') from archive or repository URL") flag.Parse() if *getManifest != "" && *getBlob != "" { @@ -168,9 +170,9 @@ func main() { log.Fatalln(err) } - filename := flag.Arg(0) - if filename == "" { - log.Fatalln("archive filename must be provided as an argument") + sourceURL, _ := url.Parse(flag.Arg(0)) + if sourceURL == (&url.URL{}) { + log.Fatalln("update source must be provided as an argument") } webRoot := *updateSite @@ -178,26 +180,36 @@ func main() { webRoot += "/.index" } - file, err := os.Open(filename) - if err != nil { - log.Fatalln(err) + var result UpdateResult + if sourceURL.Scheme == "" { + file, err := os.Open(sourceURL.Path) + if err != nil { + log.Fatalln(err) + } + + var contentType string + switch { + case strings.HasSuffix(sourceURL.Path, ".zip"): + contentType = "application/zip" + case strings.HasSuffix(sourceURL.Path, ".tar"): + contentType = "application/x-tar" + case strings.HasSuffix(sourceURL.Path, ".tar.gz"): + contentType = "application/x-tar+gzip" + case strings.HasSuffix(sourceURL.Path, ".tar.zst"): + contentType = "application/x-tar+zstd" + default: + log.Fatalf("cannot determine content type from filename %q\n", sourceURL) + } + + result = UpdateFromArchive(webRoot, contentType, file) + } else { + branch := "pages" + if sourceURL.Fragment != "" { + branch, sourceURL.Fragment = sourceURL.Fragment, "" + } + result = UpdateFromRepository(context.Background(), webRoot, sourceURL.String(), branch) } - var contentType string - switch { - case strings.HasSuffix(filename, ".zip"): - contentType = "application/zip" - case strings.HasSuffix(filename, ".tar"): - contentType = "application/x-tar" - case strings.HasSuffix(filename, ".tar.gz"): - contentType = "application/x-tar+gzip" - case strings.HasSuffix(filename, ".tar.zst"): - contentType = "application/x-tar+zstd" - default: - log.Fatalf("cannot determine content type from filename %q\n", filename) - } - - result := UpdateFromArchive(webRoot, contentType, file) switch result.outcome { case UpdateError: log.Printf("error: %s\n", result.err)