From 77f373a4aa590711155e3af0b768997781f81559 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sun, 9 May 2021 12:32:53 +0200 Subject: [PATCH] config: return map from TransitivelyDetectedGroups() 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 | 10 ++------- cmd/safcm/config/groups_test.go | 36 ++++++++++++++++----------------- cmd/safcm/sync.go | 5 +---- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/cmd/safcm/config/groups.go b/cmd/safcm/config/groups.go index c998fb8..466e493 100644 --- a/cmd/safcm/config/groups.go +++ b/cmd/safcm/config/groups.go @@ -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 } diff --git a/cmd/safcm/config/groups_test.go b/cmd/safcm/config/groups_test.go index 63c2683..7c3dd48 100644 --- a/cmd/safcm/config/groups_test.go +++ b/cmd/safcm/config/groups_test.go @@ -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, }, }, } diff --git a/cmd/safcm/sync.go b/cmd/safcm/sync.go index 77e83c4..6358f0c 100644 --- a/cmd/safcm/sync.go +++ b/cmd/safcm/sync.go @@ -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 = ` -- 2.43.2