Skip to content

Commit

Permalink
cli: add shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
WanzenBug committed Sep 17, 2021
1 parent e904b8c commit 0e8a4eb
Show file tree
Hide file tree
Showing 26 changed files with 301 additions and 16 deletions.
27 changes: 27 additions & 0 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,30 @@ func (m *mpbProgress) NewBar(name, operation string, total int64) *mpb.Bar {
mpb.AppendDecorators(decor.CountersKibiByte("%.2f / %.2f")),
)
}

func suggestImageNames(cmd *cobra.Command, args []string, tocomplete string) ([]string, cobra.ShellCompDirective) {
v, err := InitVirter()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
defer v.ForceDisconnect()

images, err := v.ImageList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

filtered := make([]string, 0, len(images))
outer:
for _, image := range images {
for _, arg := range args {
if arg == image.Name() {
continue outer
}
}

filtered = append(filtered, image.Name())
}

return filtered, cobra.ShellCompDirectiveNoFileComp
}
8 changes: 8 additions & 0 deletions cmd/image_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ func imageBuildCommand() *cobra.Command {

fmt.Printf("Built %s\n", newImageName)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) < 2 {
// Only suggest first and second argument
return suggestImageNames(cmd, args, toComplete)
}

return suggestNone(cmd, args, toComplete)
},
}

buildCmd.Flags().StringVarP(&provisionFile, "provision", "p", "", "name of toml file containing provisioning steps")
Expand Down
9 changes: 9 additions & 0 deletions cmd/image_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ read from stdin`,
p.Wait()
fmt.Printf("Loaded %s\n", image)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// Only suggest first argument
return suggestImageNames(cmd, args, toComplete)
}

// Allow files here
return nil, cobra.ShellCompDirectiveDefault
},
}

return load
Expand Down
1 change: 1 addition & 0 deletions cmd/image_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func imageLsCommand() *cobra.Command {
t.Print()
}
},
ValidArgsFunction: suggestNone,
}

lsCmd.Flags().BoolVar(&listHttp, "available", false, "List all images available from http registries")
Expand Down
1 change: 1 addition & 0 deletions cmd/image_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func imagePruneCommand() *cobra.Command {
}
}
},
ValidArgsFunction: suggestNone,
}

return pruneCmd
Expand Down
8 changes: 8 additions & 0 deletions cmd/image_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ will be used.`,

fmt.Printf("Pulled %s\n", image.Name())
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// Only suggest first argument
return suggestImageNames(cmd, args, toComplete)
}

return suggestNone(cmd, args, toComplete)
},
}

return pullCmd
Expand Down
8 changes: 8 additions & 0 deletions cmd/image_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func imagePushCommand() *cobra.Command {

fmt.Printf("Pushed %s\n", ref.Name())
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// Only suggest first argument
return suggestImageNames(cmd, args, toComplete)
}

return suggestNone(cmd, args, toComplete)
},
}

return pushCmd
Expand Down
1 change: 1 addition & 0 deletions cmd/image_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func imageRmCommand() *cobra.Command {
log.Fatal(errs)
}
},
ValidArgsFunction: suggestImageNames,
}

