Skip to content

Commit 3b4710e

Browse files
committed
style: apply gopls modernize and go fix
Run gopls modernize -fix and go fix across the codebase. Changes: - Replace interface{} with any - Use maps.Copy instead of for k, v := range loops - Use slices.Contains instead of explicit lookup loops - Strip trailing whitespace and align gofmt-style declarations No behavior changes. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la>
1 parent ba4b9e7 commit 3b4710e

12 files changed

Lines changed: 195 additions & 204 deletions

File tree

main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"slices"
89
"strings"
910
"time"
1011

@@ -83,23 +84,23 @@ func init() {
8384
for _, cmd := range commands.Commands {
8485
rootCmd.AddCommand(cmd)
8586
}
86-
87+
8788
// Add PersistentPreRunE to handle root detection and config loading
8889
originalPersistentPreRunE := rootCmd.PersistentPreRunE
8990
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
9091
// Detect and set project root using fallback strategy
9192
if err := commands.DetectAndSetRoot(cmd, args); err != nil {
9293
return err
9394
}
94-
95+
9596
// Load config after root detection (skip for init and completion commands)
9697
if !isCommandOrParent(cmd, skipConfigCommands...) {
9798
configFile := filepath.Join(commands.Config.RootDir, "Chart.yaml")
9899
if err := loadConfig(configFile); err != nil {
99100
return fmt.Errorf("error loading configuration: %w", err)
100101
}
101102
}
102-
103+
103104
// Ensure talosconfig path is set to project root if not explicitly set via flag
104105
// This is needed for all commands that use talosctl client (template, apply, etc.)
105106
if !cmd.PersistentFlags().Changed("talosconfig") {
@@ -121,7 +122,7 @@ func init() {
121122
commands.GlobalArgs.Talosconfig = talosconfigPath
122123
}
123124
}
124-
125+
125126
if originalPersistentPreRunE != nil {
126127
return originalPersistentPreRunE(cmd, args)
127128
}
@@ -138,7 +139,7 @@ func initConfig() {
138139
if cmd.HasParent() && cmd.Parent() != rootCmd {
139140
cmd = cmd.Parent()
140141
}
141-
142+
142143
if strings.HasPrefix(cmd.Use, "init") {
143144
if strings.HasPrefix(Version, "v") {
144145
commands.Config.InitOptions.Version = strings.TrimPrefix(Version, `v`)
@@ -151,10 +152,8 @@ func initConfig() {
151152
// isCommandOrParent checks if the command or any of its parents matches one of the given names.
152153
func isCommandOrParent(cmd *cobra.Command, names ...string) bool {
153154
for c := cmd; c != nil; c = c.Parent() {
154-
for _, name := range names {
155-
if c.Name() == name {
156-
return true
157-
}
155+
if slices.Contains(names, c.Name()) {
156+
return true
158157
}
159158
}
160159
return false

pkg/age/age.go

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ import (
2929
)
3030

3131
const (
32-
keyFileName = "talm.key"
33-
encryptedSecretsFile = "secrets.encrypted.yaml"
34-
plainSecretsFile = "secrets.yaml"
35-
ageEncryptionPrefix = "ENC[AGE,data:"
36-
ageEncryptionSuffix = "]"
32+
keyFileName = "talm.key"
33+
encryptedSecretsFile = "secrets.encrypted.yaml"
34+
plainSecretsFile = "secrets.yaml"
35+
ageEncryptionPrefix = "ENC[AGE,data:"
36+
ageEncryptionSuffix = "]"
3737
)
3838

3939
// GenerateKey generates a new age identity and saves it to talm.key file in age keygen format
4040
// Returns true if a new key was created (not loaded from existing file)
4141
func GenerateKey(rootDir string) (*age.X25519Identity, bool, error) {
4242
keyFile := filepath.Join(rootDir, keyFileName)
43-
43+
4444
// Check if key already exists
4545
if _, err := os.Stat(keyFile); err == nil {
4646
// Key exists, load it
@@ -50,21 +50,21 @@ func GenerateKey(rootDir string) (*age.X25519Identity, bool, error) {
5050
}
5151
return identity, false, nil
5252
}
53-
53+
5454
// Generate new key
5555
identity, err := age.GenerateX25519Identity()
5656
if err != nil {
5757
return nil, false, fmt.Errorf("failed to generate age identity: %w", err)
5858
}
5959

6060
publicKey := identity.Recipient().String()
61-
61+
6262
// Format key file in age keygen format
6363
now := time.Now()
6464
keyData := fmt.Sprintf("# created: %s\n", now.Format(time.RFC3339))
6565
keyData += fmt.Sprintf("# public key: %s\n", publicKey)
6666
keyData += identity.String() + "\n"
67-
67+
6868
if err := os.WriteFile(keyFile, []byte(keyData), 0o600); err != nil {
6969
return nil, false, fmt.Errorf("failed to write key file: %w", err)
7070
}
@@ -125,11 +125,11 @@ func GetPublicKeyFromFile(rootDir string) (string, error) {
125125
}
126126

127127
// Find the public key line (starts with # public key:)
128-
lines := strings.Split(string(keyData), "\n")
129-
for _, line := range lines {
128+
lines := strings.SplitSeq(string(keyData), "\n")
129+
for line := range lines {
130130
line = strings.TrimSpace(line)
131-
if strings.HasPrefix(line, "# public key: ") {
132-
return strings.TrimPrefix(line, "# public key: "), nil
131+
if after, ok := strings.CutPrefix(line, "# public key: "); ok {
132+
return after, nil
133133
}
134134
}
135135

@@ -171,13 +171,13 @@ func EncryptSecretsFile(rootDir string) error {
171171
}
172172

173173
// Parse YAML
174-
var secrets map[string]interface{}
174+
var secrets map[string]any
175175
if err := yaml.Unmarshal(secretsData, &secrets); err != nil {
176176
return fmt.Errorf("failed to parse secrets YAML: %w", err)
177177
}
178178

179179
// If encrypted file exists, load it and merge (preserve unchanged encrypted values)
180-
var encryptedSecrets map[string]interface{}
180+
var encryptedSecrets map[string]any
181181
if _, err := os.Stat(encryptedFile); err == nil {
182182
encryptedData, err := os.ReadFile(encryptedFile)
183183
if err == nil {
@@ -187,30 +187,30 @@ func EncryptSecretsFile(rootDir string) error {
187187
if err != nil {
188188
return fmt.Errorf("failed to merge and encrypt: %w", err)
189189
}
190-
encryptedSecrets = merged.(map[string]interface{})
190+
encryptedSecrets = merged.(map[string]any)
191191
} else {
192192
// If parsing fails, encrypt everything
193193
encrypted, err := encryptYAMLValues(secrets, identity.Recipient())
194194
if err != nil {
195195
return fmt.Errorf("failed to encrypt secrets: %w", err)
196196
}
197-
encryptedSecrets = encrypted.(map[string]interface{})
197+
encryptedSecrets = encrypted.(map[string]any)
198198
}
199199
} else {
200200
// If reading fails, encrypt everything
201201
encrypted, err := encryptYAMLValues(secrets, identity.Recipient())
202202
if err != nil {
203203
return fmt.Errorf("failed to encrypt secrets: %w", err)
204204
}
205-
encryptedSecrets = encrypted.(map[string]interface{})
205+
encryptedSecrets = encrypted.(map[string]any)
206206
}
207207
} else {
208208
// No encrypted file exists, encrypt everything
209209
encrypted, err := encryptYAMLValues(secrets, identity.Recipient())
210210
if err != nil {
211211
return fmt.Errorf("failed to encrypt secrets: %w", err)
212212
}
213-
encryptedSecrets = encrypted.(map[string]interface{})
213+
encryptedSecrets = encrypted.(map[string]any)
214214
}
215215

216216
// Marshal encrypted YAML
@@ -245,7 +245,7 @@ func DecryptSecretsFile(rootDir string) error {
245245
}
246246

247247
// Parse YAML
248-
var encryptedSecrets map[string]interface{}
248+
var encryptedSecrets map[string]any
249249
if err := yaml.Unmarshal(encryptedData, &encryptedSecrets); err != nil {
250250
return fmt.Errorf("failed to parse encrypted YAML: %w", err)
251251
}
@@ -271,10 +271,10 @@ func DecryptSecretsFile(rootDir string) error {
271271
}
272272

273273
// encryptYAMLValues recursively encrypts string values in YAML structure
274-
func encryptYAMLValues(data interface{}, recipient *age.X25519Recipient) (interface{}, error) {
274+
func encryptYAMLValues(data any, recipient *age.X25519Recipient) (any, error) {
275275
switch v := data.(type) {
276-
case map[string]interface{}:
277-
result := make(map[string]interface{})
276+
case map[string]any:
277+
result := make(map[string]any)
278278
for key, value := range v {
279279
encryptedValue, err := encryptYAMLValues(value, recipient)
280280
if err != nil {
@@ -283,8 +283,8 @@ func encryptYAMLValues(data interface{}, recipient *age.X25519Recipient) (interf
283283
result[key] = encryptedValue
284284
}
285285
return result, nil
286-
case []interface{}:
287-
result := make([]interface{}, len(v))
286+
case []any:
287+
result := make([]any, len(v))
288288
for i, item := range v {
289289
encryptedItem, err := encryptYAMLValues(item, recipient)
290290
if err != nil {
@@ -306,10 +306,10 @@ func encryptYAMLValues(data interface{}, recipient *age.X25519Recipient) (interf
306306
}
307307

308308
// decryptYAMLValues recursively decrypts string values in YAML structure
309-
func decryptYAMLValues(data interface{}, identity *age.X25519Identity) (interface{}, error) {
309+
func decryptYAMLValues(data any, identity *age.X25519Identity) (any, error) {
310310
switch v := data.(type) {
311-
case map[string]interface{}:
312-
result := make(map[string]interface{})
311+
case map[string]any:
312+
result := make(map[string]any)
313313
for key, value := range v {
314314
decryptedValue, err := decryptYAMLValues(value, identity)
315315
if err != nil {
@@ -318,8 +318,8 @@ func decryptYAMLValues(data interface{}, identity *age.X25519Identity) (interfac
318318
result[key] = decryptedValue
319319
}
320320
return result, nil
321-
case []interface{}:
322-
result := make([]interface{}, len(v))
321+
case []any:
322+
result := make([]any, len(v))
323323
for i, item := range v {
324324
decryptedItem, err := decryptYAMLValues(item, identity)
325325
if err != nil {
@@ -358,16 +358,16 @@ func decryptYAMLValuesString(encrypted string, identity *age.X25519Identity) (st
358358

359359
// mergeAndEncryptYAMLValues merges plain and encrypted YAML, encrypting only changed values
360360
// This ensures idempotency: unchanged values keep their encrypted form
361-
func mergeAndEncryptYAMLValues(plain, encrypted interface{}, identity *age.X25519Identity) (interface{}, error) {
361+
func mergeAndEncryptYAMLValues(plain, encrypted any, identity *age.X25519Identity) (any, error) {
362362
switch plainVal := plain.(type) {
363-
case map[string]interface{}:
364-
encryptedMap, ok := encrypted.(map[string]interface{})
363+
case map[string]any:
364+
encryptedMap, ok := encrypted.(map[string]any)
365365
if !ok {
366366
// Type mismatch, encrypt everything
367367
return encryptYAMLValues(plain, identity.Recipient())
368368
}
369-
370-
result := make(map[string]interface{})
369+
370+
result := make(map[string]any)
371371
// Copy all keys from plain (to handle new keys)
372372
for key, plainValue := range plainVal {
373373
if encryptedValue, exists := encryptedMap[key]; exists {
@@ -387,15 +387,15 @@ func mergeAndEncryptYAMLValues(plain, encrypted interface{}, identity *age.X2551
387387
}
388388
}
389389
return result, nil
390-
391-
case []interface{}:
392-
encryptedSlice, ok := encrypted.([]interface{})
390+
391+
case []any:
392+
encryptedSlice, ok := encrypted.([]any)
393393
if !ok || len(plainVal) != len(encryptedSlice) {
394394
// Type or length mismatch, encrypt everything
395395
return encryptYAMLValues(plain, identity.Recipient())
396396
}
397-
398-
result := make([]interface{}, len(plainVal))
397+
398+
result := make([]any, len(plainVal))
399399
for i, plainItem := range plainVal {
400400
merged, err := mergeAndEncryptYAMLValues(plainItem, encryptedSlice[i], identity)
401401
if err != nil {
@@ -404,14 +404,14 @@ func mergeAndEncryptYAMLValues(plain, encrypted interface{}, identity *age.X2551
404404
result[i] = merged
405405
}
406406
return result, nil
407-
407+
408408
case string:
409409
encryptedStr, ok := encrypted.(string)
410410
if !ok {
411411
// Type mismatch, encrypt
412412
return encryptYAMLValues(plain, identity.Recipient())
413413
}
414-
414+
415415
// Check if encrypted value is already encrypted
416416
if strings.HasPrefix(encryptedStr, ageEncryptionPrefix) && strings.HasSuffix(encryptedStr, ageEncryptionSuffix) {
417417
// Decrypt existing value to compare
@@ -423,7 +423,7 @@ func mergeAndEncryptYAMLValues(plain, encrypted interface{}, identity *age.X2551
423423
}
424424
// Encrypt the new value (if decryption fails, values differ, or both are plain)
425425
return encryptYAMLValues(plain, identity.Recipient())
426-
426+
427427
default:
428428
// For other types, compare directly
429429
if plain == encrypted {
@@ -495,7 +495,7 @@ func RotateKeys(rootDir string) error {
495495
return fmt.Errorf("failed to read encrypted file: %w", err)
496496
}
497497

498-
var encryptedSecrets map[string]interface{}
498+
var encryptedSecrets map[string]any
499499
if err := yaml.Unmarshal(encryptedData, &encryptedSecrets); err != nil {
500500
return fmt.Errorf("failed to parse encrypted YAML: %w", err)
501501
}
@@ -560,13 +560,13 @@ func EncryptYAMLFile(rootDir, plainFile, encryptedFile string) error {
560560
}
561561

562562
// Parse YAML
563-
var yamlData map[string]interface{}
563+
var yamlData map[string]any
564564
if err := yaml.Unmarshal(plainData, &yamlData); err != nil {
565565
return fmt.Errorf("failed to parse YAML: %w", err)
566566
}
567567

568568
// If encrypted file exists, load it and merge (preserve unchanged encrypted values)
569-
var encryptedYAML map[string]interface{}
569+
var encryptedYAML map[string]any
570570
if _, err := os.Stat(encryptedFilePath); err == nil {
571571
encryptedData, err := os.ReadFile(encryptedFilePath)
572572
if err == nil {
@@ -576,30 +576,30 @@ func EncryptYAMLFile(rootDir, plainFile, encryptedFile string) error {
576576
if err != nil {
577577
return fmt.Errorf("failed to merge and encrypt: %w", err)
578578
}
579-
encryptedYAML = merged.(map[string]interface{})
579+
encryptedYAML = merged.(map[string]any)
580580
} else {
581581
// If parsing fails, encrypt everything
582582
encrypted, err := encryptYAMLValues(yamlData, identity.Recipient())
583583
if err != nil {
584584
return fmt.Errorf("failed to encrypt YAML values: %w", err)
585585
}
586-
encryptedYAML = encrypted.(map[string]interface{})
586+
encryptedYAML = encrypted.(map[string]any)
587587
}
588588
} else {
589589
// If reading fails, encrypt everything
590590
encrypted, err := encryptYAMLValues(yamlData, identity.Recipient())
591591
if err != nil {
592592
return fmt.Errorf("failed to encrypt YAML values: %w", err)
593593
}
594-
encryptedYAML = encrypted.(map[string]interface{})
594+
encryptedYAML = encrypted.(map[string]any)
595595
}
596596
} else {
597597
// No encrypted file exists, encrypt everything
598598
encrypted, err := encryptYAMLValues(yamlData, identity.Recipient())
599599
if err != nil {
600600
return fmt.Errorf("failed to encrypt YAML values: %w", err)
601601
}
602-
encryptedYAML = encrypted.(map[string]interface{})
602+
encryptedYAML = encrypted.(map[string]any)
603603
}
604604

605605
// Marshal encrypted YAML
@@ -634,7 +634,7 @@ func DecryptYAMLFile(rootDir, encryptedFile, plainFile string) error {
634634
}
635635

636636
// Parse YAML
637-
var encryptedYAML map[string]interface{}
637+
var encryptedYAML map[string]any
638638
if err := yaml.Unmarshal(encryptedData, &encryptedYAML); err != nil {
639639
return fmt.Errorf("failed to parse encrypted YAML: %w", err)
640640
}
@@ -658,4 +658,3 @@ func DecryptYAMLFile(rootDir, encryptedFile, plainFile string) error {
658658

659659
return nil
660660
}
661-

0 commit comments

Comments
 (0)