-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from RobberPhex/use-cobra
make nerdctl.lima completion works
- Loading branch information
Showing
16 changed files
with
422 additions
and
380 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
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 |
---|---|---|
@@ -1,82 +1,14 @@ | ||
// Forked from https://github.com/containerd/nerdctl/blob/v0.8.1/completion.go | ||
|
||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/lima-vm/lima/pkg/store" | ||
"github.com/urfave/cli/v2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var completionCommand = &cli.Command{ | ||
Name: "completion", | ||
Usage: "Show shell completion", | ||
Subcommands: []*cli.Command{ | ||
completionBashCommand, | ||
}, | ||
} | ||
|
||
var completionBashCommand = &cli.Command{ | ||
Name: "bash", | ||
Usage: "Show bash completion (use with `source <(limactl completion bash)`)", | ||
Description: "Usage: add `source <(limactl completion bash)` to ~/.bash_profile", | ||
Action: completionBashAction, | ||
} | ||
|
||
func completionBashAction(clicontext *cli.Context) error { | ||
tmpl := `#!/bin/bash | ||
# Autocompletion enabler for limactl. | ||
# Usage: add 'source <(limactl completion bash)' to ~/.bash_profile | ||
# _limactl_bash_autocomplete is forked from https://github.com/urfave/cli/blob/v2.3.0/autocomplete/bash_autocomplete (MIT License) | ||
_limactl_bash_autocomplete() { | ||
if [[ "${COMP_WORDS[0]}" != "source" ]]; then | ||
local cur opts base | ||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
local args="${COMP_WORDS[@]:0:$COMP_CWORD}" | ||
# make {"limactl", "--foo", "=", "bar"} into {"limactl", "--foo=bar"} | ||
args="$(echo $args | sed -e 's/ = /=/g')" | ||
if [[ "$cur" == "-"* ]]; then | ||
opts=$( ${args} ${cur} --generate-bash-completion ) | ||
else | ||
opts=$( ${args} --generate-bash-completion ) | ||
fi | ||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | ||
return 0 | ||
fi | ||
} | ||
complete -o bashdefault -o default -o nospace -F _limactl_bash_autocomplete limactl | ||
` | ||
_, err := fmt.Fprint(clicontext.App.Writer, tmpl) | ||
return err | ||
} | ||
|
||
func bashCompleteInstanceNames(clicontext *cli.Context) { | ||
w := clicontext.App.Writer | ||
func bashCompleteInstanceNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { | ||
instances, err := store.Instances() | ||
if err != nil { | ||
return | ||
} | ||
for _, name := range instances { | ||
fmt.Fprintln(w, name) | ||
return nil, cobra.ShellCompDirectiveDefault | ||
} | ||
return instances, cobra.ShellCompDirectiveNoFileComp | ||
} |
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 |
---|---|---|
|
@@ -11,22 +11,30 @@ import ( | |
"github.com/lima-vm/lima/pkg/sshutil" | ||
"github.com/lima-vm/lima/pkg/store" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var copyCommand = &cli.Command{ | ||
Name: "copy", | ||
Aliases: []string{"cp"}, | ||
Usage: "Copy files between host and guest", | ||
Description: "Prefix guest filenames with the instance name and a colon.\nExample: limactl copy default:/etc/os-release .", | ||
ArgsUsage: "SOURCE ... TARGET", | ||
Action: copyAction, | ||
} | ||
var copyHelp = `Copy files between host and guest | ||
Prefix guest filenames with the instance name and a colon. | ||
Example: limactl copy default:/etc/os-release . | ||
` | ||
|
||
func copyAction(clicontext *cli.Context) error { | ||
if clicontext.NArg() < 2 { | ||
return fmt.Errorf("requires at least 2 arguments: SOURCE DEST") | ||
func newCopyCommand() *cobra.Command { | ||
var copyCommand = &cobra.Command{ | ||
Use: "copy SOURCE ... TARGET", | ||
Aliases: []string{"cp"}, | ||
Short: "Copy files between host and guest", | ||
Long: copyHelp, | ||
Args: cobra.MinimumNArgs(2), | ||
RunE: copyAction, | ||
} | ||
|
||
return copyCommand | ||
} | ||
|
||
func copyAction(cmd *cobra.Command, args []string) error { | ||
arg0, err := exec.LookPath("scp") | ||
if err != nil { | ||
return err | ||
|
@@ -37,12 +45,20 @@ func copyAction(clicontext *cli.Context) error { | |
} | ||
|
||
instDirs := make(map[string]string) | ||
args := []string{"-3", "--"} | ||
for _, arg := range clicontext.Args().Slice() { | ||
scpArgs := []string{} | ||
debug, err := cmd.Flags().GetBool("debug") | ||
if err != nil { | ||
return err | ||
} | ||
if debug { | ||
scpArgs = append(scpArgs, "-v") | ||
} | ||
scpArgs = append(scpArgs, "-3", "--") | ||
for _, arg := range args { | ||
path := strings.Split(arg, ":") | ||
switch len(path) { | ||
case 1: | ||
args = append(args, arg) | ||
scpArgs = append(scpArgs, arg) | ||
case 2: | ||
instName := path[0] | ||
inst, err := store.Inspect(instName) | ||
|
@@ -55,10 +71,10 @@ func copyAction(clicontext *cli.Context) error { | |
if inst.Status == store.StatusStopped { | ||
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName) | ||
} | ||
args = append(args, fmt.Sprintf("scp://%[email protected]:%d/%s", u.Username, inst.SSHLocalPort, path[1])) | ||
scpArgs = append(scpArgs, fmt.Sprintf("scp://%[email protected]:%d/%s", u.Username, inst.SSHLocalPort, path[1])) | ||
instDirs[instName] = inst.Dir | ||
default: | ||
return fmt.Errorf("Path %q contains multiple colons", arg) | ||
return fmt.Errorf("path %q contains multiple colons", arg) | ||
} | ||
} | ||
|
||
|
@@ -81,12 +97,12 @@ func copyAction(clicontext *cli.Context) error { | |
} | ||
} | ||
|
||
cmd := exec.Command(arg0, append(sshArgs, args...)...) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
logrus.Debugf("executing scp (may take a long time)): %+v", cmd.Args) | ||
sshCmd := exec.Command(arg0, append(sshArgs, scpArgs...)...) | ||
sshCmd.Stdin = cmd.InOrStdin() | ||
sshCmd.Stdout = cmd.OutOrStdout() | ||
sshCmd.Stderr = cmd.ErrOrStderr() | ||
logrus.Debugf("executing scp (may take a long time)): %+v", sshCmd.Args) | ||
|
||
// TODO: use syscall.Exec directly (results in losing tty?) | ||
return cmd.Run() | ||
return sshCmd.Run() | ||
} |
Oops, something went wrong.