refactor: extract getWriter helper to deduplicate writer init

- Add getWriter method to centralize writer-nil-check logic
- Replace inline writer initialization in log and Exec methods

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bo-Yi Wu
2026-04-08 23:05:25 +08:00
parent c7548576f0
commit c20e983535
+9 -8
View File
@@ -181,12 +181,16 @@ func (p Plugin) format(format string, args ...string) string {
return r.Replace(format)
}
func (p Plugin) getWriter() io.Writer {
if p.Writer != nil {
return p.Writer
}
return os.Stdout
}
// log output to console
func (p Plugin) log(host string, message ...any) {
w := p.Writer
if w == nil {
w = os.Stdout
}
w := p.getWriter()
if count := len(p.Config.Host); count == 1 {
fmt.Fprintf(w, "%s", fmt.Sprintln(message...))
return
@@ -240,10 +244,7 @@ func (p Plugin) Exec() error {
}
}
w := p.Writer
if w == nil {
w = os.Stdout
}
w := p.getWriter()
fmt.Fprintln(w, "===============================================")
fmt.Fprintln(w, "✅ Successfully executed commands to all hosts.")
fmt.Fprintln(w, "===============================================")