Skip to content

Commit 3e93a3e

Browse files
FEATURE: add autocomplete (#857)
1 parent d87acd4 commit 3e93a3e

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

launcher_go/v2/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"github.com/alecthomas/kong"
77
"github.com/discourse/discourse_docker/launcher_go/v2/utils"
8+
"github.com/posener/complete"
9+
"github.com/willabides/kongplete"
810
"golang.org/x/sys/unix"
911
"os"
1012
"os/exec"
@@ -30,14 +32,26 @@ type Cli struct {
3032
StopCmd StopCmd `cmd:"" name:"stop" help:"Stops container."`
3133
RestartCmd RestartCmd `cmd:"" name:"restart" help:"Stops then starts container."`
3234
RebuildCmd RebuildCmd `cmd:"" name:"rebuild" help:"Builds new image, then destroys old container, and starts new container."`
35+
36+
InstallCompletions kongplete.InstallCompletions `cmd:"" aliases:"sh" help:"Print shell autocompletions. Add output to dotfiles, or 'source <(./launcher2 sh)'."`
3337
}
3438

3539
func main() {
3640
cli := Cli{}
3741
runCtx, cancel := context.WithCancel(context.Background())
3842

43+
// pre parse to get config dir for prediction of conf dir
44+
confFiles := utils.FindConfigNames()
45+
3946
parser := kong.Must(&cli, kong.UsageOnError(), kong.Bind(&runCtx), kong.Vars{"version": utils.Version})
4047

48+
// Run kongplete.Complete to handle completion requests
49+
kongplete.Complete(parser,
50+
kongplete.WithPredictor("config", complete.PredictSet(confFiles...)),
51+
kongplete.WithPredictor("file", complete.PredictFiles("*")),
52+
kongplete.WithPredictor("dir", complete.PredictDirs("*")),
53+
)
54+
4155
ctx, err := parser.Parse(os.Args[1:])
4256
parser.FatalIfErrorf(err)
4357

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"io/ioutil"
7+
"os"
8+
"strings"
9+
)
10+
11+
// Find config names for autocomplete, given the current --conf-dir argument.
12+
func FindConfigNames() []string {
13+
compLine := os.Getenv("COMP_LINE")
14+
flagLine := []string{}
15+
found := false
16+
// the flag package wants a valid flag first
17+
// drop all COMP_LINE args until we find something that starts with --conf-dir
18+
for _, s := range strings.Fields(compLine) {
19+
if found {
20+
flagLine = append(flagLine, s)
21+
}
22+
if strings.HasPrefix(s, "--conf-dir") {
23+
flagLine = append(flagLine, s)
24+
found = true
25+
}
26+
}
27+
flags := flag.NewFlagSet("f", flag.ContinueOnError)
28+
//squelch helptext
29+
flags.SetOutput(&bytes.Buffer{})
30+
confDirArg := flags.String("conf-dir", "./containers", "conf dir")
31+
flags.Parse(flagLine)
32+
33+
// search in the current conf dir for any files
34+
confDir := strings.TrimRight(*confDirArg, "/") + "/"
35+
confFiles := []string{}
36+
files, err := ioutil.ReadDir(confDir)
37+
if err == nil {
38+
for _, file := range files {
39+
if !file.IsDir() {
40+
if strings.HasSuffix(file.Name(), ".yml") {
41+
confName, _ := strings.CutSuffix(file.Name(), ".yml")
42+
confFiles = append(confFiles, confName)
43+
} else if strings.HasSuffix(file.Name(), ".yaml") {
44+
confName, _ := strings.CutSuffix(file.Name(), ".yaml")
45+
confFiles = append(confFiles, confName)
46+
}
47+
}
48+
}
49+
}
50+
return confFiles
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package utils_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
7+
"github.com/discourse/discourse_docker/launcher_go/v2/utils"
8+
"os"
9+
)
10+
11+
var _ = Describe("FindConfig", func() {
12+
13+
It("Parses and returns yml or yaml files", func() {
14+
os.Setenv("COMP_LINE", "launcher2 build --conf-dir ../test/containers")
15+
Expect(utils.FindConfigNames()).To(ContainElements("test", "test2"))
16+
})
17+
18+
It("Parses and returns yml or yaml files with trailing slash", func() {
19+
os.Setenv("COMP_LINE", "launcher2 build --conf-dir ../test/containers/")
20+
Expect(utils.FindConfigNames()).To(ContainElements("test", "test2"))
21+
})
22+
23+
It("Parses and returns yml or yaml files on equals", func() {
24+
os.Setenv("COMP_LINE", "launcher2 --conf-dir=../test/containers other args")
25+
Expect(utils.FindConfigNames()).To(ContainElements("test", "test2"))
26+
})
27+
28+
It("doesn't error when dir does not exist when set", func() {
29+
os.Setenv("COMP_LINE", "launcher2 --conf-dir=./does-not-exist")
30+
Expect(utils.FindConfigNames()).To(BeEmpty())
31+
})
32+
33+
It("doesn't error when dir does not exist", func() {
34+
//by default it look is in ./containers directory, which does not exist
35+
// in this directory
36+
os.Setenv("COMP_LINE", "launcher2")
37+
Expect(utils.FindConfigNames()).To(BeEmpty())
38+
})
39+
})

0 commit comments

Comments
 (0)