Skip to content

Commit

Permalink
updated carapace to 0.1.0
Browse files Browse the repository at this point in the history
probably breaks some stuff due to changes to carapace

- moved actions to /actions
- updated multipart actions for new api
- fixed docker containers
- temporary disabled docker root subcommand completion (simply
additionally registering them to rootCmd breaks completion)
  • Loading branch information
rsteube committed Oct 5, 2020
1 parent f034400 commit f50054e
Show file tree
Hide file tree
Showing 28 changed files with 230 additions and 107 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ RUN wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod
&& dpkg -i packages-microsoft-prod.deb \
&& apt-get update

RUN apt-get install -y fish \
RUN apt-get install -y bash-completion \
fish \
elvish \
powershell \
python3-pip \
Expand All @@ -17,6 +18,7 @@ RUN ln -s /carapace-bin/carapace/carapace /usr/local/bin/carapace
# bash
RUN echo "\n\
PS1=$'\e[0;36mcarapace \e[0m'\n\
source /usr/share/bash-completion/bash_completion \n\
source <(carapace _carapace bash)\n\
for c in \$(carapace --list); do\n\
source <(carapace \$c)\n\
Expand Down Expand Up @@ -65,6 +67,7 @@ RUN curl https://www.oilshell.org/download/oil-0.8.0.tar.gz | tar -xvz \
RUN mkdir -p ~/.config/oil \
&& echo "\n\
PS1=$'\e[0;36mcarapace \e[0m'\n\
source /usr/share/bash-completion/bash_completion \n\
source <(carapace _carapace)\n\
for c in \$(carapace --list); do\n\
source <(carapace \$c)\n\
Expand Down
27 changes: 27 additions & 0 deletions actions/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fs

import (
"os/exec"
"regexp"
"strings"

"github.com/rsteube/carapace"
)

func ActionMounts() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("mount").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
re := regexp.MustCompile(`^(?P<target>\S+) on (?P<mountt>\S+) type (?P<type>\S+) (?P<mode>.+)`)
mounts := make([]string, 0)
for _, line := range strings.Split(string(output), "\n") {
if re.MatchString(line) {
matches := re.FindStringSubmatch(line)
mounts = append(mounts, matches[2], matches[1])
}
}
return carapace.ActionValuesDescribed(mounts...)
}
})
}
47 changes: 47 additions & 0 deletions actions/net/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package net

import (
"io/ioutil"
"os/exec"
"regexp"
"strings"

"github.com/mitchellh/go-homedir"

"github.com/rsteube/carapace"
)

func ActionHosts() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
hosts := []string{}
if file, err := homedir.Expand("~/.ssh/known_hosts"); err == nil {
if content, err := ioutil.ReadFile(file); err == nil {
r := regexp.MustCompile(`^(?P<host>[^ ,#]+)`)
for _, entry := range strings.Split(string(content), "\n") {
if r.MatchString(entry) {
hosts = append(hosts, r.FindStringSubmatch(entry)[0])
}
}
} else {
return carapace.ActionMessage(err.Error())
}
}
return carapace.ActionValues(hosts...)
})
}

func ActionNetInterfaces() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("ifconfig").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
interfaces := []string{}
for _, line := range strings.Split(string(output), "\n") {
if matches, _ := regexp.MatchString("^[0-9a-zA-Z]", line); matches {
interfaces = append(interfaces, strings.Split(line, ":")[0])
}
}
return carapace.ActionValues(interfaces...)
}
})
}
48 changes: 40 additions & 8 deletions actions/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package os
import (
"io/ioutil"
"os"
"os/exec"
"strings"

ps "github.com/mitchellh/go-ps"
Expand All @@ -26,13 +27,17 @@ func ActionGroups() carapace.Action {
groups := []string{}
if content, err := ioutil.ReadFile("/etc/group"); err == nil {
for _, entry := range strings.Split(string(content), "\n") {
group := strings.Split(entry, ":")[0]
if len(strings.TrimSpace(group)) > 0 {
groups = append(groups, group)
splitted := strings.Split(entry, ":")
if len(splitted) > 2 {
group := splitted[0]
id := splitted[2]
if len(strings.TrimSpace(group)) > 0 {
groups = append(groups, group, id)
}
}
}
}
return carapace.ActionValues(groups...)
return carapace.ActionValuesDescribed(groups...)
})
}

