diff --git a/Gopkg.lock b/Gopkg.lock index 26e485e..d04e2ab 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -26,20 +26,33 @@ version = "v3.5.1" [[projects]] + branch = "master" name = "github.com/c-bata/go-prompt" - packages = ["."] - revision = "bd11e00806770dc3005ebb8ce120d65d4f453421" - version = "v0.2.0" + packages = [ + ".", + "completer" + ] + revision = "536e34532a12f0ec10fad595138094ed93ec03ce" [[projects]] branch = "master" name = "github.com/coreos/go-oidc" - packages = ["http","jose","key","oauth2","oidc"] + packages = [ + "http", + "jose", + "key", + "oauth2", + "oidc" + ] revision = "a4973d9a4225417aecf5d450a9522f00c1f7130f" [[projects]] name = "github.com/coreos/pkg" - packages = ["health","httputil","timeutil"] + packages = [ + "health", + "httputil", + "timeutil" + ] revision = "3ac0863d7acf3bc44daf49afef8919af12f704ef" version = "v3" @@ -51,13 +64,20 @@ [[projects]] name = "github.com/docker/distribution" - packages = ["digest","reference"] + packages = [ + "digest", + "reference" + ] revision = "48294d928ced5dd9b378f7fd7c6f5da3ff3f2c89" version = "v2.6.2" [[projects]] name = "github.com/emicklei/go-restful" - packages = [".","log","swagger"] + packages = [ + ".", + "log", + "swagger" + ] revision = "777bb3f19bcafe2575ffb2a3e46af92509ae9594" version = "v1.2" @@ -93,7 +113,10 @@ [[projects]] name = "github.com/gogo/protobuf" - packages = ["proto","sortkeys"] + packages = [ + "proto", + "sortkeys" + ] revision = "100ba4e885062801d56799d78530b73b178a78f3" version = "v0.4" @@ -142,7 +165,11 @@ [[projects]] branch = "master" name = "github.com/mailru/easyjson" - packages = ["buffer","jlexer","jwriter"] + packages = [ + "buffer", + "jlexer", + "jwriter" + ] revision = "2f5df55504ebc322e4d52d34df6a1f5b503bf26d" [[projects]] @@ -157,6 +184,12 @@ revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39" version = "v0.0.3" +[[projects]] + branch = "master" + name = "github.com/mattn/go-runewidth" + packages = ["."] + revision = "ce7b0b5c7b45a81508558cd1dba6bb1e4ddb51bb" + [[projects]] branch = "master" name = "github.com/mattn/go-tty" @@ -196,30 +229,68 @@ [[projects]] branch = "master" name = "golang.org/x/net" - packages = ["context","context/ctxhttp","http2","http2/hpack","idna","lex/httplex"] + packages = [ + "context", + "context/ctxhttp", + "http2", + "http2/hpack", + "idna", + "lex/httplex" + ] revision = "1c05540f6879653db88113bc4a2b70aec4bd491f" [[projects]] branch = "master" name = "golang.org/x/oauth2" - packages = [".","google","internal","jws","jwt"] + packages = [ + ".", + "google", + "internal", + "jws", + "jwt" + ] revision = "9a379c6b3e95a790ffc43293c2a78dee0d7b6e20" [[projects]] branch = "master" name = "golang.org/x/sys" - packages = ["unix","windows"] + packages = [ + "unix", + "windows" + ] revision = "07c182904dbd53199946ba614a412c61d3c548f5" [[projects]] branch = "master" name = "golang.org/x/text" - packages = ["internal/gen","internal/triegen","internal/ucd","secure/bidirule","transform","unicode/bidi","unicode/cldr","unicode/norm","unicode/rangetable","width"] + packages = [ + "internal/gen", + "internal/triegen", + "internal/ucd", + "secure/bidirule", + "transform", + "unicode/bidi", + "unicode/cldr", + "unicode/norm", + "unicode/rangetable", + "width" + ] revision = "e56139fd9c5bc7244c76116c68e500765bb6db6b" [[projects]] name = "google.golang.org/appengine" - packages = [".","internal","internal/app_identity","internal/base","internal/datastore","internal/log","internal/modules","internal/remote_api","internal/urlfetch","urlfetch"] + packages = [ + ".", + "internal", + "internal/app_identity", + "internal/base", + "internal/datastore", + "internal/log", + "internal/modules", + "internal/remote_api", + "internal/urlfetch", + "urlfetch" + ] revision = "150dc57a1b433e64154302bdc40b6bb8aefa313a" version = "v1.0.0" @@ -237,13 +308,124 @@ [[projects]] name = "k8s.io/client-go" - packages = ["discovery","kubernetes","kubernetes/typed/apps/v1beta1","kubernetes/typed/authentication/v1beta1","kubernetes/typed/authorization/v1beta1","kubernetes/typed/autoscaling/v1","kubernetes/typed/batch/v1","kubernetes/typed/batch/v2alpha1","kubernetes/typed/certificates/v1alpha1","kubernetes/typed/core/v1","kubernetes/typed/extensions/v1beta1","kubernetes/typed/policy/v1beta1","kubernetes/typed/rbac/v1alpha1","kubernetes/typed/storage/v1beta1","pkg/api","pkg/api/errors","pkg/api/install","pkg/api/meta","pkg/api/meta/metatypes","pkg/api/resource","pkg/api/unversioned","pkg/api/v1","pkg/api/validation/path","pkg/apimachinery","pkg/apimachinery/announced","pkg/apimachinery/registered","pkg/apis/apps","pkg/apis/apps/install","pkg/apis/apps/v1beta1","pkg/apis/authentication","pkg/apis/authentication/install","pkg/apis/authentication/v1beta1","pkg/apis/authorization","pkg/apis/authorization/install","pkg/apis/authorization/v1beta1","pkg/apis/autoscaling","pkg/apis/autoscaling/install","pkg/apis/autoscaling/v1","pkg/apis/batch","pkg/apis/batch/install","pkg/apis/batch/v1","pkg/apis/batch/v2alpha1","pkg/apis/certificates","pkg/apis/certificates/install","pkg/apis/certificates/v1alpha1","pkg/apis/extensions","pkg/apis/extensions/install","pkg/apis/extensions/v1beta1","pkg/apis/policy","pkg/apis/policy/install","pkg/apis/policy/v1beta1","pkg/apis/rbac","pkg/apis/rbac/install","pkg/apis/rbac/v1alpha1","pkg/apis/storage","pkg/apis/storage/install","pkg/apis/storage/v1beta1","pkg/auth/user","pkg/conversion","pkg/conversion/queryparams","pkg/fields","pkg/genericapiserver/openapi/common","pkg/labels","pkg/runtime","pkg/runtime/serializer","pkg/runtime/serializer/json","pkg/runtime/serializer/protobuf","pkg/runtime/serializer/recognizer","pkg/runtime/serializer/streaming","pkg/runtime/serializer/versioning","pkg/selection","pkg/third_party/forked/golang/reflect","pkg/third_party/forked/golang/template","pkg/types","pkg/util","pkg/util/cert","pkg/util/clock","pkg/util/errors","pkg/util/flowcontrol","pkg/util/framer","pkg/util/homedir","pkg/util/integer","pkg/util/intstr","pkg/util/json","pkg/util/jsonpath","pkg/util/labels","pkg/util/net","pkg/util/parsers","pkg/util/rand","pkg/util/runtime","pkg/util/sets","pkg/util/uuid","pkg/util/validation","pkg/util/validation/field","pkg/util/wait","pkg/util/yaml","pkg/version","pkg/watch","pkg/watch/versioned","plugin/pkg/client/auth","plugin/pkg/client/auth/gcp","plugin/pkg/client/auth/oidc","rest","tools/auth","tools/clientcmd","tools/clientcmd/api","tools/clientcmd/api/latest","tools/clientcmd/api/v1","tools/metrics","transport"] + packages = [ + "discovery", + "kubernetes", + "kubernetes/typed/apps/v1beta1", + "kubernetes/typed/authentication/v1beta1", + "kubernetes/typed/authorization/v1beta1", + "kubernetes/typed/autoscaling/v1", + "kubernetes/typed/batch/v1", + "kubernetes/typed/batch/v2alpha1", + "kubernetes/typed/certificates/v1alpha1", + "kubernetes/typed/core/v1", + "kubernetes/typed/extensions/v1beta1", + "kubernetes/typed/policy/v1beta1", + "kubernetes/typed/rbac/v1alpha1", + "kubernetes/typed/storage/v1beta1", + "pkg/api", + "pkg/api/errors", + "pkg/api/install", + "pkg/api/meta", + "pkg/api/meta/metatypes", + "pkg/api/resource", + "pkg/api/unversioned", + "pkg/api/v1", + "pkg/api/validation/path", + "pkg/apimachinery", + "pkg/apimachinery/announced", + "pkg/apimachinery/registered", + "pkg/apis/apps", + "pkg/apis/apps/install", + "pkg/apis/apps/v1beta1", + "pkg/apis/authentication", + "pkg/apis/authentication/install", + "pkg/apis/authentication/v1beta1", + "pkg/apis/authorization", + "pkg/apis/authorization/install", + "pkg/apis/authorization/v1beta1", + "pkg/apis/autoscaling", + "pkg/apis/autoscaling/install", + "pkg/apis/autoscaling/v1", + "pkg/apis/batch", + "pkg/apis/batch/install", + "pkg/apis/batch/v1", + "pkg/apis/batch/v2alpha1", + "pkg/apis/certificates", + "pkg/apis/certificates/install", + "pkg/apis/certificates/v1alpha1", + "pkg/apis/extensions", + "pkg/apis/extensions/install", + "pkg/apis/extensions/v1beta1", + "pkg/apis/policy", + "pkg/apis/policy/install", + "pkg/apis/policy/v1beta1", + "pkg/apis/rbac", + "pkg/apis/rbac/install", + "pkg/apis/rbac/v1alpha1", + "pkg/apis/storage", + "pkg/apis/storage/install", + "pkg/apis/storage/v1beta1", + "pkg/auth/user", + "pkg/conversion", + "pkg/conversion/queryparams", + "pkg/fields", + "pkg/genericapiserver/openapi/common", + "pkg/labels", + "pkg/runtime", + "pkg/runtime/serializer", + "pkg/runtime/serializer/json", + "pkg/runtime/serializer/protobuf", + "pkg/runtime/serializer/recognizer", + "pkg/runtime/serializer/streaming", + "pkg/runtime/serializer/versioning", + "pkg/selection", + "pkg/third_party/forked/golang/reflect", + "pkg/third_party/forked/golang/template", + "pkg/types", + "pkg/util", + "pkg/util/cert", + "pkg/util/clock", + "pkg/util/errors", + "pkg/util/flowcontrol", + "pkg/util/framer", + "pkg/util/homedir", + "pkg/util/integer", + "pkg/util/intstr", + "pkg/util/json", + "pkg/util/jsonpath", + "pkg/util/labels", + "pkg/util/net", + "pkg/util/parsers", + "pkg/util/rand", + "pkg/util/runtime", + "pkg/util/sets", + "pkg/util/uuid", + "pkg/util/validation", + "pkg/util/validation/field", + "pkg/util/wait", + "pkg/util/yaml", + "pkg/version", + "pkg/watch", + "pkg/watch/versioned", + "plugin/pkg/client/auth", + "plugin/pkg/client/auth/gcp", + "plugin/pkg/client/auth/oidc", + "rest", + "tools/auth", + "tools/clientcmd", + "tools/clientcmd/api", + "tools/clientcmd/api/latest", + "tools/clientcmd/api/v1", + "tools/metrics", + "transport" + ] revision = "e121606b0d09b2e1c467183ee46217fa85a6b672" version = "v2.0.0" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "3f1d0eda25d21776841ff0dfbe98c9ce47a60ba3223eb65324e175dabf330aca" + inputs-digest = "e77fe24aa2a5b94b387be1ab37aecb16b9cb14a2430be73aa86e6934e21f4438" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index d022a31..9b9c91f 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -23,7 +23,7 @@ [[constraint]] name = "github.com/c-bata/go-prompt" - version = "v0.2.0" + branch = "master" [[constraint]] name = "k8s.io/client-go" diff --git a/kube/option_arguments.go b/kube/option_arguments.go index e448732..b0e7104 100644 --- a/kube/option_arguments.go +++ b/kube/option_arguments.go @@ -1,16 +1,24 @@ package kube import ( - "io/ioutil" - "log" - "path/filepath" + "os" "strings" "github.com/c-bata/go-prompt" + "github.com/c-bata/go-prompt/completer" ) -func init() { - fileListCache = map[string][]prompt.Suggest{} +var yamlFileCompleter = completer.FilePathCompleter{ + IgnoreCase: true, + Filter: func(fi os.FileInfo) bool { + if fi.IsDir() { + return true + } + if strings.HasSuffix(fi.Name(), ".yaml") || strings.HasSuffix(fi.Name(), ".yml") { + return true + } + return false + }, } func getPreviousOption(d prompt.Document) (cmd, option string, found bool) { @@ -36,41 +44,10 @@ func completeOptionArguments(d prompt.Document) ([]prompt.Suggest, bool) { "label", "annotate", "scale", "convert", "autoscale", "top": switch option { case "-f", "--filename": - return fileCompleter(d), true + return yamlFileCompleter.Complete(d), true case "--namespace": return getNameSpaceSuggestions(), true } } return []prompt.Suggest{}, false } - -/* file list */ - -var fileListCache map[string][]prompt.Suggest - -func fileCompleter(d prompt.Document) []prompt.Suggest { - path := d.GetWordBeforeCursor() - if strings.HasPrefix(path, "./") { - path = path[2:] - } - dir := filepath.Dir(path) - if cached, ok := fileListCache[dir]; ok { - return cached - } - - files, err := ioutil.ReadDir(dir) - if err != nil { - log.Print("[ERROR] catch error " + err.Error()) - return []prompt.Suggest{} - } - suggests := make([]prompt.Suggest, 0, len(files)) - for _, f := range files { - if !f.IsDir() && - !strings.HasSuffix(f.Name(), ".yml") && - !strings.HasSuffix(f.Name(), ".yaml") { - continue - } - suggests = append(suggests, prompt.Suggest{Text: filepath.Join(dir, f.Name())}) - } - return prompt.FilterHasPrefix(suggests, path, false) -} diff --git a/main.go b/main.go index 672a805..5352ab8 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/c-bata/go-prompt" + "github.com/c-bata/go-prompt/completer" "github.com/c-bata/kube-prompt/kube" ) @@ -22,6 +23,7 @@ func main() { prompt.OptionTitle("kube-prompt: interactive kubernetes client"), prompt.OptionPrefix(">>> "), prompt.OptionInputTextColor(prompt.Yellow), + prompt.OptionCompletionWordSeparator(completer.FilePathCompletionSeparator), ) p.Run() }