-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
90 lines (72 loc) · 1.9 KB
/
write.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"fmt"
"strings"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// writeCmd represents the write command
var writeCmd = &cobra.Command{
Use: "write",
Aliases: []string{"w"},
Short: "Write key/value pair to secret data",
Long: `Write key/value pair to secret data
To add "USER_PASSWORD: admin123" to "db-pass" secret data, located in "core" namespace, command will be:
ksec write db-pass USER_PASSWORD admin123 -n core
`,
Args: cobra.MatchAll(cobra.ExactArgs(3), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
secretName := args[0]
secretKey := args[1]
newValue := args[2]
clientSet, err := getKubernetesClient()
if err != nil {
return err
}
secretsClient := clientSet.CoreV1().Secrets(namespace)
secret, err := secretsClient.Get(context.TODO(), secretName, metav1.GetOptions{})
if err != nil {
return err
}
if oldValue, ok := secret.Data[secretKey]; ok {
if string(oldValue) == newValue {
fmt.Println("Current value match with the desired one")
return nil
}
if !confirmOverwrite(string(oldValue), newValue) {
return nil
}
}
secret.Data[secretKey] = []byte(newValue)
_, err = secretsClient.Update(context.TODO(), secret, metav1.UpdateOptions{})
return err
},
}
func init() {
rootCmd.AddCommand(writeCmd)
}
func confirmOverwrite(oldValue, newValue string) bool {
var input string
fmt.Println("Current value:", oldValue)
fmt.Println("New value:", newValue)
fmt.Println()
for {
fmt.Printf("Do you want to continue with this operation? [y|n]: ")
_, err := fmt.Scanln(&input)
if err != nil {
panic(err)
}
input = strings.ToLower(input)
if input == "y" || input == "yes" {
return true
}
if input == "n" || input == "no" {
return false
}
fmt.Printf("Unrecognized input %s\n", input)
}
}