1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
package main
import (
"crypto/sha1"
_ "embed"
"encoding/hex"
"flag"
"io"
"log"
"os"
"path/filepath"
"text/template"
)
const configFile = ".ht_git2html"
//go:embed config.tmpl
var tmpl string
func main() {
var p string
var r string
var l string
var b string
var q bool
var f bool
flag.StringVar(&p, "p", "My project", "Choose a project name")
flag.StringVar(&r, "r", "/path/to/repo", "Repository to clone from")
flag.StringVar(&l, "l", "http://host.org/project.git", "Public link to repo")
flag.StringVar(&b, "b", "all", "List of branches")
flag.BoolVar(&q, "q", false, "Be quiet")
flag.BoolVar(&f, "f", false, "Force rebuilding of all pages")
flag.Parse()
log.Printf("%v %v %v %v %v %v", p, r, l, b, q, f)
args := os.Args
if len(args) != 2 {
log.Fatalf("jimmy: please specify a single target path")
}
targetDir := args[1]
if ok := filepath.IsAbs(targetDir); !ok {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("jimmy: %v", err)
}
targetDir = filepath.Join(cwd, targetDir)
}
// TODO: Look up more mode for 755 or 644.
if err := os.MkdirAll(targetDir, os.ModePerm); err != nil {
log.Fatalf("jimmy: unable to create target directory: %v", err)
}
configTmpl := template.Must(template.New("default").Parse(tmpl))
// TODO: Check file permissions are set to 0666.
// TODO: Read file if it exists.
outFile, err := os.Create(filepath.Join(targetDir, configFile))
if err != nil {
log.Fatalf("jimmy: unable to create config file: %v", err)
}
h := sha1.New()
if _, err := io.Copy(h, outFile); err != nil {
log.Fatal(err)
}
configTmpl.Execute(outFile, struct {
Project string
Repository string
PublicRepository string
Target string
Branches string
// SHA1SUM
Template string
}{
Project: p,
Repository: r,
PublicRepository: l,
Target: targetDir,
Branches: b,
Template: hex.EncodeToString(h.Sum(nil)),
})
}
|