cmd/build/main.go (view raw)
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 94 95 96 97 98 99 100 101 102 103 104 105 | package main
import (
"database/sql"
"fmt"
"os"
"go.alanpearce.eu/homestead/internal/builder"
"go.alanpearce.eu/homestead/internal/config"
"go.alanpearce.eu/homestead/internal/storage"
"go.alanpearce.eu/homestead/internal/storage/files"
"go.alanpearce.eu/homestead/internal/storage/sqlite"
"go.alanpearce.eu/homestead/internal/vcs"
"go.alanpearce.eu/x/log"
"github.com/ardanlabs/conf/v3"
"gitlab.com/tozd/go/errors"
)
const branch = "main"
type Options struct {
*builder.Options
Destination string `conf:"default:./public,short:d,flag:dest"`
Compress bool `conf:"default:true"`
Writer string `conf:"default:files,help:Output type (files|sqlite)"`
}
func main() {
options := &Options{}
if help, err := conf.Parse("", options); err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
os.Exit(1)
}
panic("error parsing configuration: " + err.Error())
}
log := log.Configure(!options.Development)
repo, _, err := vcs.CloneOrOpen(&vcs.Options{
LocalPath: options.Source,
RemoteURL: options.VCSRemoteURL,
Branch: branch,
}, log.Named("vcs"))
if err != nil {
panic("could not open repository: " + err.Error())
}
options.Repo = repo
var storage storage.Writer
switch options.Writer {
case "files":
storage, err = files.NewWriter(options.Destination, log.Named("storage"), &files.Options{
Compress: options.Compress,
})
if err != nil {
panic("could not create storage: " + err.Error())
}
case "sqlite":
var stat os.FileInfo
stat, err = os.Stat(options.Destination)
if err != nil && !errors.Is(err, os.ErrNotExist) {
panic("could not stat destination: " + err.Error())
}
if stat != nil {
if stat.IsDir() {
panic("destination is a directory")
}
err = os.Remove(options.Destination)
if err != nil {
panic("could not remove destination: " + err.Error())
}
}
var db *sql.DB
db, err = sqlite.OpenDB(options.Destination)
if err != nil {
panic("could not open database: " + err.Error())
}
defer db.Close()
storage, err = sqlite.NewWriter(db, log.Named("storage"), &sqlite.Options{
Compress: options.Compress,
})
if err != nil {
panic("could not create storage: " + err.Error())
}
default:
panic("unknown storage type: " + options.Writer)
}
options.Storage = storage
log.Debug("starting build process")
cfg, err := config.GetConfig(options.Source, log)
if err != nil {
log.Error("could not read config", "error", err)
}
r, err := builder.BuildSite(options.Options, cfg, log)
if err != nil {
panic("could not build site: " + err.Error())
}
for _, w := range r.Hashes {
fmt.Printf("\"'%s'\"\n", w)
}
}
|