mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-18 05:02:15 +01:00
feat(conf): override flags' values with their corresponding environment variables' values
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -50,14 +51,42 @@ func Run() {
|
|||||||
|
|
||||||
fmt.Printf("[SYSTEM] modules: %s\n", modsInfo)
|
fmt.Printf("[SYSTEM] modules: %s\n", modsInfo)
|
||||||
|
|
||||||
// Parse the flags...
|
// Parse the flags.
|
||||||
err := fs.Parse(os.Args[1:])
|
err := fs.Parse(os.Args[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...and create a wrapper around those.
|
// Override their values if the corresponding environment variables are
|
||||||
|
// set.
|
||||||
|
fs.VisitAll(func(f *flag.Flag) {
|
||||||
|
envName := strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
|
||||||
|
val, ok := os.LookupEnv(envName)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sliceVal, ok := f.Value.(flag.SliceValue)
|
||||||
|
if ok {
|
||||||
|
// We don't want to append the values (default pflag behavior).
|
||||||
|
items := strings.Split(val, ",")
|
||||||
|
err = sliceVal.Replace(items)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("[FATAL] invalid overriding value '%s' from %s: %v\n", val, envName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = f.Value.Set(val)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("[FATAL] invalid overriding value '%s' from %s: %v\n", val, envName, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Create a wrapper around our flags.
|
||||||
parsedFlags := gotenberg.ParsedFlags{FlagSet: fs}
|
parsedFlags := gotenberg.ParsedFlags{FlagSet: fs}
|
||||||
|
|
||||||
// Get the graceful shutdown duration.
|
// Get the graceful shutdown duration.
|
||||||
|
|||||||
Reference in New Issue
Block a user