Skip to content

Commit e35113f

Browse files
committed
feat: add stderr and stdout to cron problem sensor attributes
1 parent 787cc57 commit e35113f

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

components/switches/cron.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func NewCron(cfg CronConfig) []*entity.Entity {
4141
Name(cfg.Name + " Successful").
4242
ID(cfg.UniqueID + "_successful").
4343
DeviceClass("problem").
44-
DefaultStateTopic().Build()
44+
DefaultStateTopic().
45+
DefaultAttributesTopic().Build()
4546
entities = append(entities, successEntity)
4647
}
4748

@@ -71,7 +72,6 @@ func NewCron(cfg CronConfig) []*entity.Entity {
7172
Type(entity.DomainSwitch).
7273
ObjectID(cfg.UniqueID).
7374
ScheduleJob(gocron.CronJob(cfg.Schedule, true), func(entity *entity.Entity, client mqtt.Client, scheduler gocron.Scheduler) error {
74-
log.Debug().Str("command", cfg.CommandConfig.Command).Msg("running command task")
7575
start := time.Now()
7676

7777
// run command
@@ -97,6 +97,14 @@ func NewCron(cfg CronConfig) []*entity.Entity {
9797
if err != nil {
9898
return err
9999
}
100+
err = successEntity.PublishAttributes(client, map[string]interface{}{
101+
"stdout": string(res.Stdout),
102+
"stderr": string(res.Stderr),
103+
"code": res.Code,
104+
})
105+
if err != nil {
106+
return err
107+
}
100108
}
101109
if err != nil {
102110
log.Err(err).Str("command", cfg.CommandConfig.Command).Msg("failed to run command")
@@ -108,6 +116,10 @@ func NewCron(cfg CronConfig) []*entity.Entity {
108116
if err != nil {
109117
return err
110118
}
119+
err = successEntity.PublishAttributes(client, map[string]string{})
120+
if err != nil {
121+
return err
122+
}
111123
}
112124
}
113125

utils/cmd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type CommandConfig struct {
2424
}
2525

2626
type CommandResult struct {
27-
Stdout []byte
28-
Stderr []byte
29-
Code int
27+
Stdout []byte `json:"stdout"`
28+
Stderr []byte `json:"stderr"`
29+
Code int `json:"code"`
3030
}
3131

3232
func GetDefaultShell() []string {
@@ -79,15 +79,15 @@ func renderTemplate(command string) (string, error) {
7979
func RunCommand(cfg CommandConfig) (CommandResult, error) {
8080
var args []string
8181

82-
log.Info().Str("command", cfg.Command).Str("shell", cfg.Shell).
82+
log.Debug().Str("command", cfg.Command).Str("shell", cfg.Shell).
8383
Interface("hidden", cfg.Hidden).Msg("running command")
8484

8585
command, err := renderTemplate(cfg.Command)
8686
if err != nil {
8787
return CommandResult{}, err
8888
}
8989

90-
log.Info().Str("command", command).Msg("templated command")
90+
log.Debug().Str("command", command).Msg("templated command")
9191

9292
if cfg.Shell == "none" {
9393
cmdArgs, err := shlex.Split(command)

0 commit comments

Comments
 (0)