96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/luciochen233/luphoto/internal/build"
|
|
"github.com/luciochen233/luphoto/internal/config"
|
|
)
|
|
|
|
const usage = `usage: luphoto [-config path] <command>
|
|
|
|
commands:
|
|
build process albums once and publish a new build
|
|
serve build, watch and serve over HTTP
|
|
export build into the output folder and keep watching
|
|
|
|
flags:
|
|
-config path config file (default /etc/luphoto/config.toml; a missing file means all defaults)
|
|
`
|
|
|
|
func main() {
|
|
os.Exit(run(os.Args[1:]))
|
|
}
|
|
|
|
func run(args []string) int {
|
|
logger := log.New(os.Stderr, "", 0)
|
|
global := flag.NewFlagSet("luphoto", flag.ContinueOnError)
|
|
global.SetOutput(os.Stderr)
|
|
global.Usage = func() { fmt.Fprint(os.Stderr, usage) }
|
|
configPath := global.String("config", config.DefaultPath, "config file")
|
|
if err := global.Parse(args); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
rest := global.Args()
|
|
if len(rest) == 0 {
|
|
fmt.Fprint(os.Stderr, usage)
|
|
return 2
|
|
}
|
|
command := rest[0]
|
|
sub := flag.NewFlagSet("luphoto "+command, flag.ContinueOnError)
|
|
sub.SetOutput(os.Stderr)
|
|
sub.Usage = global.Usage
|
|
sub.StringVar(configPath, "config", *configPath, "config file")
|
|
if err := sub.Parse(rest[1:]); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
if sub.NArg() > 0 {
|
|
fmt.Fprintf(os.Stderr, "luphoto %s: unexpected arguments %v\n", command, sub.Args())
|
|
return 2
|
|
}
|
|
switch command {
|
|
case "build", "serve", "export":
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "luphoto: unknown command %q\n\n%s", command, usage)
|
|
return 2
|
|
}
|
|
|
|
cfg, err := config.Load(*configPath)
|
|
if err != nil {
|
|
logger.Printf("luphoto: %v", err)
|
|
return 1
|
|
}
|
|
if cfg.Source == "" {
|
|
logger.Printf("luphoto: config %s not found, using defaults", *configPath)
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
summary, err := build.Run(ctx, cfg, logger)
|
|
if err != nil {
|
|
logger.Printf("luphoto: build failed: %v", err)
|
|
return 1
|
|
}
|
|
logger.Print(summary.String())
|
|
|
|
if command != "build" {
|
|
logger.Printf("luphoto %s: not implemented yet (arrives in milestone M6); built once, exiting", command)
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|