package main
import (
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
"alin.ovh/homestead/domain/content/builder"
"alin.ovh/homestead/shared/config"
"alin.ovh/homestead/shared/file"
"alin.ovh/homestead/shared/storage"
"alin.ovh/homestead/shared/storage/files"
"alin.ovh/homestead/shared/storage/sqlite"
"alin.ovh/homestead/shared/vcs"
"alin.ovh/x/log"
"github.com/ardanlabs/conf/v3"
)
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)"`
}
const branch = "main"
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 = file.Stat(options.Destination)
if err != nil && !errors.Is(err, os.ErrNotExist) {
panic("could not stat destination: " + err.Error())
}
switch {
case stat == nil:
err := os.MkdirAll(options.Destination, 0o750)
if err != nil {
panic("could not make directory: " + err.Error())
}
case !stat.IsDir():
panic("destination is not a directory")
default:
err := file.CleanDir(options.Destination)
if err != nil {
panic("could not clean destination: " + err.Error())
}
}
var db *sql.DB
db, err = sqlite.OpenDB(filepath.Join(options.Destination, "site.db"))
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())
}
err = file.Copy(
filepath.Join(options.Source, "config.toml"),
filepath.Join(options.Destination, "config.toml"),
)
if err != nil {
panic("could not copy configuration: " + 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)
}
err = builder.BuildSite(options.Options, cfg, log)
if err != nil {
panic("could not build site: " + err.Error())
}
}
domain/content/builder/build/main.go (view raw)