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: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.claude
dist
node_modules
docs/package-lock.json
docs/package-lock.json
.omc
2 changes: 1 addition & 1 deletion cmd/jzero/internal/command/gen/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func Run() error {
}

// collectAndSaveMetadata 收集并保存项目元数据(复用已解析的数据)
func collectAndSaveMetadata(apiSpecMap map[string]*spec.ApiSpec, protoSpecMap map[string]rpcparser.Proto) error {
func collectAndSaveMetadata(apiSpecMap map[string]*spec.ApiSpec, protoSpecMap map[string]*rpcparser.Proto) error {
if len(apiSpecMap) == 0 && len(protoSpecMap) == 0 {
return nil
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/jzero/internal/command/gen/genmodel/genmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (jm *JzeroModel) Gen() error {
sqlFiles []string
genCodeSqlFiles []string
)
genCodeSqlSpecMap := make(map[string][]*ddlparser.Table)
genCodeSqlSpecMap := make(map[string]*ddlparser.Table)

if !config.C.Gen.ModelDatasource {
sqlFiles, err = jzerodesc.FindSqlFiles(config.C.SqlDir())
Expand Down Expand Up @@ -217,7 +217,7 @@ func (jm *JzeroModel) Gen() error {
}
mu.Lock()
defer mu.Unlock()
genCodeSqlSpecMap[f] = tableParsers
genCodeSqlSpecMap[f] = tableParsers[0]

bf := strings.TrimSuffix(filepath.Base(f), ".sql")

Expand Down Expand Up @@ -246,10 +246,8 @@ func (jm *JzeroModel) Gen() error {
eg.SetLimit(len(genCodeSqlFiles))
for _, f := range genCodeSqlFiles {
eg.Go(func() error {
tableParsers := genCodeSqlSpecMap[f]
for _, tp := range tableParsers {
genCodeTables = append(genCodeTables, tp.Name)
}
tableParser := genCodeSqlSpecMap[f]
genCodeTables = append(genCodeTables, tableParser.Name)
return generateModelFromSqlFile(f, goctlHome)
})
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/jzero/internal/command/gen/genrpc/genrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (l RegisterLines) String() string {
return "\n\t\t" + strings.Join(l, "\n\t\t")
}

func (jr *JzeroRpc) Gen() (map[string]rpcparser.Proto, error) {
func (jr *JzeroRpc) Gen() (map[string]*rpcparser.Proto, error) {
var (
serverImports ImportLines
pbImports ImportLines
Expand All @@ -58,7 +58,7 @@ func (jr *JzeroRpc) Gen() (map[string]rpcparser.Proto, error) {
return nil, nil
}

protoSpecMap := make(map[string]rpcparser.Proto, len(protoFiles))
protoSpecMap := make(map[string]*rpcparser.Proto, len(protoFiles))
for _, v := range protoFiles {
// parse proto
protoParser := rpcparser.NewDefaultProtoParser()
Expand All @@ -67,12 +67,12 @@ func (jr *JzeroRpc) Gen() (map[string]rpcparser.Proto, error) {
if err != nil {
return nil, err
}
protoSpecMap[v] = parse
protoSpecMap[v] = &parse
}

// 获取需要生成代码的proto 文件
var genCodeProtoFiles []string
genCodeProtoSpecMap := make(map[string]rpcparser.Proto, len(protoFiles))
genCodeProtoSpecMap := make(map[string]*rpcparser.Proto, len(protoFiles))

switch {
case config.C.Gen.GitChange && gitstatus.IsGitRepo(filepath.Join(config.C.Wd())) && len(config.C.Gen.Desc) == 0:
Expand Down
2 changes: 1 addition & 1 deletion cmd/jzero/internal/command/gen/genrpc/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type LogicFile struct {
ServerStream bool
}

func (jr *JzeroRpc) GetAllLogicFiles(descFilepath string, protoSpec rpcparser.Proto) ([]LogicFile, error) {
func (jr *JzeroRpc) GetAllLogicFiles(descFilepath string, protoSpec *rpcparser.Proto) ([]LogicFile, error) {
var logicFiles []LogicFile
for _, service := range protoSpec.Service {
for _, rpc := range service.RPC {
Expand Down
2 changes: 1 addition & 1 deletion cmd/jzero/internal/command/gen/genrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (jr *JzeroRpc) genServer(serverImports, pbImports ImportLines, registerServ
return nil
}

func (jr *JzeroRpc) GetAllServerFiles(descFilepath string, protoSpec rpcparser.Proto) ([]ServerFile, error) {
func (jr *JzeroRpc) GetAllServerFiles(descFilepath string, protoSpec *rpcparser.Proto) ([]ServerFile, error) {
var serverFiles []ServerFile
for _, service := range protoSpec.Service {
namingFormat, err := format.FileNamingFormat(config.C.Style, service.Name+"Server")
Expand Down
6 changes: 3 additions & 3 deletions cmd/jzero/internal/command/gen/genswagger/genswagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/jzero-io/jzero/cmd/jzero/internal/embeded"
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/osx"
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/stringx"
"github.com/jzero-io/jzero/cmd/jzero/internal/plugin"
"github.com/jzero-io/jzero/cmd/jzero/internal/serverless"
)

func Gen() (err error) {
Expand Down Expand Up @@ -58,7 +58,7 @@ func Gen() (err error) {
}

// 增加 plugins 的 api 文件
plugins, err := plugin.GetPlugins()
plugins, err := serverless.GetPlugins()
if err == nil {
for _, p := range plugins {
if pathx.FileExists(filepath.Join(p.Path, "desc", "api")) {
Expand Down Expand Up @@ -359,7 +359,7 @@ func Gen() (err error) {
}

// 增加 plugins 的 proto 文件
plugins, err := plugin.GetPlugins()
plugins, err := serverless.GetPlugins()
if err == nil {
for _, p := range plugins {
if pathx.FileExists(filepath.Join(p.Path, "desc", "proto")) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/jzero/internal/command/gen/genzrpcclient/genzrpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/mod"
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/osx"
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/templatex"
"github.com/jzero-io/jzero/cmd/jzero/internal/plugin"
"github.com/jzero-io/jzero/cmd/jzero/internal/serverless"
)

type DirContext struct {
Expand Down Expand Up @@ -152,7 +152,7 @@ func Generate(genModule bool) (err error) {
return err
}

plugins, _ := plugin.GetPlugins()
plugins, _ := serverless.GetPlugins()

excludeThirdPartyProtoFiles, err := desc.FindExcludeThirdPartyProtoFiles(config.C.ProtoDir())
if err != nil {
Expand Down Expand Up @@ -372,7 +372,7 @@ func Generate(genModule bool) (err error) {
return nil
}

func generatePluginFiles(plugins []plugin.Plugin) error {
func generatePluginFiles(plugins []serverless.Plugin) error {
if len(plugins) == 0 {
return nil
}
Expand Down Expand Up @@ -592,7 +592,7 @@ func generatePluginFiles(plugins []plugin.Plugin) error {
return nil
}

func genPluginNoRpcServiceExcludeThirdPartyProto(plugin plugin.Plugin, goModule, output string) error {
func genPluginNoRpcServiceExcludeThirdPartyProto(plugin serverless.Plugin, goModule, output string) error {
pluginProtoDir := filepath.Join(plugin.Path, "desc", "proto")
pluginThirdPartyProtoDir := filepath.Join(plugin.Path, "desc", "proto", "third_party")

Expand Down
60 changes: 60 additions & 0 deletions cmd/jzero/internal/command/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright © 2024 jaronnie <jaron@jaronnie.com>

*/

package plugin

import (
"github.com/spf13/cobra"

"github.com/jzero-io/jzero/cmd/jzero/internal/command/plugin/pluginlist"
"github.com/jzero-io/jzero/cmd/jzero/internal/command/plugin/pluginremove"
)

var (
// ValidPluginFilenamePrefixes defines the allowed plugin prefix to search
ValidPluginFilenamePrefixes = []string{"jzero"}
)

// pluginCmd represents the plugin command
var pluginCmd = &cobra.Command{
Use: "plugin",
Short: "Manage jzero plugins",
Long: `Provides utilities for interacting with plugins.
Plugins provide extended functionality that is not part of the major command-line distribution.
Plugins must be prefixed with "jzero-" and are stored in ~/.jzero/plugins.`,
}

var pluginListCmd = &cobra.Command{
Use: "list",
Short: "List all installed jzero plugins",
Long: `List all installed plugins from ~/.jzero/plugins directory.
Plugins must be prefixed with "jzero-" to be recognized.`,
RunE: func(cmd *cobra.Command, args []string) error {
return pluginlist.Run()
},
}

var pluginRemoveCmd = &cobra.Command{
Use: "remove <plugin-name>",
Short: "Remove an installed jzero plugin",
Long: `Remove an installed jzero plugin from ~/.jzero/plugins.

Example:
jzero plugin remove jzero-myplugin`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return pluginremove.Run(args[0])
},
}

func GetCommand() *cobra.Command {
pluginCmd.AddCommand(pluginListCmd)
pluginCmd.AddCommand(pluginRemoveCmd)

pluginListCmd.Flags().BoolVar(&pluginlist.NameOnly, "name-only", false, "If true, display only the binary name of each plugin, rather than its full path")
pluginRemoveCmd.Flags().BoolVar(&pluginremove.Force, "force", false, "Force removal without confirmation")

return pluginCmd
}
101 changes: 101 additions & 0 deletions cmd/jzero/internal/command/plugin/pluginlist/pluginlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright © 2024 jaronnie <jaron@jaronnie.com>

*/

package pluginlist

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"

"github.com/zeromicro/go-zero/core/logx"
)

var (
NameOnly bool
)

func Run() error {
homeDir, err := os.UserHomeDir()
if err != nil {
logx.Errorf("Failed to get home directory: %v", err)
return err
}

pluginDir := filepath.Join(homeDir, ".jzero", "plugins")
pluginsFound := false

// Check if plugin directory exists
if _, err := os.Stat(pluginDir); errors.Is(err, fs.ErrNotExist) {
fmt.Fprintf(os.Stderr, "Plugin directory %s does not exist. No plugins installed.\n", pluginDir)
return nil
}

// Collect plugins first
var plugins []string
err = filepath.WalkDir(pluginDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

// Skip directories
if d.IsDir() {
return nil
}

// Check if file has valid prefix
filename := filepath.Base(path)
if !hasValidPrefix(filename, []string{"jzero"}) {
return nil
}

plugins = append(plugins, path)
return nil
})

if err != nil {
logx.Errorf("Error walking plugin directory: %v", err)
return err
}

// Sort plugins
sort.Strings(plugins)

// Display plugins
if len(plugins) > 0 {
fmt.Fprint(os.Stderr, "The following jzero plugins are installed:\n\n")
pluginsFound = true

for _, pluginPath := range plugins {
filename := filepath.Base(pluginPath)
if NameOnly {
fmt.Fprintf(os.Stdout, "%s\n", filename)
} else {
fmt.Fprintf(os.Stdout, "%s\n", pluginPath)
}
}
}

if !pluginsFound {
fmt.Fprintf(os.Stderr, "No jzero plugins found in %s\n", pluginDir)
fmt.Fprint(os.Stderr, "\nPlugins must be prefixed with \"jzero-\" to be recognized.\n")
}

return nil
}

// hasValidPrefix checks if the filepath has a valid prefix
func hasValidPrefix(filepath string, validPrefixes []string) bool {
for _, prefix := range validPrefixes {
if strings.HasPrefix(filepath, prefix+"-") {
return true
}
}
return false
}
69 changes: 69 additions & 0 deletions cmd/jzero/internal/command/plugin/pluginremove/pluginremove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright © 2024 jaronnie <jaron@jaronnie.com>

*/

package pluginremove

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/zeromicro/go-zero/core/logx"
)

var (
Force bool
)

func Run(pluginName string) error {
// Validate plugin name
if !hasValidPrefix(pluginName, []string{"jzero"}) {
return fmt.Errorf("plugin name must start with \"jzero-\" prefix. Got: %s", pluginName)
}

homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get home directory: %w", err)
}

pluginPath := filepath.Join(homeDir, ".jzero", "plugins", pluginName)

// Check if plugin exists
if _, err := os.Stat(pluginPath); errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("plugin %s not found at %s", pluginName, pluginPath)
}

// Confirm removal
if !Force {
fmt.Fprintf(os.Stderr, "Are you sure you want to remove plugin %s? [y/N]: ", pluginName)
var response string
fmt.Scanln(&response)
if strings.ToLower(response) != "y" && strings.ToLower(response) != "yes" {
fmt.Fprint(os.Stderr, "Removal cancelled\n")
return nil
}
}

// Remove plugin
if err := os.Remove(pluginPath); err != nil {
return fmt.Errorf("failed to remove plugin: %w", err)
}

logx.Infof("Successfully removed plugin %s", pluginName)
return nil
}

// hasValidPrefix checks if the plugin name has a valid prefix
func hasValidPrefix(name string, validPrefixes []string) bool {
for _, prefix := range validPrefixes {
if strings.HasPrefix(name, prefix+"-") {
return true
}
}
return false
}
Loading
Loading