]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
safcm: use Command struct instead of string to run commands
authorSimon Ruderich <simon@ruderich.org>
Tue, 20 Apr 2021 09:21:10 +0000 (11:21 +0200)
committerSimon Ruderich <simon@ruderich.org>
Tue, 20 Apr 2021 09:21:10 +0000 (11:21 +0200)
cmd/safcm-remote/sync/commands.go
cmd/safcm-remote/sync/commands_test.go
cmd/safcm/config/commands.go
cmd/safcm/sync_sync.go
cmd/safcm/sync_sync_test.go
types.go

index e3c48dd10e4df6d78ff0242439b462d16364ba14..84a318d79668b8e43118d9981e5d7736db3f766c 100644 (file)
@@ -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
                }
index 142a2542a931d4191d56f0b5be641da6a2b00cd7..ba7f4a2e89ec47e16fec11f88f41aad4577a2617 100644 (file)
@@ -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{
index 8a6f24014e4783c07defe314b1dd1b9db30238ae..1ba2b18a4f530b1ba2deb43035b597e3032da87b 100644 (file)
@@ -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
 }
index f8aeea2517031c69f214f04d8b13268f1f90ab3c..1328af7ca7acf6d87073c12cc8b71d36ca489e44 100644 (file)
@@ -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
index cf24d59801562f72641e4fc808074d2a5750e03b..1294a452dc65bbff13d74852789efdf356cc75e6 100644 (file)
@@ -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,
index 267872db82f1ef50fd452d9e05d1c8ef5db0b5cf..d8868868cd299ab9f59c437c757870fee9819992 100644 (file)
--- 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