-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
184 lines (155 loc) · 4.32 KB
/
config.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package cmd
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/maran1947/localite/internal/config"
"github.com/maran1947/localite/internal/utils"
"github.com/spf13/cobra"
)
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage configurations",
Long: `Manage application configurations using key-value pairs.`,
Run: func(cmd *cobra.Command, args []string) {
listFlag, _ := cmd.Flags().GetBool("list")
if listFlag {
configData, err := config.LoadConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
configJsonData, err := json.MarshalIndent(configData, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, "Error marshalling config to JSON:", err)
os.Exit(1)
}
fmt.Println(string(configJsonData))
os.Exit(0)
}
},
}
var setCmd = &cobra.Command{
Use: "set KEY=VALUE",
Short: "Set a configuration value",
Long: `Set a configuration value in the format KEY=VALUE.`,
Run: func(cmd *cobra.Command, args []string) {
listFlag, _ := cmd.Flags().GetBool("list")
if listFlag {
configData, err := config.LoadConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Println(configData)
os.Exit(0)
}
if len(args) < 1 {
fmt.Println("Please provide the key-value pair in the format: KEY=VALUE")
return
}
keyValuePair := strings.SplitN(args[0], "=", 2)
if len(keyValuePair) != 2 {
fmt.Println("Please provide the key-value pair in the format: KEY=VALUE")
return
}
key, value := keyValuePair[0], keyValuePair[1]
err := config.SaveConfig(key, value)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
},
}
var delCmd = &cobra.Command{
Use: "del KEY",
Short: "del a key-value from configuration",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide the key to delete.")
return
}
key := args[0]
localiteConfig, err := config.LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
os.Exit(1)
}
localiteConfig.DeleteConfigValue(key)
err = config.UpdateConfig(*localiteConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error saving config: %v\n", err)
os.Exit(1)
}
fmt.Printf("Configuration key '%s' has been deleted.\n", key)
os.Exit(0)
},
}
var getCmd = &cobra.Command{
Use: "get KEY",
Short: "Get a value of a given key from configuration",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide the key.")
return
}
key := args[0]
localiteConfig, err := config.LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
os.Exit(1)
}
value, exists := localiteConfig.GetConfigValue(key)
if !exists {
fmt.Printf("Given key '%s' doesn not exists.\n", key)
os.Exit(1)
}
fmt.Println(value)
os.Exit(0)
},
}
var pushCmd = &cobra.Command{
Use: "push -f <file_path> <keys>",
Short: "Save the provided keys to the specified file.",
Run: func(cmd *cobra.Command, args []string) {
file, _ := cmd.Flags().GetString("file")
keys, _ := cmd.Flags().GetBool("keys")
if file == "" {
fmt.Println("Please specify the file path.")
os.Exit(1)
}
if !keys || len(args) < 1 {
fmt.Println("Please provide the keys using the -k flag followed by keys (e.g., -k KEY1 KEY2 ... KEYN).")
os.Exit(1)
}
localiteConfig, err := config.LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error in loading config: %v\n", err)
os.Exit(1)
}
var userKeys []string
for _, key := range args {
if value, exists := localiteConfig.GetConfigValue(key); exists {
entry := fmt.Sprintf("%s=%s", key, value)
userKeys = append(userKeys, entry)
}
}
pushError := utils.PushToFile(file, userKeys)
if pushError != nil {
fmt.Printf("Failed to push keys: %v\n", pushError)
os.Exit(1)
}
fmt.Printf("Keys %v pushed to file: %s", args, file)
os.Exit(0)
},
}
func init() {
configCmd.PersistentFlags().BoolP("list", "l", false, "Displays the current configurations for the Localite tool")
pushCmd.PersistentFlags().StringP("file", "f", "", "File where keys will be push")
pushCmd.PersistentFlags().BoolP("keys", "k", false, "Provided keys")
configCmd.AddCommand(setCmd)
configCmd.AddCommand(delCmd)
configCmd.AddCommand(getCmd)
configCmd.AddCommand(pushCmd)
}