]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
config: return map from TransitivelyDetectedGroups()
authorSimon Ruderich <simon@ruderich.org>
Sun, 9 May 2021 10:32:53 +0000 (12:32 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sun, 9 May 2021 13:18:45 +0000 (15:18 +0200)
This is less clean than the original slice of strings. However, it
removes unnecessary code as the caller requires a map instead of a
slice.

cmd/safcm/config/groups.go
cmd/safcm/config/groups_test.go
cmd/safcm/sync.go

index c998fb8a96f44ed2d3f6db7bac7c153214465581..466e493c401ea895062294304b0961c5f360ce2d 100644 (file)
@@ -193,7 +193,7 @@ func ResolveHostGroups(host string, groups map[string][]string,
 // TransitivelyDetectedGroups returns all groups which depend on "detected"
 // groups, either directly or by depending on groups which transitively depend
 // on "detected" groups.
-func TransitivelyDetectedGroups(groups map[string][]string) []string {
+func TransitivelyDetectedGroups(groups map[string][]string) map[string]bool {
        work := make(map[string][]string)
        for k, v := range groups {
                work[k] = v
@@ -220,11 +220,5 @@ func TransitivelyDetectedGroups(groups map[string][]string) []string {
                        break
                }
        }
-
-       var res []string
-       for x := range detected {
-               res = append(res, x)
-       }
-       sort.Strings(res)
-       return res
+       return detected
 }
index 63c26836d92de66969904d643d77d7c9ae9a8222..7c3dd483393e9dbb5791004633358912c705285b 100644 (file)
@@ -332,7 +332,7 @@ func TestTransitivelyDetectedGroups(t *testing.T) {
        tests := []struct {
                name   string
                groups map[string][]string
-               exp    []string
+               exp    map[string]bool
        }{
 
                {
@@ -351,7 +351,7 @@ func TestTransitivelyDetectedGroups(t *testing.T) {
                                        "d",
                                },
                        },
-                       nil,
+                       map[string]bool{},
                },
 
                {
@@ -367,8 +367,8 @@ func TestTransitivelyDetectedGroups(t *testing.T) {
                                        "d",
                                },
                        },
-                       []string{
-                               "group-a",
+                       map[string]bool{
+                               "group-a": true,
                        },
                },
 
@@ -389,8 +389,8 @@ func TestTransitivelyDetectedGroups(t *testing.T) {
                                        "d",
                                },
                        },
-                       []string{
-                               "group-a",
+                       map[string]bool{
+                               "group-a": true,
                        },
                },
 
@@ -418,12 +418,12 @@ func TestTransitivelyDetectedGroups(t *testing.T) {
                                        "b",
                                },
                        },
-                       []string{
-                               "group-a",
-                               "group-b",
-                               "group-c",
-                               "group-d",
-                               "group-e",
+                       map[string]bool{
+                               "group-a": true,
+                               "group-b": true,
+                               "group-c": true,
+                               "group-d": true,
+                               "group-e": true,
                        },
                },
 
@@ -453,12 +453,12 @@ func TestTransitivelyDetectedGroups(t *testing.T) {
                                        "b",
                                },
                        },
-                       []string{
-                               "group-a",
-                               "group-b",
-                               "group-c",
-                               "group-d",
-                               "group-e",
+                       map[string]bool{
+                               "group-a": true,
+                               "group-b": true,
+                               "group-c": true,
+                               "group-d": true,
+                               "group-e": true,
                        },
                },
        }
index 77e83c4187592ca1bd5cc31e44da3f4acfb4830d..6358f0cb60feb4570cc5dae1a9e0e24bce6a69ed 100644 (file)
@@ -226,10 +226,7 @@ func MainSync(args []string) error {
 func hostsToSync(names []string, allHosts *config.Hosts,
        allGroups map[string][]string) ([]*config.Host, error) {
 
-       detectedMap := make(map[string]bool)
-       for _, x := range config.TransitivelyDetectedGroups(allGroups) {
-               detectedMap[x] = true
-       }
+       detectedMap := config.TransitivelyDetectedGroups(allGroups)
 
        const detectedErr = `