From 84106842bf4cd9143784141c3fd73f651bc48463 Mon Sep 17 00:00:00 2001 From: James Mills Date: Sat, 18 Mar 2023 13:06:33 +1000 Subject: [PATCH] Add support for -p/--production global variable --- main.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index a6cd21f..c0c7dd4 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,11 @@ README.md` // Ignore holds a set of patterns to ignore from parsing a .zsignore file var Ignore *ignore.GitIgnore +var ( + // Production is a global variable to conditionally build for production + Production = false +) + // Vars holds a map of global variables type Vars map[string]string @@ -236,6 +241,11 @@ func renameExt(path, oldext, newext string) string { // with ZS_ prefix as Vars, so the values can be used inside templates func globals() Vars { vars := Vars{} + + if Production { + vars["production"] = "1" + } + for _, e := range os.Environ() { pair := strings.Split(e, "=") if strings.HasPrefix(pair[0], "ZS_") { @@ -578,13 +588,11 @@ func ensureFirstPath(p string) { } func init() { - RootCmd.PersistentFlags().BoolP( - "debug", "d", false, - "Enable debug logging", - ) + RootCmd.PersistentFlags().BoolP("debug", "d", false, "enable debug logging $($ZS_DEBUG)") + RootCmd.PersistentFlags().BoolVarP(&Production, "production", "p", false, "enable production mode ($ZS_PRODUCTION)") - ServeCmd.Flags().StringP("bind", "b", ":8000", "Set the [
]: to listen on") - ServeCmd.Flags().StringP("root", "r", PUBDIR, "Set the root directory to serve") + ServeCmd.Flags().StringP("bind", "b", ":8000", "set the [
]: to listen on") + ServeCmd.Flags().StringP("root", "r", PUBDIR, "set the root directory to serve") RootCmd.AddCommand(BuildCmd) RootCmd.AddCommand(ServeCmd)