Expand Down Expand Up @@ -105,12 +110,39 @@ func ActionUsers() carapace.Action {
users := []string{}
if content, err := ioutil.ReadFile("/etc/passwd"); err == nil {
for _, entry := range strings.Split(string(content), "\n") {
user := strings.Split(entry, ":")[0]
if len(strings.TrimSpace(user)) > 0 {
users = append(users, user)
splitted := strings.Split(entry, ":")
if len(splitted) > 2 {
user := splitted[0]
id := splitted[2]
if len(strings.TrimSpace(user)) > 0 {
users = append(users, user, id)
}
}
}
}
return carapace.ActionValues(users...)
return carapace.ActionValuesDescribed(users...)
})
}

func ActionUserGroup() carapace.Action {
return carapace.ActionMultiParts(":", func(args []string, parts []string) carapace.Action {
switch len(parts) {
case 0:
return ActionUsers().Suffix(":", args)
case 1:
return ActionGroups()
default:
return carapace.ActionValues()
}
})
}

func ActionShells() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("chsh", "--list-shells").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return carapace.ActionValues(strings.Split(string(output), "\n")...)
}
})
}
3 changes: 2 additions & 1 deletion completers/chgrp_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func init() {
if rootCmd.Flag("reference").Changed {
return carapace.ActionFiles("")
} else {
return carapace.ActionGroups()
return os.ActionGroups()
}
}),
)
Expand Down
5 changes: 3 additions & 2 deletions completers/chown_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,7 +35,7 @@ func init() {
rootCmd.Flags().Bool("version", false, "output version information and exit")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"from": carapace.ActionUserGroup(),
"from": os.ActionUserGroup(),
"reference": carapace.ActionFiles(""),
})

Expand All @@ -43,7 +44,7 @@ func init() {
if rootCmd.Flag("reference").Changed {
return carapace.ActionFiles("")
} else {
return carapace.ActionUserGroup().Callback(args)
return os.ActionUserGroup().Callback(args)
}
}),
)
Expand Down
5 changes: 3 additions & 2 deletions completers/chroot_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/spf13/cobra"
)

Expand All @@ -24,7 +25,7 @@ func init() {
rootCmd.Flags().Bool("version", false, "output version information and exit")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"groups": carapace.ActionGroups(),
"userspec": carapace.ActionUserGroup(),
"groups": os.ActionGroups(),
"userspec": os.ActionUserGroup(),
})
}
14 changes: 2 additions & 12 deletions completers/chsh_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"os/exec"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/spf13/cobra"
)

Expand All @@ -26,14 +24,6 @@ func init() {
rootCmd.Flags().BoolP("version", "v", false, "display version")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"shell": ActionShells(),
"shell": os.ActionShells(),
})
}

func ActionShells() carapace.Action {
if output, err := exec.Command("chsh", "--list-shells").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return carapace.ActionValues(strings.Split(string(output), "\n")...)
}
}
3 changes: 2 additions & 1 deletion completers/docker-compose_completer/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/spf13/cobra"
)

