소스 검색

refactor: plugin and bundle command, support version command

Yeuoly 8 달 전
부모
커밋
e54b307a4d

+ 3 - 3
.github/workflows/publish-cli.yaml

@@ -34,9 +34,9 @@ jobs:
       - name: Build CLI
         run: |
           go mod tidy
-          GOOS=windows GOARCH=${{ env.GOARCH }} go build -o dify-plugin-windows-${{ env.GOARCH }} ./cmd/commandline
-          GOOS=darwin GOARCH=${{ env.GOARCH }} go build -o dify-plugin-darwin-${{ env.GOARCH }} ./cmd/commandline
-          GOOS=linux GOARCH=${{ env.GOARCH }} go build -o dify-plugin-linux-${{ env.GOARCH }} ./cmd/commandline
+          GOOS=windows GOARCH=${{ env.GOARCH }} go build -ldflags "-X main.VersionX=${{ github.event.release.tag_name }}" -o dify-plugin-windows-${{ env.GOARCH }} ./cmd/commandline
+          GOOS=darwin GOARCH=${{ env.GOARCH }} go build -ldflags "-X main.VersionX=${{ github.event.release.tag_name }}" -o dify-plugin-darwin-${{ env.GOARCH }} ./cmd/commandline
+          GOOS=linux GOARCH=${{ env.GOARCH }} go build -ldflags "-X main.VersionX=${{ github.event.release.tag_name }}" -o dify-plugin-linux-${{ env.GOARCH }} ./cmd/commandline
 
       - name: Publish CLI
         run: |

+ 48 - 41
cmd/commandline/bundle.go

@@ -1,6 +1,8 @@
 package main
 
 import (
+	"fmt"
+	"path/filepath"
 	"strconv"
 
 	"github.com/langgenius/dify-plugin-daemon/cmd/commandline/bundle"
@@ -20,11 +22,12 @@ var (
 	}
 
 	bundleAnalyzeCommand = &cobra.Command{
-		Use:   "analyze",
+		Use:   "analyze [bundle_path]",
 		Short: "List all dependencies",
-		Long:  "List all dependencies",
+		Long:  "List all dependencies in the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
+			bundlePath := args[0]
 			bundle.ListDependencies(bundlePath)
 		},
 	}
