Skip to content

Commit

Permalink
feat(ui): shell completion for envs and apps (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzduchardt authored Feb 18, 2024
1 parent c017fc5 commit ef03b96
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var allCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
RunAllCmd()
},
ValidArgsFunction: shellCompletion,
}

func RunAllCmd() {
Expand Down
1 change: 1 addition & 0 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ func newPluginCmd(plugin myks.Plugin) *cobra.Command {

return nil
},
ValidArgsFunction: shellCompletion,
}
}
1 change: 1 addition & 0 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ var renderCmd = &cobra.Command{
}
}
},
ValidArgsFunction: shellCompletion,
}
1 change: 1 addition & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ For example, if you reference a secret named "mycreds" in your vendir.yaml, you
log.Fatal().Err(err).Msg("Unable to sync vendir configs")
}
},
ValidArgsFunction: shellCompletion,
}

return cmd
Expand Down
47 changes: 47 additions & 0 deletions cmd/util.go
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{}
}
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -343,8 +341,6 @@ golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 h1:/RIbNt/Zr7rVhIkQhooTxCxFcdWLGIKnZA4IXNFSrvo=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE=
Expand Down Expand Up @@ -407,15 +403,11 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
8 changes: 8 additions & 0 deletions internal/myks/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,11 @@ func (e *Environment) yttS(purpose string, paths []string, stdin io.Reader, args
}
}, args...)
}

func (e *Environment) GetApplicationNames() []string {
var appNames []string
for _, app := range e.Applications {
appNames = append(appNames, app.Name)
}
return appNames
}
23 changes: 21 additions & 2 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (g *Globe) ValidateRootDir() error {
}

func (g *Globe) Init(asyncLevel int, envSearchPathToAppMap EnvAppMap) error {
envAppMap := g.collectEnvironments(envSearchPathToAppMap)
envAppMap := g.collectEnvironments(g.AddBaseDirToEnvAppMap(envSearchPathToAppMap))

return process(asyncLevel, maps.Keys(envAppMap), func(item interface{}) error {
envPath, ok := item.(string)
Expand Down Expand Up @@ -372,7 +372,7 @@ func (g *Globe) collectEnvironmentsInPath(searchPath string) []string {
return result
}

func (g Globe) isEnvPath(path string) bool {
func (g *Globe) isEnvPath(path string) bool {
for envPath := range g.environments {
if strings.HasPrefix(envPath, path) {
return true
Expand All @@ -398,3 +398,22 @@ func (g *Globe) getSyncTools() []SyncTool {
}
return syncTools
}

func (g *Globe) GetEnvs() map[string]*Environment {
return g.environments
}

func (g *Globe) AddBaseDirToEnvAppMap(envSearchPathToAppMap EnvAppMap) EnvAppMap {
envAppMap := EnvAppMap{}
for envPath, val := range envSearchPathToAppMap {
envAppMap[g.AddBaseDirToEnvPath(envPath)] = val
}
return envAppMap
}

func (g *Globe) AddBaseDirToEnvPath(envName string) string {
if strings.HasPrefix(envName, g.EnvironmentBaseDir+string(filepath.Separator)) {
return envName
}
return filepath.Join(g.EnvironmentBaseDir, envName)
}

0 comments on commit ef03b96

Please sign in to comment.