Expand All @@ -22,7 +23,7 @@ func init() {
rootCmd.AddCommand(execCmd)

carapace.Gen(execCmd).FlagCompletion(carapace.ActionMap{
"user": carapace.ActionUsers(),
"user": os.ActionUsers(),
})

carapace.Gen(execCmd).PositionalCompletion(
Expand Down
3 changes: 2 additions & 1 deletion completers/docker-compose_completer/cmd/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/spf13/cobra"
)

Expand All @@ -18,6 +19,6 @@ func init() {
rootCmd.AddCommand(killCmd)

carapace.Gen(killCmd).FlagCompletion(carapace.ActionMap{
"s": carapace.ActionKillSignals(),
"s": os.ActionKillSignals(),
})
}
20 changes: 10 additions & 10 deletions completers/docker_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func ActionRepositories() carapace.Action {
}

func ActionRepositoryTags() carapace.Action {
return carapace.ActionMultiParts(":", func(args []string, parts []string) []string {
return carapace.ActionMultiParts(":", func(args []string, parts []string) carapace.Action {
if output, err := exec.Command("docker", "image", "ls", "--format", "{{.Repository}}:{{.Tag}}", "--filter", "dangling=false").Output(); err != nil {
return []string{}
return carapace.ActionMessage(err.Error())
} else {
// TODO add checks to not cause an out of bounds error
vals := strings.Split(string(output), "\n")
Expand All @@ -57,7 +57,7 @@ func ActionRepositoryTags() carapace.Action {
reposWithSuffix[index] = strings.SplitAfter(val, ":")[0]
}
}
return reposWithSuffix
return carapace.ActionValues(reposWithSuffix...)
case 1:
tags := make([]string, 0)
for _, val := range vals[:len(vals)-1] {
Expand All @@ -66,32 +66,32 @@ func ActionRepositoryTags() carapace.Action {
tags = append(tags, tag)
}
}
return tags
return carapace.ActionValues(tags...)
default:
return []string{}
return carapace.ActionValues()
}
}
})
}

// TODO not yet working - also needs multiple characters to split on `:` `/`
func ActionContainerPath() carapace.Action {
return carapace.ActionMultiParts(":", func(args []string, parts []string) []string {
return carapace.ActionMultiParts(":", func(args []string, parts []string) carapace.Action {
switch len(parts) {
case 0:
// TODO add description support
//if output, err := exec.Command("docker", "container", "ls", "--format", "{{.Names}}:\n{{.Image}} ({{.Status}})").Output(); err != nil {
if output, err := exec.Command("docker", "container", "ls", "--format", "{{.Names}}:").Output(); err != nil {
return []string{}
return carapace.ActionValues()
} else {
vals := strings.Split(string(output), "\n")
return vals[:len(vals)-1]
return carapace.ActionValues(vals[:len(vals)-1]...)
}
default:
if output, err := exec.Command("docker", "exec", parts[0], "ls", filepath.Dir(strings.Join(parts[1:], "/"))).Output(); err != nil {
return []string{}
return carapace.ActionValues()
} else {
return strings.Split(string(output), "\n")
return carapace.ActionValues(strings.Split(string(output), "\n")...)
}
}
})
Expand Down
5 changes: 3 additions & 2 deletions completers/docker_completer/cmd/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/rsteube/carapace-bin/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -115,13 +116,13 @@ func init() {
"cpu-shares": carapace.ActionValues("0", "10", "100", "200", "500", "800", "1000"),
"device": carapace.ActionFiles(""),
"env-file": carapace.ActionFiles(""),
"group-add": carapace.ActionGroups(),
"group-add": os.ActionGroups(),
"isolation": carapace.ActionValues("default", "hyperv", "process"),
"label-file": carapace.ActionFiles(""),
"log-driver": action.ActionLogDrivers(),
"network": carapace.ActionValues("bridge", "container", "host", "none"),
"pid": carapace.ActionValues("container", "host"),
"user": carapace.ActionUsers(),
"user": os.ActionUsers(),
})

carapace.Gen(container_createCmd).PositionalCompletion(
Expand Down
3 changes: 2 additions & 1 deletion completers/docker_completer/cmd/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/actions/os"
"github.com/rsteube/carapace-bin/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)
Expand All @@ -27,7 +28,7 @@ func init() {

carapace.Gen(container_execCmd).FlagCompletion(carapace.ActionMap{
"detach-keys": action.ActionDetachKeys(),
"user": carapace.ActionUserGroup(),
"user": os.ActionUserGroup(),
})

carapace.Gen(container_execCmd).PositionalCompletion(
Expand Down
Loading

0 comments on commit f50054e

Please sign in to comment.