@@ -36,11 +39,12 @@ var (
 	}
 
 	bundleAppendGithubDependencyCommand = &cobra.Command{
-		Use:   "github",
+		Use:   "github [bundle_path]",
 		Short: "Append a github dependency",
-		Long:  "Append a github dependency",
+		Long:  "Append a github dependency to the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
+			bundlePath := args[0]
 			repoPattern := c.Flag("repo_pattern").Value.String()
 			githubPattern, err := bundle_entities.NewGithubRepoPattern(repoPattern)
 			if err != nil {
@@ -52,11 +56,12 @@ var (
 	}
 
 	bundleAppendMarketplaceDependencyCommand = &cobra.Command{
-		Use:   "marketplace",
+		Use:   "marketplace [bundle_path]",
 		Short: "Append a marketplace dependency",
-		Long:  "Append a marketplace dependency",
+		Long:  "Append a marketplace dependency to the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
+			bundlePath := args[0]
 			marketplacePatternString := c.Flag("marketplace_pattern").Value.String()
 			marketplacePattern, err := bundle_entities.NewMarketplacePattern(marketplacePatternString)
 			if err != nil {
@@ -68,32 +73,35 @@ var (
 	}
 
 	bundleAppendPackageDependencyCommand = &cobra.Command{
-		Use:   "package",
+		Use:   "package [bundle_path]",
 		Short: "Append a local package dependency",
-		Long:  "Append a local package dependency",
+		Long:  "Append a local package dependency to the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
+			bundlePath := args[0]
 			packagePath := c.Flag("package_path").Value.String()
 			bundle.AddPackageDependency(bundlePath, packagePath)
 		},
 	}
 
 	bundleRegenerateCommand = &cobra.Command{
-		Use:   "regenerate",
+		Use:   "regenerate [bundle_path]",
 		Short: "Regenerate the bundle",
-		Long:  "Regenerate the bundle",
+		Long:  "Regenerate the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
+			bundlePath := args[0]
 			bundle.RegenerateBundle(bundlePath)
 		},
 	}
 
 	bundleRemoveDependencyCommand = &cobra.Command{
-		Use:   "remove",
+		Use:   "remove [bundle_path]",
 		Short: "Remove a dependency",
-		Long:  "Remove a dependency",
+		Long:  "Remove a dependency from the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
+			bundlePath := args[0]
 			index := c.Flag("index").Value.String()
 			indexInt, err := strconv.Atoi(index)
 			if err != nil {
@@ -105,23 +113,38 @@ var (
 	}
 
 	bundleBumpVersionCommand = &cobra.Command{
-		Use:   "bump",
+		Use:   "bump [bundle_path]",
 		Short: "Bump the version of the bundle",
-		Long:  "Bump the version of the bundle",
+		Long:  "Bump the version of the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
+			bundlePath := args[0]
 			targetVersion := c.Flag("target_version").Value.String()
 			bundle.BumpVersion(bundlePath, targetVersion)
 		},
 	}
 
 	bundlePackageCommand = &cobra.Command{
-		Use:   "package",
+		Use:   "package [bundle_path]",
 		Short: "Package the bundle",
-		Long:  "Package the bundle",
+		Long:  "Package the specified bundle",
+		Args:  cobra.ExactArgs(1),
 		Run: func(c *cobra.Command, args []string) {
-			bundlePath := c.Flag("bundle_path").Value.String()
-			outputPath := c.Flag("output_path").Value.String()
+			bundlePath := args[0]
+			// using filename of input_path as output_path if not specified
+			outputPath := ""
+
+			if c.Flag("output_path") != nil {
+				outputPath = c.Flag("output_path").Value.String()
+			} else {
+				base := filepath.Base(bundlePath)
+				if base == "." || base == "/" {
+					fmt.Println("Error: invalid input path, you should specify the path outside of bundle directory")
+					return
+				}
+				outputPath = base + ".difybndl"
+			}
+
 			bundle.PackageBundle(bundlePath, outputPath)
 		},
 	}
@@ -140,35 +163,19 @@ func init() {
 	bundleCommand.AddCommand(bundleAnalyzeCommand)
 
 	bundleAppendGithubDependencyCommand.Flags().StringP("repo_pattern", "r", "", "github repo pattern")
-	bundleAppendGithubDependencyCommand.Flags().StringP("bundle_path", "i", "", "path to the bundle file")
 	bundleAppendGithubDependencyCommand.MarkFlagRequired("repo_pattern")
-	bundleAppendGithubDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleAppendMarketplaceDependencyCommand.Flags().StringP("marketplace_pattern", "m", "", "marketplace pattern")
-	bundleAppendMarketplaceDependencyCommand.Flags().StringP("bundle_path", "i", "", "path to the bundle file")
 	bundleAppendMarketplaceDependencyCommand.MarkFlagRequired("marketplace_pattern")
-	bundleAppendMarketplaceDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleAppendPackageDependencyCommand.Flags().StringP("package_path", "p", "", "path to the package")
-	bundleAppendPackageDependencyCommand.Flags().StringP("bundle_path", "i", "", "path to the bundle file")
 	bundleAppendPackageDependencyCommand.MarkFlagRequired("package_path")
-	bundleAppendPackageDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleRemoveDependencyCommand.Flags().StringP("index", "i", "", "index of the dependency")
-	bundleRemoveDependencyCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
 	bundleRemoveDependencyCommand.MarkFlagRequired("index")
-	bundleRemoveDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleBumpVersionCommand.Flags().StringP("target_version", "t", "", "target version")
-	bundleBumpVersionCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
 	bundleBumpVersionCommand.MarkFlagRequired("target_version")
-	bundleBumpVersionCommand.MarkFlagRequired("bundle_path")
 
 	bundlePackageCommand.Flags().StringP("output_path", "o", "", "output path")
-	bundlePackageCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
-	bundlePackageCommand.MarkFlagRequired("output_path")
-	bundlePackageCommand.MarkFlagRequired("bundle_path")
-
-	bundleAnalyzeCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
-	bundleAnalyzeCommand.MarkFlagRequired("bundle_path")
 }

+ 0 - 21
cmd/commandline/bundle/dep.go

@@ -116,27 +116,6 @@ func RemoveDependency(bundlePath string, index int) {
 	log.Info("Successfully removed dependency")
 }
 
-func PackageBundle(bundlePath string, outputPath string) {
-	packager, err := loadBundlePackager(bundlePath)
-	if err != nil {
-		log.Error("Failed to load bundle packager: %v", err)
-		return
-	}
-
-	zipFile, err := packager.Export()
-	if err != nil {
-		log.Error("Failed to export bundle: %v", err)
-		return
-	}
-
-	if err := os.WriteFile(outputPath, zipFile, 0644); err != nil {
-		log.Error("Failed to write zip file: %v", err)
-		return
-	}
-
-	log.Info("Successfully packaged bundle")
-}
-
 func ListDependencies(bundlePath string) {
 	packager, err := loadBundlePackager(bundlePath)
 	if err != nil {

+ 20 - 0
cmd/commandline/bundle/encoder.go

@@ -0,0 +1,20 @@
+package bundle
+
+import (
+	"bytes"
+
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+	"gopkg.in/yaml.v3"
+)
+
+func marshalYamlBytes(v any) []byte {
+	buf := bytes.NewBuffer([]byte{})
+	encoder := yaml.NewEncoder(buf)
+	encoder.SetIndent(2)
+	err := encoder.Encode(v)
+	if err != nil {
+		log.Error("failed to marshal yaml: %s", err)
+		return nil
+	}
+	return buf.Bytes()
+}

+ 1 - 2
cmd/commandline/bundle/init.go

@@ -10,7 +10,6 @@ import (
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/manifest_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
-	"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
 
 	tea "github.com/charmbracelet/bubbletea"
 )
@@ -75,7 +74,7 @@ func InitBundle() {
 	}()
 
 	// save
-	bundleYaml := parser.MarshalYamlBytes(bundle)
+	bundleYaml := marshalYamlBytes(bundle)
 	if err := os.WriteFile(path.Join(bundleDir, "manifest.yaml"), bundleYaml, 0644); err != nil {
 		log.Error("Error saving manifest.yaml: %v", err)
 		return

+ 28 - 0
cmd/commandline/bundle/package.go

@@ -0,0 +1,28 @@
+package bundle
+
+import (
+	"os"
+
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+)
+
+func PackageBundle(bundlePath string, outputPath string) {
+	packager, err := loadBundlePackager(bundlePath)
+	if err != nil {
+		log.Error("Failed to load bundle packager: %v", err)
+		return
+	}
+
+	zipFile, err := packager.Export()
+	if err != nil {
+		log.Error("Failed to export bundle: %v", err)
+		return
+	}
+
+	if err := os.WriteFile(outputPath, zipFile, 0644); err != nil {
+		log.Error("Failed to write zip file: %v", err)
+		return
+	}
+
+	log.Info("Successfully packaged bundle")
+}

+ 10 - 0
cmd/commandline/main.go

@@ -28,6 +28,15 @@ var (
 		Short: "Bundle",
 		Long:  "Bundle related commands",
 	}
+
+	versionCommand = &cobra.Command{
+		Use:   "version",
+		Short: "Version",
+		Long:  "Show the version of dify cli",
+		Run: func(cmd *cobra.Command, args []string) {
+			fmt.Println(VersionX)
+		},
+	}
 )
 
 func init() {
@@ -36,6 +45,7 @@ func init() {
 	rootCommand.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dify.yaml)")
 	rootCommand.AddCommand(pluginCommand)
 	rootCommand.AddCommand(bundleCommand)
+	rootCommand.AddCommand(versionCommand)
 }
 
 func initConfig() {

+ 9 - 10
cmd/commandline/plugin.go

@@ -22,19 +22,22 @@ var (
 		Use:   "package plugin_path [-o output_path]",
 		Short: "Package",
 		Long:  "Package plugins",
+		Args:  cobra.ExactArgs(1),
 		Run: func(cmd *cobra.Command, args []string) {
-			if len(args) < 1 {
-				fmt.Println("Error: plugin_path is required")
-				return
-			}
 			inputPath := args[0]
+
 			// using filename of input_path as output_path if not specified
 			outputPath := ""
 
 			if cmd.Flag("output_path") != nil {
 				outputPath = cmd.Flag("output_path").Value.String()
 			} else {
-				outputPath = filepath.Base(inputPath) + ".difypkg"
+				base := filepath.Base(inputPath)
+				if base == "." || base == "/" {
+					fmt.Println("Error: invalid input path, you should specify the path outside of plugin directory")
+					return
+				}
+				outputPath = base + ".difypkg"
 			}
 
 			plugin.PackagePlugin(inputPath, outputPath)
@@ -45,12 +48,8 @@ var (
 		Use:   "checksum plugin_path",
 		Short: "Checksum",
 		Long:  "Calculate the checksum of the plugin, you need specify the plugin path or .difypkg file path",
+		Args:  cobra.ExactArgs(1),
 		Run: func(cmd *cobra.Command, args []string) {
-			if len(args) < 1 {
-				fmt.Println("Error: plugin_path is required")
-				return
-			}
-
 			pluginPath := args[0]
 			plugin.CalculateChecksum(pluginPath)
 		},

+ 5 - 0
cmd/commandline/version.go

@@ -0,0 +1,5 @@
+package main
+
+// VersionX is the version of dify cli
+// it will be replaced by build script in CI/CD pipeline, do not change it manually
+var VersionX = "REPLACE_ME"