Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/get_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"

"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/help"
"github.com/spf13/cobra"
)
Expand All @@ -18,7 +17,7 @@ var getConfigCmd = &cobra.Command{
group := args[1]

// Create Nacos client
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()

// Get config
fmt.Printf("Fetching config: %s (%s)...\n\n", dataID, group)
Expand Down
11 changes: 7 additions & 4 deletions cmd/get_skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"path/filepath"
"strings"

"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/help"
"github.com/nov11/nacos-cli/internal/skill"
"github.com/spf13/cobra"
)

var (
getSkillOutput string
getSkillOutput string
getSkillVersion string
getSkillLabel string
)

var getSkillCmd = &cobra.Command{
Expand Down Expand Up @@ -43,7 +44,7 @@ var getSkillCmd = &cobra.Command{
}

// Create Nacos client
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()

// Create skill service
skillService := skill.NewSkillService(nacosClient)
Expand All @@ -58,7 +59,7 @@ var getSkillCmd = &cobra.Command{
fmt.Printf("\n[%d/%d] ", i+1, len(skillNames))
}
fmt.Printf("Fetching skill: %s...\n", skillName)
err := skillService.GetSkill(skillName, getSkillOutput)
err := skillService.GetSkill(skillName, getSkillOutput, getSkillVersion, getSkillLabel)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to download skill '%s': %v\n", skillName, err)
failCount++
Expand Down Expand Up @@ -89,5 +90,7 @@ var getSkillCmd = &cobra.Command{

func init() {
getSkillCmd.Flags().StringVarP(&getSkillOutput, "output", "o", "", "Output directory (default: ~/.skills)")
getSkillCmd.Flags().StringVar(&getSkillVersion, "version", "", "Specific version to download (e.g. v1, v2)")
getSkillCmd.Flags().StringVar(&getSkillLabel, "label", "", "Route label to resolve version (e.g. latest, stable)")
rootCmd.AddCommand(getSkillCmd)
}
3 changes: 1 addition & 2 deletions cmd/interactive.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/terminal"
"github.com/spf13/cobra"
)
Expand All @@ -12,7 +11,7 @@ var interactiveCmd = &cobra.Command{
Long: `Start an interactive terminal for managing Nacos configurations and skills`,
Run: func(cmd *cobra.Command, args []string) {
// Create Nacos client
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()

// Create and start terminal
term := terminal.NewTerminal(nacosClient)
Expand Down
3 changes: 1 addition & 2 deletions cmd/list_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"

"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/help"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +20,7 @@ var listConfigCmd = &cobra.Command{
Long: help.ConfigList.FormatForCLI("nacos-cli"),
Run: func(cmd *cobra.Command, args []string) {
// Create Nacos client
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()

// List configs
configs, err := nacosClient.ListConfigs(configListDataID, configListGroup, "", configListPage, configListSize)
Expand Down
3 changes: 1 addition & 2 deletions cmd/list_skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"

"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/help"
"github.com/nov11/nacos-cli/internal/skill"
"github.com/spf13/cobra"
Expand All @@ -23,7 +22,7 @@ var listSkillCmd = &cobra.Command{
Long: help.SkillList.FormatForCLI("nacos-cli"),
Run: func(cmd *cobra.Command, args []string) {
// Create Nacos client
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()

// Create skill service
skillService := skill.NewSkillService(nacosClient)
Expand Down
7 changes: 6 additions & 1 deletion cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,20 @@ Examples:
if input == "" || input == "y" || input == "yes" {
fmt.Println()
// Start interactive terminal with the edited config
nacosClient := client.NewNacosClient(
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)
Expand Down
47 changes: 23 additions & 24 deletions cmd/upload_skill.go → cmd/publish_skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ import (
"path/filepath"
"strings"

"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/help"
"github.com/nov11/nacos-cli/internal/skill"
"github.com/spf13/cobra"
)

var (
uploadAll bool
publishAll bool
)

var uploadSkillCmd = &cobra.Command{
Use: "skill-upload [skillPath]",
Short: "Upload a skill to Nacos",
Long: help.SkillUpload.FormatForCLI("nacos-cli"),
var publishSkillCmd = &cobra.Command{
Use: "skill-publish [skillPath]",
Short: "Publish a skill to Nacos (upload as ZIP)",
Long: help.SkillPublish.FormatForCLI("nacos-cli"),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Expand All @@ -29,23 +28,23 @@ var uploadSkillCmd = &cobra.Command{
skillPath := args[0]

// Create Nacos client
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()

// Create skill service
skillService := skill.NewSkillService(nacosClient)

// Handle batch upload
if uploadAll {
uploadAllSkills(skillPath, skillService)
// Handle batch publish
if publishAll {
publishAllSkills(skillPath, skillService)
return
}

// Single skill upload
uploadSingleSkill(skillPath, skillService)
// Single skill publish
publishSingleSkill(skillPath, skillService)
},
}

func uploadSingleSkill(skillPath string, skillService *skill.SkillService) {
func publishSingleSkill(skillPath string, skillService *skill.SkillService) {
// Expand ~ to home directory
if strings.HasPrefix(skillPath, "~") {
homeDir, err := os.UserHomeDir()
Expand All @@ -58,16 +57,16 @@ func uploadSingleSkill(skillPath string, skillService *skill.SkillService) {
checkError(err)

skillName := filepath.Base(absPath)
fmt.Printf("Uploading skill: %s...\n", skillName)
fmt.Printf("Publishing skill: %s...\n", skillName)

err = skillService.UploadSkill(absPath)
checkError(err)

fmt.Printf("Skill uploaded successfully!\n")
fmt.Printf(" Tip: Use 'skill-list' to verify or 'skill-get %s' to download\n", skillName)
fmt.Printf("Skill published successfully!\n")
fmt.Printf(" Tip: Use the Nacos console to review and go online, or use 'skill-list' to verify.\n")
}

func uploadAllSkills(folderPath string, skillService *skill.SkillService) {
func publishAllSkills(folderPath string, skillService *skill.SkillService) {
// Expand ~ to home directory
if strings.HasPrefix(folderPath, "~") {
homeDir, err := os.UserHomeDir()
Expand Down Expand Up @@ -108,35 +107,35 @@ func uploadAllSkills(folderPath string, skillService *skill.SkillService) {

for i, skillName := range skillDirs {
fmt.Println(strings.Repeat("=", 80))
fmt.Printf("[%d/%d] Uploading skill: %s\n", i+1, len(skillDirs), skillName)
fmt.Printf("[%d/%d] Publishing skill: %s\n", i+1, len(skillDirs), skillName)
fmt.Println(strings.Repeat("=", 80))

skillPath := filepath.Join(folderPath, skillName)
err := skillService.UploadSkill(skillPath)
if err != nil {
fmt.Printf("Upload failed: %v\n", err)
fmt.Printf("Publish failed: %v\n", err)
failedCount++
} else {
fmt.Printf("Upload successful!\n")
fmt.Printf("Publish successful!\n")
successCount++
}
fmt.Println()
}

// Summary
fmt.Println(strings.Repeat("=", 80))
fmt.Println("Batch Upload Complete")
fmt.Println("Batch Publish Complete")
fmt.Println(strings.Repeat("=", 80))
fmt.Printf("Success: %d\n", successCount)
if failedCount > 0 {
fmt.Printf("Failed: %d\n", failedCount)
}
fmt.Printf("Total: %d\n", len(skillDirs))
fmt.Println()
fmt.Println("Tip: Use 'skill-list' to view all uploaded skills")
fmt.Println("Tip: Use the Nacos console to review and go online, or use 'skill-list' to verify.")
}

func init() {
uploadSkillCmd.Flags().BoolVar(&uploadAll, "all", false, "Upload all skills in the directory")
rootCmd.AddCommand(uploadSkillCmd)
publishSkillCmd.Flags().BoolVar(&publishAll, "all", false, "Publish all skills in the directory")
rootCmd.AddCommand(publishSkillCmd)
}
56 changes: 33 additions & 23 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
authType string
username string
password string
token string
accessKey string
secretKey string
configFile string
Expand Down Expand Up @@ -55,7 +56,7 @@ Examples:
var err error

// Check if any connection parameters are provided via command line
hasCommandLineConfig := host != "" || port > 0 || serverAddr != "" || username != "" || password != "" || accessKey != "" || secretKey != ""
hasCommandLineConfig := host != "" || port > 0 || serverAddr != "" || username != "" || password != "" || token != "" || accessKey != "" || secretKey != ""

if configFile != "" {
// Explicit config file specified
Expand Down Expand Up @@ -105,31 +106,29 @@ Examples:
namespace = fileConfig.Namespace
}

// AuthType: command line > config file > default nacos
if authType == "" {
if fileConfig != nil && fileConfig.AuthType != "" {
authType = fileConfig.AuthType
} else {
authType = "nacos"
}
// AuthType: command line > config file > auto-detect by NewNacosClient
if authType == "" && fileConfig != nil && fileConfig.AuthType != "" {
authType = fileConfig.AuthType
}

// Username: command line > config file > default
if username == "" {
if fileConfig != nil && fileConfig.Username != "" {
username = fileConfig.Username
} else {
username = "nacos"
}
// Username: command line > config file
if username == "" && fileConfig != nil && fileConfig.Username != "" {
username = fileConfig.Username
}

// Password: command line > config file > default
if password == "" {
if fileConfig != nil && fileConfig.Password != "" {
password = fileConfig.Password
} else {
password = "nacos"
}
// Password: command line > config file
if password == "" && fileConfig != nil && fileConfig.Password != "" {
password = fileConfig.Password
}

// Token: command line > config file (token takes priority over username/password when set)
if token == "" && fileConfig != nil && fileConfig.Token != "" {
token = fileConfig.Token
}
// If token is provided, clear username/password defaults to avoid unnecessary login attempts
if token != "" {
username = ""
password = ""
}

// AccessKey / SecretKey: command line > config file(AuthType=aliyun 时使用)
Expand All @@ -147,7 +146,7 @@ Examples:
},
Run: func(cmd *cobra.Command, args []string) {
// Default behavior: start interactive terminal
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()
term := terminal.NewTerminal(nacosClient)
if err := term.Start(); err != nil {
checkError(err)
Expand All @@ -173,6 +172,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&authType, "auth-type", "", "Auth type: nacos (username/password) or aliyun (AK/SK)")
rootCmd.PersistentFlags().StringVarP(&username, "username", "u", "", "Username (nacos auth)")
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "Password (nacos auth)")
rootCmd.PersistentFlags().StringVar(&token, "token", "", "Access token (skips username/password login)")
rootCmd.PersistentFlags().StringVar(&accessKey, "access-key", "", "AccessKey (aliyun auth)")
rootCmd.PersistentFlags().StringVar(&secretKey, "secret-key", "", "SecretKey (aliyun auth)")

Expand All @@ -186,3 +186,13 @@ func checkError(err error) {
os.Exit(1)
}
}

// mustNewNacosClient creates a NacosClient and exits with a clear error message on failure (e.g. login failed).
func mustNewNacosClient() *client.NacosClient {
c, err := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey, token)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
return c
}
3 changes: 1 addition & 2 deletions cmd/set_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"

"github.com/nov11/nacos-cli/internal/client"
"github.com/nov11/nacos-cli/internal/help"
"github.com/spf13/cobra"
)
Expand All @@ -30,7 +29,7 @@ var setConfigCmd = &cobra.Command{
}

// Create Nacos client
nacosClient := client.NewNacosClient(serverAddr, namespace, authType, username, password, accessKey, secretKey)
nacosClient := mustNewNacosClient()

fmt.Printf("Publishing config: %s (%s)...\n", dataID, group)
err = nacosClient.PublishConfig(dataID, group, content)
Expand Down
Loading