/
run.go
109 lines (101 loc) · 2.69 KB
/
run.go
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
106
107
108
109
package commands
import (
"os"
"path/filepath"
"reflect"
"runtime/pprof"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/site"
"github.com/osteele/gojekyll/version"
)
// ParseAndRun parses and executes the command-line arguments.
func ParseAndRun(args []string) error {
if reflect.DeepEqual(args, []string{"--version"}) {
return versionCommand()
}
cmd := kingpin.MustParse(app.Parse(args))
if options.Destination != nil {
dest, err := filepath.Abs(*options.Destination)
app.FatalIfError(err, "")
options.Destination = &dest
}
if options.DryRun {
verbose := true
options.Verbose = &verbose
}
return run(cmd)
}
func run(cmd string) error { // nolint: gocyclo
// dispatcher gets to ignore cyclo threshold ^
if profile || cmd == benchmark.FullCommand() {
defer setupProfiling()()
}
// These commands run *without* loading the site
switch cmd {
case benchmark.FullCommand():
return benchmarkCommand()
case pluginsApp.FullCommand():
return pluginsCommand()
case versionCmd.FullCommand():
return versionCommand()
}
site, err := loadSite(*source, options)
// Print the version at an awkward place, so its
// labels will line up. And print it even if
// loading the site produced an error.
if *versionFlag {
logger.label("Version:", version.Version)
}
if err != nil {
return err
}
// These commands run *after* the site is loaded
switch cmd {
case build.FullCommand():
return buildCommand(site)
case clean.FullCommand():
return cleanCommand(site)
case render.FullCommand():
return renderCommand(site)
case routes.FullCommand():
return routesCommand(site)
case serve.FullCommand():
return serveCommand(site)
case variables.FullCommand():
return variablesCommand(site)
default:
// kingpin should have provided help and exited before here
panic("exhaustive switch")
}
}
// Load the site, and print the common banner settings.
func loadSite(source string, flags config.Flags) (*site.Site, error) {
site, err := site.FromDirectory(source, flags)
if err != nil {
return nil, err
}
const configurationFileLabel = "Configuration file:"
if cf := site.Config().ConfigFile; cf != "" {
logger.path(configurationFileLabel, cf)
} else {
logger.label(configurationFileLabel, "none")
}
logger.path("Source:", site.SourceDir())
err = site.Read()
return site, err
}
func setupProfiling() func() {
profilePath := "gojekyll.prof"
logger.label("Profiling...", "")
f, err := os.Create(profilePath)
app.FatalIfError(err, "")
err = pprof.StartCPUProfile(f)
app.FatalIfError(err, "")
return func() {
pprof.StopCPUProfile()
err = f.Close()
app.FatalIfError(err, "")
logger.Info("Wrote", profilePath)
}
}