-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
63 lines (54 loc) · 1.41 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/encloud-tech/encloud/pkg/api"
"github.com/encloud-tech/encloud/pkg/types"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
func ConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Update app configurations",
Long: `Update configurations for the application using a compatible yaml file`,
Run: func(cmd *cobra.Command, args []string) {
configFilePath, _ := cmd.Flags().GetString("path")
data, err := os.ReadFile(configFilePath)
if err != nil {
fmt.Fprint(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
var conf types.ConfYaml
if err := yaml.Unmarshal(data, &conf); err != nil {
fmt.Fprint(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
err = api.Store(conf)
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
response := types.ConfigResponse{
Status: "success",
StatusCode: http.StatusFound,
Message: "Configuration updated successfully.",
Data: conf,
}
encoded, err := json.MarshalIndent(response, "", " ")
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
fmt.Fprintf(cmd.OutOrStdout(), string(encoded))
},
}
cmd.Flags().StringP("path", "p", "", "Path to config file")
cmd.MarkFlagRequired("path")
return cmd
}
func init() {
RootCmd.AddCommand(ConfigCmd())
}