cmd/build: enable sqlite output
1 file changed, 43 insertions(+), 5 deletions(-)
jump to
M cmd/build/main.go → cmd/build/main.go
@@ -1,12 +1,15 @@ package main import ( + "database/sql" "fmt" "os" "go.alanpearce.eu/website/internal/builder" "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/website/internal/storage" "go.alanpearce.eu/website/internal/storage/files" + "go.alanpearce.eu/website/internal/storage/sqlite" "go.alanpearce.eu/website/internal/vcs" "go.alanpearce.eu/x/log"@@ -20,6 +23,7 @@ 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() {@@ -42,11 +46,45 @@ panic("could not open repository: " + err.Error()) } options.Repo = repo - storage, err := files.NewWriter(options.Destination, log.Named("storage"), &files.Options{ - Compress: options.Compress, - }) - if err != nil { - panic("could not create storage: " + err.Error()) + 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