Skip to content

Commit 9f79790

Browse files
Clean up setup age (#264)
Can use some map/uniqby funcs to avoid some manual looping/set manipulation
1 parent 12d0ae5 commit 9f79790

1 file changed

Lines changed: 4 additions & 17 deletions

File tree

pkg/crypto/age.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"github.com/pluralsh/plural/pkg/utils/pathing"
1818
"github.com/pluralsh/polly/algorithms"
1919
"github.com/pluralsh/polly/containers"
20+
"github.com/samber/lo"
2021
"gopkg.in/yaml.v2"
21-
"k8s.io/apimachinery/pkg/util/sets"
2222
)
2323

2424
const (
@@ -134,27 +134,14 @@ func SetupAge(client api.Client, emails []string) error {
134134
if err != nil {
135135
return err
136136
}
137+
137138
missingEmails := findMissingKeyForEmail(emails, keys)
138139
if len(missingEmails) > 0 {
139140
return fmt.Errorf("Some of the users %v have no keys setup", missingEmails)
140141
}
141142

142-
present := sets.NewString()
143-
dedupeKey := func(id *AgeIdentity) string { return fmt.Sprintf("%s::%s", id.Email, id.Key) }
144-
145-
idents := ageConfig.Identities
146-
for _, id := range idents {
147-
present.Insert(dedupeKey(id))
148-
}
149-
150-
for _, key := range keys {
151-
id := &AgeIdentity{Key: key.Content, Email: key.User.Email}
152-
if !present.Has(dedupeKey(id)) {
153-
idents = append(idents, id)
154-
}
155-
}
156-
157-
ageConfig.Identities = idents
143+
idents := algorithms.Map(keys, func(key *api.PublicKey) *AgeIdentity { return &AgeIdentity{Key: key.Content, Email: key.User.Email} })
144+
ageConfig.Identities = lo.UniqBy(append(idents, ageConfig.Identities...), func(id *AgeIdentity) string { return fmt.Sprintf("%s::%s", id.Email, id.Key) })
158145
}
159146

160147
keyPath := pathing.SanitizeFilepath(filepath.Join(cryptPath(), "key"))

0 commit comments

Comments
 (0)