forked from nacos-group/nacos-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.go
More file actions
192 lines (170 loc) · 5.29 KB
/
profile.go
File metadata and controls
192 lines (170 loc) · 5.29 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
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
185
186
187
188
189
190
191
192
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/config"
"github.com/nov11/nacos-cli/internal/terminal"
"github.com/spf13/cobra"
)
var profileCmd = &cobra.Command{
Use: "profile",
Short: "Manage configuration profiles",
Long: `Manage configuration profiles for different environments.
Examples:
nacos-cli profile edit # Edit default config
nacos-cli profile edit dev # Edit dev config
nacos-cli profile show # Show default config
nacos-cli profile show dev # Show dev config`,
}
var profileEditCmd = &cobra.Command{
Use: "edit [profile]",
Short: "Edit a configuration profile",
Long: `Interactively edit a configuration profile.
Examples:
nacos-cli profile edit # Edit default config
nacos-cli profile edit dev # Edit dev config
nacos-cli profile edit prod # Edit prod config`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Get profile name from args
profileName := config.DefaultProfile
if len(args) > 0 {
profileName = args[0]
}
// Get config path
configPath, err := config.GetProfileConfigPath(profileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Try to load existing config
var cfg *config.Config
if _, err := os.Stat(configPath); err == nil {
cfg, err = config.LoadConfig(configPath)
if err != nil {
fmt.Printf("Warning: Failed to load existing config: %v\n", err)
cfg = &config.Config{}
}
} else {
cfg = &config.Config{}
}
// Show current config and prompt for updates
fmt.Printf("Editing configuration for profile '%s'\n", profileName)
fmt.Printf("Config file: %s\n", configPath)
fmt.Println()
if err := cfg.PromptForUpdate(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Save the updated config
if err := cfg.SaveConfig(configPath); err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to save config: %v\n", err)
os.Exit(1)
}
fmt.Printf("\nConfiguration saved to %s\n", configPath)
// Ask user if they want to login
reader := bufio.NewReader(os.Stdin)
fmt.Print("\nLogin now? [Y/n] (Enter=Yes): ")
input, err := reader.ReadString('\n')
if err != nil {
return
}
input = strings.TrimSpace(strings.ToLower(input))
// Default to yes (empty input or 'y' or 'yes')
if input == "" || input == "y" || input == "yes" {
fmt.Println()
// Start interactive terminal with the edited config
nacosClient, err := client.NewNacosClient(
cfg.GetServerAddr(),
cfg.Namespace,
cfg.AuthType,
cfg.Username,
cfg.Password,
cfg.AccessKey,
cfg.SecretKey,
cfg.Token,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
term := terminal.NewTerminal(nacosClient)
if err := term.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
} else {
fmt.Printf("\nTo use this profile, run: nacos-cli --profile %s\n", profileName)
}
},
}
var profileShowCmd = &cobra.Command{
Use: "show [profile]",
Short: "Show a configuration profile",
Long: `Display the current configuration for a profile.
Examples:
nacos-cli profile show # Show default config
nacos-cli profile show dev # Show dev config
nacos-cli profile show prod # Show prod config`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Get profile name from args
profileName := config.DefaultProfile
if len(args) > 0 {
profileName = args[0]
}
// Get config path
configPath, err := config.GetProfileConfigPath(profileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
// Check if config exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
fmt.Printf("Profile '%s' does not exist.\n", profileName)
fmt.Printf("Config file: %s\n", configPath)
fmt.Println("\nRun 'nacos-cli profile edit " + profileName + "' to create it.")
return
}
// Load config
cfg, err := config.LoadConfig(configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to load config: %v\n", err)
os.Exit(1)
}
// Display config
fmt.Printf("Profile: %s\n", profileName)
fmt.Printf("Config file: %s\n", configPath)
fmt.Println("─────────────────────────────────────────")
fmt.Printf("%-15s %s\n", "host:", cfg.Host)
fmt.Printf("%-15s %d\n", "port:", cfg.Port)
fmt.Printf("%-15s %s\n", "auth-type:", cfg.AuthType)
if cfg.AuthType == "aliyun" {
fmt.Printf("%-15s %s\n", "access-key:", cfg.AccessKey)
fmt.Printf("%-15s %s\n", "secret-key:", maskPassword(cfg.SecretKey))
} else {
fmt.Printf("%-15s %s\n", "username:", cfg.Username)
fmt.Printf("%-15s %s\n", "password:", maskPassword(cfg.Password))
}
if cfg.Namespace != "" {
fmt.Printf("%-15s %s\n", "namespace:", cfg.Namespace)
} else {
fmt.Printf("%-15s %s\n", "namespace:", "(public)")
}
},
}
// maskPassword masks a password string for display
func maskPassword(pwd string) string {
if pwd == "" {
return "(not set)"
}
return "******"
}
func init() {
profileCmd.AddCommand(profileEditCmd)
profileCmd.AddCommand(profileShowCmd)
rootCmd.AddCommand(profileCmd)
}