-
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.
feat(ui): shell completion for envs and apps (#240)
- Loading branch information
1 parent
c017fc5
commit ef03b96
Showing
8 changed files
with
80 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,5 +79,6 @@ func newPluginCmd(plugin myks.Plugin) *cobra.Command { | |
|
||
return nil | ||
}, | ||
ValidArgsFunction: shellCompletion, | ||
} | ||
} |
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 |
---|---|---|
|
@@ -36,4 +36,5 @@ var renderCmd = &cobra.Command{ | |
} | ||
} | ||
}, | ||
ValidArgsFunction: shellCompletion, | ||
} |
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,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/mykso/myks/internal/myks" | ||
) | ||
|
||
// shellCompletion provides shell completion for envs and apps selection | ||
func shellCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) > 1 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
tmp := myks.New(".") | ||
err := tmp.Init(asyncLevel, map[string][]string{}) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
// return envs | ||
if len(args) == 0 { | ||
return getEnvNames(tmp), cobra.ShellCompDirectiveNoFileComp | ||
} | ||
// return args | ||
if len(args) == 1 { | ||
return getAppNamesForEnv(tmp, args[0]), cobra.ShellCompDirectiveNoFileComp | ||
} | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
|
||
func getEnvNames(globe *myks.Globe) []string { | ||
var envNames []string | ||
for _, env := range globe.GetEnvs() { | ||
envNames = append(envNames, strings.TrimPrefix(env.Dir, globe.EnvironmentBaseDir+string(filepath.Separator))) | ||
} | ||
return envNames | ||
} | ||
|
||
func getAppNamesForEnv(globe *myks.Globe, envPath string) []string { | ||
env, ok := globe.GetEnvs()[globe.AddBaseDirToEnvPath(envPath)] | ||
if ok { | ||
return env.GetApplicationNames() | ||
} | ||
return []string{} | ||
} |
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