return rmCmd
Expand Down
9 changes: 9 additions & 0 deletions cmd/image_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ squashed into a single file.`,
p.Wait()
_, _ = fmt.Fprintf(os.Stderr, "Saved %s\n", imgRef.Name())
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// Only suggest first argument
return suggestImageNames(cmd, args, toComplete)
}

// Allow files here
return nil, cobra.ShellCompDirectiveDefault
},
}

return saveCmd
Expand Down
27 changes: 27 additions & 0 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ func networkCommand() *cobra.Command {
networkCmd.AddCommand(networkListAttachedCommand())
return networkCmd
}

func suggestNetworkNames(cmd *cobra.Command, args []string, tocomplete string) ([]string, cobra.ShellCompDirective) {
v, err := InitVirter()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
defer v.ForceDisconnect()

nets, err := v.NetworkList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

filtered := make([]string, 0, len(nets))
outer:
for _, net := range nets {
for _, arg := range args {
if arg == net.Name {
continue outer
}
}

filtered = append(filtered, net.Name)
}

return filtered, cobra.ShellCompDirectiveNoFileComp
}
1 change: 1 addition & 0 deletions cmd/network_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func networkAddCommand() *cobra.Command {
log.Fatal(err)
}
},
ValidArgsFunction: suggestNone,
}

addCmd.Flags().StringVarP(&forward, "forward-mode", "m", "", "Set the forward mode, for example 'nat'")
Expand Down
1 change: 1 addition & 0 deletions cmd/network_host_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func networkHostAddCommand() *cobra.Command {
}
}
},
ValidArgsFunction: suggestNone,
}

addCmd.Flags().UintVarP(&vmID, "id", "", 0, "ID which determines the MAC and IP addresses to associate")
Expand Down
1 change: 1 addition & 0 deletions cmd/network_host_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func networkHostRmCommand() *cobra.Command {
}
}
},
ValidArgsFunction: suggestNone,
}

rmCmd.Flags().UintVarP(&vmID, "id", "", 0, "ID which determines the host entry or entries to remove")
Expand Down
7 changes: 7 additions & 0 deletions cmd/network_list_attached.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func networkListAttachedCommand() *cobra.Command {
}
tbl.Print()
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return suggestNetworkNames(cmd, args, toComplete)
}

return suggestNone(cmd, args, toComplete)
},
}

return listAttachedCmd
Expand Down
6 changes: 4 additions & 2 deletions cmd/network_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"net"
"strings"

"github.com/rodaine/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -74,6 +75,7 @@ func networkLsCommand() *cobra.Command {

tbl.Print()
},
ValidArgsFunction: suggestNone,
}

return lsCmd
Expand Down
1 change: 1 addition & 0 deletions cmd/network_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func networkRmCommand() *cobra.Command {
log.Fatal(err)
}
},
ValidArgsFunction: suggestNetworkNames,
}
return rmCmd
}
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ func Execute() {
log.Fatal(err)
}
}

func suggestNone(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
}
28 changes: 28 additions & 0 deletions cmd/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"golang.org/x/crypto/ssh"

sshclient "github.com/LINBIT/gosshclient"

"github.com/LINBIT/virter/internal/virter"
)

Expand Down Expand Up @@ -48,3 +49,30 @@ func extraAuthorizedKeys() ([]string, error) {

return publicKeys, nil
}

func suggestVmNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
v, err := InitVirter()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
defer v.ForceDisconnect()

vms, err := v.ListVM()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

filtered := make([]string, 0, len(vms))
outer:
for _, vm := range vms {
for _, arg := range args {
if arg == vm {
continue outer
}
}

filtered = append(filtered, vm)
}

return filtered, cobra.ShellCompDirectiveNoFileComp
}
11 changes: 11 additions & 0 deletions cmd/vm_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ the virtual machine name if no other value is given.`,
log.Fatal(err)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return suggestVmNames(cmd, args, toComplete)
}

if len(args) == 1 {
return suggestImageNames(cmd, args, toComplete)
}

return suggestNone(cmd, args, toComplete)
},
}

commitCmd.Flags().BoolVarP(&shutdown, "shutdown", "s", false, "whether to shut the VM down and wait until it stops (default false)")
Expand Down
2 changes: 2 additions & 0 deletions cmd/vm_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/LINBIT/containerapi"

"github.com/LINBIT/virter/internal/virter"
"github.com/LINBIT/virter/pkg/netcopy"

Expand Down Expand Up @@ -34,6 +35,7 @@ func vmExecCommand() *cobra.Command {
log.Fatal(err)
}
},
ValidArgsFunction: suggestVmNames,
}

execCmd.Flags().StringVarP(&provisionFile, "provision", "p", "", "name of toml file containing provisioning steps")
Expand Down
1 change: 1 addition & 0 deletions cmd/vm_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func vmRmCommand() *cobra.Command {
log.Fatal(err)
}
},
ValidArgsFunction: suggestVmNames,
}

return rmCmd
Expand Down
7 changes: 7 additions & 0 deletions cmd/vm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ func vmRunCommand() *cobra.Command {
fmt.Println(name)
}
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return suggestImageNames(cmd, args, toComplete)
}

return suggestNone(cmd, args, toComplete)
},
}

runCmd.Flags().StringVarP(&vmName, "name", "n", "", "name of new VM")
Expand Down
1 change: 1 addition & 0 deletions cmd/vm_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func vmSSHCommand() *cobra.Command {
log.Fatal(err)
}
},
ValidArgsFunction: suggestVmNames,
}
return sshCmd
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ require (
github.com/rodaine/table v1.0.1
github.com/sethvargo/go-signalcontext v0.1.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
github.com/vbauerster/mpb/v7 v7.0.2
github.com/vektra/mockery v1.1.2
Expand Down
Loading

0 comments on commit 0e8a4eb

Please sign in to comment.