Skip to content

Commit

Permalink
fix(script): fix problem of reading empty script name
Browse files Browse the repository at this point in the history
  • Loading branch information
windvalley committed Jul 1, 2024
1 parent bf687ea commit 92f92f3
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions internal/cmd/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ THE SOFTWARE.
package cmd

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -60,23 +61,28 @@ Execute a local shell script on target hosts.`,
util.PrintErrExit(errs)
}

if scriptFile != "" && !util.FileExists(scriptFile) {
if scriptFile == "" {
return
}

if !util.FileExists(scriptFile) {
util.PrintErrExit(fmt.Sprintf("script '%s' not found", scriptFile))
}

if noSafeCheck {
log.Debugf("Skip the safety check of commands before execution")
return
}

if len(configflags.Config.Run.CommandBlacklist) == 0 {
log.Debugf("Using default command blacklist for the safety check: %s", defaultCommandBlacklist)
configflags.Config.Run.CommandBlacklist = defaultCommandBlacklist
} else {
if len(configflags.Config.Run.CommandBlacklist) == 0 {
configflags.Config.Run.CommandBlacklist = defaultCommandBlacklist
log.Debugf("Using default command blacklist for the safety check: %s", defaultCommandBlacklist)
} else {
log.Debugf("Using custom command blacklist for the safety check: %s", configflags.Config.Run.CommandBlacklist)
}

if err := checkScript(scriptFile, configflags.Config.Run.CommandBlacklist); err != nil {
util.PrintErrExit(err)
}
log.Debugf("Using custom command blacklist for the safety check: %s", configflags.Config.Run.CommandBlacklist)
}

if err := checkScript(scriptFile, configflags.Config.Run.CommandBlacklist); err != nil {
util.PrintErrExit(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -119,6 +125,10 @@ func init() {
}

func checkScript(scriptFile string, commandBlacklist []string) error {
if scriptFile == "" {
return errors.New("empty script name")
}

script, err := os.ReadFile(scriptFile)
if err != nil {
return fmt.Errorf("read script file '%s' failed: %w", scriptFile, err)
Expand Down

0 comments on commit 92f92f3

Please sign in to comment.