forked from nacos-group/nacos-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_config.go
More file actions
68 lines (57 loc) · 1.53 KB
/
set_config.go
File metadata and controls
68 lines (57 loc) · 1.53 KB
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
package cmd
import (
"bufio"
"fmt"
"os"
"github.com/nov11/nacos-cli/internal/help"
"github.com/spf13/cobra"
)
var setConfigFile string
var setConfigCmd = &cobra.Command{
Use: "config-set [dataId] [group]",
Short: "Publish a configuration to Nacos",
Long: help.ConfigSet.FormatForCLI("nacos-cli"),
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
dataID := args[0]
group := args[1]
content, err := readSetConfigContent()
checkError(err)
if content == "" {
fmt.Fprintf(os.Stderr, "Error: config content is empty (use --file or stdin)\n")
os.Exit(1)
}
// Create Nacos client
nacosClient := mustNewNacosClient()
fmt.Printf("Publishing config: %s (%s)...\n", dataID, group)
err = nacosClient.PublishConfig(dataID, group, content)
checkError(err)
fmt.Println("Configuration published successfully")
},
}
func readSetConfigContent() (string, error) {
if setConfigFile != "" {
data, err := os.ReadFile(setConfigFile)
if err != nil {
return "", fmt.Errorf("read file %s: %w", setConfigFile, err)
}
return string(data), nil
}
// Read from stdin
var content string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if content != "" {
content += "\n"
}
content += scanner.Text()
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("read stdin: %w", err)
}
return content, nil
}
func init() {
setConfigCmd.Flags().StringVarP(&setConfigFile, "file", "f", "", "Path to config file (default: read from stdin)")
rootCmd.AddCommand(setConfigCmd)
}