func prepareBuild(args cliArgs, config caddybuild.Config) (App, error) { caddybuild.SetConfig(config) var builder custombuild.Builder var f *os.File var caddyProcess *os.Process // remove temp files. addCleanUpFunc(func() { if caddyProcess != nil { caddyProcess.Kill() } builder.Teardown() if f != nil { os.Remove(f.Name()) } }) builder, err := caddybuild.PrepareBuild(config.Plugins, false) exitIfErr(err) // if output is set, generate binary only. if args.output != "" { err := saveCaddy(builder, args.output, args.goArgs) exitIfErr(err) os.Exit(0) } // create temporary file for binary f, err = ioutil.TempFile("", "caddydev") exitIfErr(err) f.Close() // fix for windows exe fName := f.Name() if runtime.GOOS == "windows" { fName += ".exe" } return &caddyApp{ file: fName, goArgs: args.goArgs, caddyArgs: args.caddyArgs, builder: builder, }, nil }
func main() { // parse cli arguments. args, err := parseArgs() exitIfErr(err) // read config file. config, err := readConfig(args) exitIfErr(err) fetched := false // if caddy source does not exits, pull source. if !isLocalPkg(caddybuild.CaddyPackage) { fmt.Print("Caddy source not found. Fetching...") err := fetchCaddy() exitIfErr(err) fmt.Println(" done.") fetched = true } // if update flag is set, pull source. if args.update && !fetched { fmt.Print("Updating caddy source...") err := fetchCaddy() exitIfErr(err) fmt.Println(" done.") } caddybuild.SetConfig(config) var builder custombuild.Builder var f *os.File var caddyProcess *os.Process // remove temp files. addCleanUpFunc(func() { if caddyProcess != nil { caddyProcess.Kill() } builder.Teardown() if f != nil { os.Remove(f.Name()) } }) builder, err = caddybuild.PrepareBuild(features.Middlewares{config.Middleware}) exitIfErr(err) // if output is set, generate binary only. if args.output != "" { err := saveCaddy(builder, args.output, args.goArgs) exitIfErr(err) return } // create temporary file for binary f, err = ioutil.TempFile("", "caddydev") exitIfErr(err) f.Close() // perform custom build err = builder.Build("", "", f.Name(), args.goArgs...) exitIfErr(err) fmt.Println("Starting caddy...") // trap os interrupts to ensure cleaning up temp files. done := trapInterrupts() // start custom caddy. go func() { cmd, err := startCaddy(f.Name(), args.caddyArgs) exitIfErr(err) caddyProcess = cmd.Process err = cmd.Wait() cleanUp() exitIfErr(err) done <- struct{}{} }() // wait for exit signal <-done }