From: Simon Ruderich Date: Tue, 20 Apr 2021 09:21:10 +0000 (+0200) Subject: safcm: use Command struct instead of string to run commands X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=1934f1ab83306dff45310d5b45e2a99c97e28c35;p=safcm%2Fsafcm.git safcm: use Command struct instead of string to run commands --- diff --git a/cmd/safcm-remote/sync/commands.go b/cmd/safcm-remote/sync/commands.go index e3c48dd..84a318d 100644 --- a/cmd/safcm-remote/sync/commands.go +++ b/cmd/safcm-remote/sync/commands.go @@ -40,7 +40,7 @@ func (s *Sync) syncCommands() error { // Regular commands afterwards so they can react on triggers if // necessary for _, x := range s.req.Commands { - err := s.syncCommand(x, "") + err := s.syncCommand(x.Cmd, "") if err != nil { return err } diff --git a/cmd/safcm-remote/sync/commands_test.go b/cmd/safcm-remote/sync/commands_test.go index 142a254..ba7f4a2 100644 --- a/cmd/safcm-remote/sync/commands_test.go +++ b/cmd/safcm-remote/sync/commands_test.go @@ -65,8 +65,10 @@ func TestSyncCommands(t *testing.T) { "group2", "host.example.org", }, - Commands: []string{ - "echo; env | grep SAFCM_", + Commands: []*safcm.Command{ + { + Cmd: "echo; env | grep SAFCM_", + }, }, }, nil, @@ -105,8 +107,10 @@ func TestSyncCommands(t *testing.T) { "group2", "host.example.org", }, - Commands: []string{ - "echo; env | grep SAFCM_", + Commands: []*safcm.Command{ + { + Cmd: "echo; env | grep SAFCM_", + }, }, }, nil, @@ -134,8 +138,10 @@ func TestSyncCommands(t *testing.T) { "group2", "host.example.org", }, - Commands: []string{ - "echo hi; false", + Commands: []*safcm.Command{ + { + Cmd: "echo hi; false", + }, }, }, nil, @@ -175,8 +181,10 @@ func TestSyncCommands(t *testing.T) { "group2", "host.example.org", }, - Commands: []string{ - "echo hi; false", + Commands: []*safcm.Command{ + { + Cmd: "echo hi; false", + }, }, }, nil, @@ -204,11 +212,16 @@ func TestSyncCommands(t *testing.T) { "group2", "host.example.org", }, - Commands: []string{ - "echo first", - "echo second", - "false", - "echo third", + Commands: []*safcm.Command{ + { + Cmd: "echo first", + }, { + Cmd: "echo second", + }, { + Cmd: "false", + }, { + Cmd: "echo third", + }, }, }, nil, @@ -318,8 +331,10 @@ func TestSyncCommands(t *testing.T) { }, }, }, - Commands: []string{ - "echo; env | grep SAFCM_", + Commands: []*safcm.Command{ + { + Cmd: "echo; env | grep SAFCM_", + }, }, }, []string{ @@ -434,8 +449,10 @@ func TestSyncCommands(t *testing.T) { }, }, }, - Commands: []string{ - "echo; env | grep SAFCM_", + Commands: []*safcm.Command{ + { + Cmd: "echo; env | grep SAFCM_", + }, }, }, []string{ diff --git a/cmd/safcm/config/commands.go b/cmd/safcm/config/commands.go index 8a6f240..1ba2b18 100644 --- a/cmd/safcm/config/commands.go +++ b/cmd/safcm/config/commands.go @@ -23,12 +23,14 @@ import ( "path/filepath" "gopkg.in/yaml.v2" + + "ruderich.org/simon/safcm" ) -func LoadCommands(group string) ([]string, error) { +func LoadCommands(group string) ([]*safcm.Command, error) { path := filepath.Join(group, "commands.yaml") - var res []string + var cmds []string x, err := os.ReadFile(path) if err != nil { if os.IsNotExist(err) { @@ -36,9 +38,16 @@ func LoadCommands(group string) ([]string, error) { } return nil, err } - err = yaml.UnmarshalStrict(x, &res) + err = yaml.UnmarshalStrict(x, &cmds) if err != nil { return nil, fmt.Errorf("%s: failed to load: %v", path, err) } + + var res []*safcm.Command + for _, x := range cmds { + res = append(res, &safcm.Command{ + Cmd: x, + }) + } return res, nil } diff --git a/cmd/safcm/sync_sync.go b/cmd/safcm/sync_sync.go index f8aeea2..1328af7 100644 --- a/cmd/safcm/sync_sync.go +++ b/cmd/safcm/sync_sync.go @@ -91,7 +91,7 @@ func (s *Sync) hostSyncReq(detectedGroups []string) ( allFiles := make(map[string]*safcm.File) allPackagesMap := make(map[string]bool) // map to deduplicate allServicesMap := make(map[string]bool) // map to deduplicate - var allCommands []string + var allCommands []*safcm.Command for _, group := range groups { // Skip non-existent group directories diff --git a/cmd/safcm/sync_sync_test.go b/cmd/safcm/sync_sync_test.go index cf24d59..1294a45 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -136,9 +136,13 @@ func TestHostSyncReq(t *testing.T) { Services: []string{ "unbound", }, - Commands: []string{ - "echo command one", - "echo -n command two", + Commands: []*safcm.Command{ + { + Cmd: "echo command one", + }, + { + Cmd: "echo -n command two", + }, }, }, []string{ @@ -235,9 +239,13 @@ func TestHostSyncReq(t *testing.T) { Services: []string{ "unbound", }, - Commands: []string{ - "echo command one", - "echo -n command two", + Commands: []*safcm.Command{ + { + Cmd: "echo command one", + }, + { + Cmd: "echo -n command two", + }, }, }, nil, diff --git a/types.go b/types.go index 267872d..d886886 100644 --- a/types.go +++ b/types.go @@ -53,7 +53,7 @@ type MsgSyncReq struct { Files map[string]*File Packages []string Services []string - Commands []string + Commands []*Command } type MsgSyncResp struct { FileChanges []FileChange @@ -106,6 +106,10 @@ type File struct { TriggerCommands []string } +type Command struct { + Cmd string +} + type FileChange struct { Path string Created bool