Skip to content

Commit

Permalink
search plugin frontly
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Dec 27, 2023
1 parent e31bd7b commit 86c84ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
17 changes: 10 additions & 7 deletions cmd/gtctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ func main() {
panic(err)
}

if err := NewRootCommand().Execute(); err != nil {
if pm.ShouldRun(err) {
if err := pm.Run(os.Args[1:]); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
if pm.ShouldRun(os.Args[1]) {
if err = pm.Run(os.Args[1:]); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}

if err = NewRootCommand().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
10 changes: 6 additions & 4 deletions pkg/plugins/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"os/exec"
"path/filepath"
"strings"

fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

const (
Expand Down Expand Up @@ -66,9 +68,9 @@ func NewManager() (*Manager, error) {
}

// ShouldRun returns true whether you should run the plugin.
func (m *Manager) ShouldRun(err error) bool {
// The error is returned by cobra itself.
return strings.Contains(err.Error(), "unknown command")
func (m *Manager) ShouldRun(name string) bool {
_, err := m.searchPlugins(name)
return err == nil
}

// Run searches for the plugin and runs it.
Expand Down Expand Up @@ -102,7 +104,7 @@ func (m *Manager) searchPlugins(name string) (string, error) {
pluginName := m.prefix + name
for _, path := range m.searchPaths {
pluginPath := filepath.Join(path, pluginName)
if _, err := os.Stat(pluginPath); os.IsNotExist(err) {
if exist, _ := fileutils.IsFileExists(pluginPath); !exist {
continue
}

Expand Down

0 comments on commit 86c84ca

Please sign in to comment.