@@ -5,9 +5,7 @@ package cmd
55import (
66 "encoding/json"
77 "fmt"
8- "os"
98 "os/exec"
10- "path/filepath"
119
1210 "hydectl/internal/logger"
1311 "hydectl/internal/plugin"
@@ -26,16 +24,16 @@ var dispatchCmd = &cobra.Command{
2624 Short : "Dispatch a plugin command" ,
2725 Long : `Dispatch a plugin command by specifying the plugin name and arguments.` ,
2826 Run : func (cmd * cobra.Command , args []string ) {
29- if listPlugins {
30- scripts , err := plugin .LoadScripts (ScriptPaths )
31- if err != nil {
32- logger .Errorf ("Error loading scripts: %v" , err )
33- fmt .Printf ("Error loading scripts: %v\n " , err )
34- return
35- }
27+ scripts , err := plugin .FindAllScripts (ScriptPaths )
28+ if err != nil {
29+ logger .Errorf ("Error loading scripts: %v" , err )
30+ fmt .Printf ("Error loading scripts: %v\n " , err )
31+ return
32+ }
3633
34+ if listPlugins {
3735 fmt .Println ("Available Plugins:" )
38- for _ , script := range scripts {
36+ for script := range scripts {
3937 fmt .Println (script )
4038 }
4139 return
@@ -49,35 +47,8 @@ var dispatchCmd = &cobra.Command{
4947 pluginName := args [0 ]
5048 pluginArgs := args [1 :]
5149
52- // Filter out non-existent directories
53- existingScriptPaths := filterExistingPaths (ScriptPaths )
54-
55- var scriptPath string
56- for _ , dir := range existingScriptPaths {
57- path := filepath .Join (dir , pluginName )
58- if _ , err := os .Stat (path ); err == nil {
59- scriptPath = path
60- break
61- }
62- }
63-
64- if scriptPath == "" {
65- // Try to find the script with a known extension
66- for _ , dir := range existingScriptPaths {
67- for _ , ext := range []string {".sh" , ".py" } {
68- path := filepath .Join (dir , pluginName + ext )
69- if _ , err := os .Stat (path ); err == nil {
70- scriptPath = path
71- break
72- }
73- }
74- if scriptPath != "" {
75- break
76- }
77- }
78- }
79-
80- if scriptPath == "" {
50+ scriptPath , ok := scripts [pluginName ]
51+ if ! ok {
8152 logger .Infof ("Plugin %s does not exist." , pluginName )
8253 fmt .Printf ("Plugin %s does not exist.\n " , pluginName )
8354 return
@@ -90,18 +61,6 @@ var dispatchCmd = &cobra.Command{
9061 },
9162}
9263
93- func filterExistingPaths (paths []string ) []string {
94- var existingPaths []string
95- for _ , path := range paths {
96- if _ , err := os .Stat (path ); err == nil {
97- existingPaths = append (existingPaths , path )
98- } else {
99- logger .Debugf ("Directory does not exist: %s" , path )
100- }
101- }
102- return existingPaths
103- }
104-
10564var dynamicCommands []* cobra.Command
10665
10766func init () {
@@ -161,20 +120,14 @@ type ScriptUsage struct {
161120// Example function to dynamically add plugin commands
162121func AddPluginCommands () {
163122 logger .Debug ("Loading scripts for dynamic commands" )
164- scripts , err := plugin .LoadScripts (ScriptPaths )
123+ scripts , err := plugin .FindAllScripts (ScriptPaths )
165124 if err != nil {
166125 logger .Errorf ("Error loading scripts: %v" , err )
167126 return
168127 }
169128
170- for _ , script := range scripts {
129+ for script , scriptPath := range scripts {
171130 logger .Debugf ("Processing script: %s" , script )
172- scriptPath := findScriptPath (script )
173- if scriptPath == "" {
174- logger .Infof ("Plugin %s does not exist." , script )
175- continue
176- }
177-
178131 usage , err := getScriptUsage (scriptPath )
179132 if err != nil {
180133 logger .Errorf ("Error getting usage for script %s: %v" , script , err )
@@ -228,20 +181,3 @@ func getScriptUsage(scriptPath string) (*ScriptUsage, error) {
228181
229182 return & usage , nil
230183}
231-
232- func findScriptPath (pluginName string ) string {
233- existingScriptPaths := filterExistingPaths (ScriptPaths )
234- for _ , dir := range existingScriptPaths {
235- path := filepath .Join (dir , pluginName )
236- if _ , err := os .Stat (path ); err == nil {
237- return path
238- }
239- for _ , ext := range []string {".sh" , ".py" } {
240- path := filepath .Join (dir , pluginName + ext )
241- if _ , err := os .Stat (path ); err == nil {
242- return path
243- }
244- }
245- }
246- return ""
247- }
0 commit comments