Add support for -p/--production global variable

This commit is contained in:
James Mills 2023-03-18 13:06:33 +10:00
parent 8481779221
commit 84106842bf
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6

20
main.go
View File

@ -52,6 +52,11 @@ README.md`
// Ignore holds a set of patterns to ignore from parsing a .zsignore file // Ignore holds a set of patterns to ignore from parsing a .zsignore file
var Ignore *ignore.GitIgnore var Ignore *ignore.GitIgnore
var (
// Production is a global variable to conditionally build for production
Production = false
)
// Vars holds a map of global variables // Vars holds a map of global variables
type Vars map[string]string 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 // with ZS_ prefix as Vars, so the values can be used inside templates
func globals() Vars { func globals() Vars {
vars := Vars{} vars := Vars{}
if Production {
vars["production"] = "1"
}
for _, e := range os.Environ() { for _, e := range os.Environ() {
pair := strings.Split(e, "=") pair := strings.Split(e, "=")
if strings.HasPrefix(pair[0], "ZS_") { if strings.HasPrefix(pair[0], "ZS_") {
@ -578,13 +588,11 @@ func ensureFirstPath(p string) {
} }
func init() { func init() {
RootCmd.PersistentFlags().BoolP( RootCmd.PersistentFlags().BoolP("debug", "d", false, "enable debug logging $($ZS_DEBUG)")
"debug", "d", false, RootCmd.PersistentFlags().BoolVarP(&Production, "production", "p", false, "enable production mode ($ZS_PRODUCTION)")
"Enable debug logging",
)
ServeCmd.Flags().StringP("bind", "b", ":8000", "Set the [<address>]:<port> to listen on") ServeCmd.Flags().StringP("bind", "b", ":8000", "set the [<address>]:<port> to listen on")
ServeCmd.Flags().StringP("root", "r", PUBDIR, "Set the root directory to serve") ServeCmd.Flags().StringP("root", "r", PUBDIR, "set the root directory to serve")
RootCmd.AddCommand(BuildCmd) RootCmd.AddCommand(BuildCmd)
RootCmd.AddCommand(ServeCmd) RootCmd.AddCommand(ServeCmd)