-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
432 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/mykso/myks/internal/myks" | ||
) | ||
|
||
const pluginPrefix = "myks-" | ||
|
||
// findPlugins searches for plugins in the PATH and in configured plugin-sources | ||
func findPlugins() []myks.Plugin { | ||
path := filepath.SplitList(os.Getenv("PATH")) | ||
pluginsPath := myks.FindPluginsInPaths(path, pluginPrefix) | ||
|
||
sources := viper.GetStringSlice("plugin-sources") | ||
for i, source := range sources { | ||
sources[i] = os.ExpandEnv(source) // supports $HOME but not ~ | ||
} | ||
pluginsLocal := myks.FindPluginsInPaths(sources, "") | ||
|
||
return append(pluginsPath, pluginsLocal...) | ||
} | ||
|
||
func addPlugins(cmd *cobra.Command) { | ||
plugins := findPlugins() | ||
|
||
uniquePlugins := make(map[string]myks.Plugin) | ||
for _, plugin := range plugins { | ||
uniquePlugins[plugin.Name()] = plugin | ||
} | ||
|
||
if len(uniquePlugins) > 0 { | ||
cmd.AddGroup(&cobra.Group{ID: "Plugins", Title: "Plugin Subcommands:"}) | ||
} | ||
|
||
for _, plugin := range uniquePlugins { | ||
cmd.AddCommand(newPluginCmd(plugin)) | ||
} | ||
} | ||
|
||
func newPluginCmd(plugin myks.Plugin) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: plugin.Name(), | ||
Short: "Execute " + plugin.Name(), | ||
Long: "Execute" + plugin.Name(), | ||
GroupID: "Plugins", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
splitAt := cmd.ArgsLenAtDash() | ||
if splitAt == -1 { | ||
splitAt = len(args) | ||
} | ||
myksArgs, pluginArgs := args[:splitAt], args[splitAt:] | ||
|
||
if err := initTargetEnvsAndApps(cmd, myksArgs); err != nil { | ||
return err | ||
} | ||
g := myks.New(".") | ||
|
||
if err := g.ValidateRootDir(); err != nil { | ||
log.Fatal().Err(err).Msg("Root directory is not suitable for myks") | ||
return err | ||
} | ||
|
||
if err := g.Init(asyncLevel, envAppMap); err != nil { | ||
log.Fatal().Err(err).Msg("Unable to initialize globe") | ||
return err | ||
} | ||
|
||
if err := g.ExecPlugin(asyncLevel, plugin, pluginArgs); err != nil { | ||
log.Fatal().Err(err).Msg("Unable to render applications") | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.