about summary refs log tree commit diff stats
path: root/cmd/build/main.go
blob: 069f9bd1b3317fc3e5a845cab700dd5578090db2 (plain)
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
package main

import (
	"fmt"
	"io/fs"
	"log"
	"log/slog"
	"os"

	"website/internal/builder"

	"github.com/BurntSushi/toml"
	"github.com/ardanlabs/conf/v3"
	"github.com/pkg/errors"
)

func main() {
	if os.Getenv("DEBUG") != "" {
		slog.SetLogLoggerLevel(slog.LevelDebug)
	}
	log.SetFlags(log.LstdFlags | log.Lmsgprefix)
	log.SetPrefix("build: ")
	slog.Debug("starting build process")

	ioConfig := builder.IOConfig{}
	if help, err := conf.Parse("", &ioConfig); err != nil {
		if errors.Is(err, conf.ErrHelpWanted) {
			fmt.Println(help)
			os.Exit(1)
		}
		log.Panicf("parsing I/O configuration: %v", err)
	}

	if ioConfig.Source != "." {
		err := os.Chdir(ioConfig.Source)
		if err != nil {
			log.Panic("could not change to source directory")
		}
	}

	if err := builder.BuildSite(ioConfig); err != nil {
		switch cause := errors.Cause(err).(type) {
		case *fs.PathError:
			slog.Info("pathError")
			slog.Error(fmt.Sprintf("%s", err))
		case toml.ParseError:
			slog.Info("parseError")
			slog.Error(fmt.Sprintf("%s", err))
		default:
			slog.Info("other")
			slog.Error(fmt.Sprintf("cause:%+v", errors.Cause(cause)))
			slog.Error(fmt.Sprintf("%+v", cause))
		}
		os.Exit(1)
	}
}