未验证 提交 4abf6fd4 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Fix the bash completion error which caused by refactoring (#294)

* Fix the bash completion error which caused by refactoring

* Fix the failure test cases caused by list filter
上级 0f300927
...@@ -3,13 +3,14 @@ package cmd ...@@ -3,13 +3,14 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"go.uber.org/zap"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io" "io"
"net/http" "net/http"
"reflect" "reflect"
"strings" "strings"
"go.uber.org/zap"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client" "github.com/jenkins-zh/jenkins-cli/client"
...@@ -130,10 +131,6 @@ func (o *OutputOption) ListFilter(obj interface{}) interface{} { ...@@ -130,10 +131,6 @@ func (o *OutputOption) ListFilter(obj interface{}) interface{} {
// Match filter an item // Match filter an item
func (o *OutputOption) Match(item reflect.Value) bool { func (o *OutputOption) Match(item reflect.Value) bool {
if len(o.Filter) == 0 {
return true
}
for _, f := range o.Filter { for _, f := range o.Filter {
arr := strings.Split(f, "=") arr := strings.Split(f, "=")
if len(arr) < 2 { if len(arr) < 2 {
...@@ -144,12 +141,10 @@ func (o *OutputOption) Match(item reflect.Value) bool { ...@@ -144,12 +141,10 @@ func (o *OutputOption) Match(item reflect.Value) bool {
val := arr[1] val := arr[1]
if !strings.Contains(util.ReflectFieldValueAsString(item, key), val) { if !strings.Contains(util.ReflectFieldValueAsString(item, key), val) {
continue return false
} else {
return true
} }
} }
return false return true
} }
// GetLine returns the line of a table // GetLine returns the line of a table
......
...@@ -148,7 +148,7 @@ foo-1 ...@@ -148,7 +148,7 @@ foo-1
Expect(result).To(BeTrue()) Expect(result).To(BeTrue())
}) })
Context("invalid filter", func() { Context("ignore invalid filter", func() {
BeforeEach(func() { BeforeEach(func() {
outputOption = cmd.OutputOption{ outputOption = cmd.OutputOption{
Filter: []string{"Name"}, Filter: []string{"Name"},
...@@ -156,7 +156,7 @@ foo-1 ...@@ -156,7 +156,7 @@ foo-1
}) })
It("not matched", func() { It("not matched", func() {
Expect(result).To(BeFalse()) Expect(result).To(BeTrue())
}) })
}) })
}) })
......
...@@ -126,7 +126,7 @@ fake 1.0 true ...@@ -126,7 +126,7 @@ fake 1.0 true
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9") request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
rootCmd.SetArgs([]string{"plugin", "list", "fake", "--output", "yaml", "--filter", "HasUpdate=true", rootCmd.SetArgs([]string{"plugin", "list", "fake", "--output", "yaml", "--filter", "HasUpdate=true",
"--filter", "Name=fake", "--filter", "Enable=true", "--filter", "Active=true"}) "--filter", "ShortName=fake", "--filter", "Enable=true", "--filter", "Active=true"})
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
rootCmd.SetOutput(buf) rootCmd.SetOutput(buf)
......
...@@ -275,8 +275,8 @@ const ( ...@@ -275,8 +275,8 @@ const (
jcliBashCompletionFunc = `__plugin_name_parse_get() jcliBashCompletionFunc = `__plugin_name_parse_get()
{ {
local jcli_output out local jcli_output out
if jcli_output=$(jcli plugin list --filter hasUpdate --no-headers --filter name="$1" 2>/dev/null); then if jcli_output=$(jcli plugin list --filter HasUpdate=true --no-headers --filter ShortName="$1" 2>/dev/null); then
out=($(echo "${jcli_output}" | awk '{print $2}')) out=($(echo "${jcli_output}" | awk '{print $1}'))
COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
fi fi
} }
...@@ -293,7 +293,7 @@ __config_name_parse_get() ...@@ -293,7 +293,7 @@ __config_name_parse_get()
{ {
local jcli_output out local jcli_output out
if jcli_output=$(jcli config list --no-headers 2>/dev/null); then if jcli_output=$(jcli config list --no-headers 2>/dev/null); then
out=($(echo "${jcli_output}" | awk '{print $2}')) out=($(echo "${jcli_output}" | awk '{print $1}'))
COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
fi fi
} }
...@@ -309,7 +309,7 @@ __jcli_get_config_name() ...@@ -309,7 +309,7 @@ __jcli_get_config_name()
__job_name_parse_get() __job_name_parse_get()
{ {
local jcli_output out local jcli_output out
if jcli_output=$(jcli job search -o path "$cur" 2>/dev/null); then if jcli_output=$(jcli job search --columns URL --no-headers "$cur" 2>/dev/null); then
out=($(echo "${jcli_output}")) out=($(echo "${jcli_output}"))
COMPREPLY=( ${out} ) COMPREPLY=( ${out} )
fi fi
...@@ -323,6 +323,23 @@ __jcli_get_job_name() ...@@ -323,6 +323,23 @@ __jcli_get_job_name()
fi fi
} }
__computer_name_parse_get()
{
local jcli_output out
if jcli_output=$(jcli computer list --no-headers --columns DisplayName 2>/dev/null); then
out=($(echo "${jcli_output}"))
COMPREPLY=( ${out} )
fi
}
__jcli_get_computer_name()
{
__computer_name_parse_get
if [[ $? -eq 0 ]]; then
return 0
fi
}
__jcli_custom_func() { __jcli_custom_func() {
case ${last_command} in case ${last_command} in
jcli_plugin_upgrade | jcli_plugin_uninstall) jcli_plugin_upgrade | jcli_plugin_uninstall)
...@@ -333,6 +350,10 @@ __jcli_custom_func() { ...@@ -333,6 +350,10 @@ __jcli_custom_func() {
__jcli_get_config_name __jcli_get_config_name
return return
;; ;;
jcli_computer_delete | jcli_computer_delete | jcli_computer_launch)
__jcli_get_computer_name
return
;;
jcli_job_build | jcli_job_stop | jcli_job_log | jcli_job_delete | jcli_job_history | jcli_job_artifact | jcli_job_input) jcli_job_build | jcli_job_stop | jcli_job_log | jcli_job_delete | jcli_job_history | jcli_job_artifact | jcli_job_input)
__jcli_get_job_name __jcli_get_job_name
return return
......
...@@ -2,6 +2,7 @@ package cmd ...@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
...@@ -10,8 +11,6 @@ import ( ...@@ -10,8 +11,6 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"gopkg.in/yaml.v2"
"github.com/jenkins-zh/jenkins-cli/app/i18n" "github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -32,6 +31,7 @@ fi ...@@ -32,6 +31,7 @@ fi
if type -t __start_jcli >/dev/null; then true; else if type -t __start_jcli >/dev/null; then true; else
source <(jcli completion) source <(jcli completion)
fi fi
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
` `
zshRcFile = ` zshRcFile = `
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册