This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
completion.go
161 lines (141 loc) · 3.99 KB
/
completion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2017 The rkt 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 (
"io"
"os"
"github.com/spf13/cobra"
)
var (
cmdCompletion = &cobra.Command{
Use: "completion SHELL",
Short: "Output shell completion code for the specified shell",
Long: `This command outputs completion code for the specified shell. The generated
code must be evaluated to provide interactive completion of rkt sub-commands.
This can be done by sourcing it from the .bash_profile.
Save completion code in a home directory and then include it in .bash_profile
script:
$ rkt completion bash > $HOME/.rkt.bash.inc
$ printf '
# rkt shell completion
source "$HOME/.rkt.bash.inc"
' >> $HOME/.bash_profile
$ source $HOME/.bash_profile
Alternatively, include the completion code directly into the launched shell:
$ source <(rkt completion bash)`,
ValidArgs: []string{"bash"},
Run: runWrapper(newCompletion(os.Stdout)),
}
bashCompletionFunc = `__rkt_parse_image()
{
local rkt_output
if rkt_output=$(rkt image list --no-legend 2>/dev/null); then
out=($(echo "${rkt_output}" | awk '{print $1}'))
COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
fi
}
__rkt_parse_list()
{
local rkt_output
if rkt_output=$(rkt list --no-legend 2>/dev/null); then
if [[ -n "$1" ]]; then
out=($(echo "${rkt_output}" | grep ${1} | awk '{print $1}'))
else
out=($(echo "${rkt_output}" | awk '{print $1}'))
fi
COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
fi
}
__custom_func() {
case ${last_command} in
rkt_image_export | \
rkt_image_extract | \
rkt_image_cat-manifest | \
rkt_image_render | \
rkt_image_rm | \
rkt_run | \
rkt_prepare)
__rkt_parse_image
return
;;
rkt_run-prepared)
__rkt_parse_list prepared
return
;;
rkt_enter)
__rkt_parse_list running
return
;;
rkt_rm)
__rkt_parse_list "prepare\|exited"
return
;;
rkt_status)
__rkt_parse_list
return
;;
*)
;;
esac
}
`
completionShells = map[string]func(io.Writer, *cobra.Command) error{
"bash": runBashCompletion,
}
)
func init() {
cmdRkt.AddCommand(cmdCompletion)
}
// newCompletion creates a new command with a bounded writer. Writer
// is used to print the generated shell-completion script, which is
// intended to be consumed by the CLI users.
func newCompletion(w io.Writer) func(*cobra.Command, []string) int {
return func(cmd *cobra.Command, args []string) int {
return runCompletion(w, cmd, args)
}
}
// runCompletion is a command handler to generate the shell script with
// shell completion functions.
//
// It ensures that there are enough arguments to generate the completion
// scripts.
func runCompletion(w io.Writer, cmd *cobra.Command, args []string) (exit int) {
if len(args) == 0 {
stderr.Print("shell type is not specified")
return 254
}
if len(args) > 1 {
stderr.Print("too many arguments, only shell type is expected")
return 254
}
// Right now only bash completion is supported, but zsh could be
// supported in a future as well.
completion, ok := completionShells[args[0]]
if !ok {
stderr.Printf("'%s' shell is not supported", args[0])
return 254
}
// Write the shell completion to the specified writer.
err := completion(w, cmd.Parent())
if err != nil {
stderr.PrintE("completion failed", err)
return 254
}
return 0
}
// runBashCompletion generates bash completion script by delegating
// this responsibility to the cobra package itself.
func runBashCompletion(out io.Writer, cmd *cobra.Command) error {
return cmd.GenBashCompletion(out)
}