From 6a43c1837e14869ac624d4c347524990751c2150 Mon Sep 17 00:00:00 2001 From: Evan Van Dam Date: Tue, 5 Sep 2023 22:48:01 -0700 Subject: [PATCH] add error handling for StringSlice --- cmd/commandfuncs.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index a329c8252752..4e27b48857f9 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -156,16 +156,23 @@ func cmdStart(fl Flags) (int, error) { func cmdRun(fl Flags) (int, error) { caddy.TrapSignals() + var err error runCmdConfigFlag := fl.String("config") runCmdConfigAdapterFlag := fl.String("adapter") runCmdResumeFlag := fl.Bool("resume") - runCmdLoadEnvfileFlag, _ := fl.GetStringSlice("envfile") runCmdPrintEnvFlag := fl.Bool("environ") runCmdWatchFlag := fl.Bool("watch") runCmdPidfileFlag := fl.String("pidfile") runCmdPingbackFlag := fl.String("pingback") + var runCmdLoadEnvfileFlag []string + runCmdLoadEnvfileFlag, err = fl.GetStringSlice("envfile") + if err != nil { + return caddy.ExitCodeFailedStartup, + fmt.Errorf("reading envfile flag: %v", err) + } + // load all additional envs as soon as possible for _, envFile := range runCmdLoadEnvfileFlag { if err := loadEnvFromFile(envFile); err != nil { @@ -181,7 +188,6 @@ func cmdRun(fl Flags) (int, error) { // load the config, depending on flags var config []byte - var err error if runCmdResumeFlag { config, err = os.ReadFile(caddy.ConfigAutosavePath) if os.IsNotExist(err) { @@ -495,9 +501,16 @@ func cmdAdaptConfig(fl Flags) (int, error) { } func cmdValidateConfig(fl Flags) (int, error) { + var err error validateCmdConfigFlag := fl.String("config") validateCmdAdapterFlag := fl.String("adapter") - runCmdLoadEnvfileFlag, _ := fl.GetStringSlice("envfile") + + var runCmdLoadEnvfileFlag []string + runCmdLoadEnvfileFlag, err = fl.GetStringSlice("envfile") + if err != nil { + return caddy.ExitCodeFailedStartup, + fmt.Errorf("reading envfile flag: %v", err) + } // load all additional envs as soon as possible for _, envFile := range runCmdLoadEnvfileFlag { @@ -508,7 +521,6 @@ func cmdValidateConfig(fl Flags) (int, error) { } // use default config and ensure a config file is specified - var err error validateCmdConfigFlag, err = configFileWithRespectToDefault(caddy.Log(), validateCmdConfigFlag) if err != nil { return caddy.ExitCodeFailedStartup, err