From 159b9bbde85a10826b1bcf4ef46fec9c732bd61c Mon Sep 17 00:00:00 2001 From: Bo Meng Date: Wed, 2 Sep 2020 10:51:30 +0800 Subject: [PATCH 1/3] new sub-command to show the plugins --- cmd/ocm/main.go | 9 ++- cmd/ocm/plugin/cmd.go | 34 +++++++++ cmd/ocm/plugin/list/cmd.go | 139 +++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 cmd/ocm/plugin/cmd.go create mode 100644 cmd/ocm/plugin/list/cmd.go diff --git a/cmd/ocm/main.go b/cmd/ocm/main.go index 25f89332..8e66e5d9 100644 --- a/cmd/ocm/main.go +++ b/cmd/ocm/main.go @@ -19,11 +19,12 @@ package main import ( "flag" "fmt" + "os" + "os/exec" + _ "github.com/golang/glog" "github.com/spf13/cobra" "github.com/spf13/pflag" - "os" - "os/exec" "github.com/openshift-online/ocm-cli/cmd/ocm/account" "github.com/openshift-online/ocm-cli/cmd/ocm/cluster" @@ -38,13 +39,14 @@ import ( "github.com/openshift-online/ocm-cli/cmd/ocm/login" "github.com/openshift-online/ocm-cli/cmd/ocm/logout" "github.com/openshift-online/ocm-cli/cmd/ocm/patch" + plugincmd "github.com/openshift-online/ocm-cli/cmd/ocm/plugin" "github.com/openshift-online/ocm-cli/cmd/ocm/post" "github.com/openshift-online/ocm-cli/cmd/ocm/token" "github.com/openshift-online/ocm-cli/cmd/ocm/tunnel" "github.com/openshift-online/ocm-cli/cmd/ocm/version" "github.com/openshift-online/ocm-cli/cmd/ocm/whoami" "github.com/openshift-online/ocm-cli/pkg/arguments" - "github.com/openshift-online/ocm-cli/pkg/plugin" + plugin "github.com/openshift-online/ocm-cli/pkg/plugin" ) var root = &cobra.Command{ @@ -88,6 +90,7 @@ func init() { root.AddCommand(completion.Cmd) root.AddCommand(whoami.Cmd) root.AddCommand(config.Cmd) + root.AddCommand(plugincmd.Cmd) } func main() { diff --git a/cmd/ocm/plugin/cmd.go b/cmd/ocm/plugin/cmd.go new file mode 100644 index 00000000..83cbffa5 --- /dev/null +++ b/cmd/ocm/plugin/cmd.go @@ -0,0 +1,34 @@ +/* +Copyright (c) 2020 Red Hat, Inc. + +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 plugin + +import ( + plugin "github.com/openshift-online/ocm-cli/cmd/ocm/plugin/list" + + "github.com/spf13/cobra" +) + +var Cmd = &cobra.Command{ + Use: "plugin COMMAND", + Short: "Get information about plugins", + Long: "Get information about installed ocm plugins", + Args: cobra.MinimumNArgs(1), +} + +func init() { + Cmd.AddCommand(plugin.Cmd) +} diff --git a/cmd/ocm/plugin/list/cmd.go b/cmd/ocm/plugin/list/cmd.go new file mode 100644 index 00000000..d626d9e9 --- /dev/null +++ b/cmd/ocm/plugin/list/cmd.go @@ -0,0 +1,139 @@ +/* +Copyright © 2020 NAME HERE + +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 plugin + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "runtime" + "strings" + + "github.com/openshift-online/ocm-cli/pkg/table" + "github.com/spf13/cobra" +) + +// Cmd represents the plugin command +var Cmd = &cobra.Command{ + Use: "list", + Short: "list ocm plugins", + Long: "list all the plugins under the user executable path", + RunE: run, +} + +var args struct { + nameOnly bool +} + +func init() { + flags := Cmd.Flags() + flags.BoolVar( + &args.nameOnly, + "nameonly", + false, + "Show the plugin name only", + ) +} + +func run(cmd *cobra.Command, argv []string) error { + defaultPath := filepath.SplitList(os.Getenv("PATH")) + newPath := uniquePath(defaultPath) + pluginPrefix := "ocm-" + + columns := "NAME,PATH" + paddingByColumn := []int{20, 30} + + table.PrintPadded(os.Stdout, strings.Split(columns, ","), paddingByColumn) + + for _, dir := range newPath { + if _, err := os.Stat(dir); os.IsNotExist(err) { + continue + } + items, err := ioutil.ReadDir(dir) + if err != nil { + return err + } + for _, f := range items { + if f.IsDir() { + continue + } + + if !strings.HasPrefix(f.Name(), pluginPrefix) { + continue + } + + plugin := f.Name() + + var pluginOutput []string + absPath := dir + "/" + plugin + if isExec, err := isExecutable(absPath); err == nil && !isExec { + defer fmt.Printf("Warning: %s identified as an ocm plugin, but it is not executable. \n", absPath) + } else if err != nil { + return err + } else { + if args.nameOnly { + pluginOutput = []string{plugin} + } else { + pluginOutput = []string{plugin, dir} + } + table.PrintPadded(os.Stdout, pluginOutput, paddingByColumn) + } + } + } + return nil +} + +// uniquePath remove the duplicate items from the PATH +func uniquePath(path []string) []string { + keys := make(map[string]int) + uniPath := make([]string, 0) + + for _, p := range path { + keys[p] = 1 + } + + for element := range keys { + uniPath = append(uniPath, element) + } + + return uniPath +} + +// detect if the plugin is excutable +func isExecutable(file string) (bool, error) { + info, err := os.Stat(file) + if err != nil { + return false, err + } + + if runtime.GOOS == "windows" { + fileExt := strings.ToLower(filepath.Ext(file)) + + switch fileExt { + case ".bat", ".cmd", ".com", ".exe", ".ps1": + return true, nil + } + return false, nil + } + + if m := info.Mode(); !m.IsDir() && m&0111 != 0 { + return true, nil + } + + return false, nil +} From 10695c19cb4818fe8b1e77408e54bfabcb0dedb2 Mon Sep 17 00:00:00 2001 From: Bo Meng Date: Tue, 15 Sep 2020 16:06:40 +0800 Subject: [PATCH 2/3] several fixs: add alias for plugin/plugins sort the Path before print set the path as . if it is empty --- cmd/ocm/plugin/cmd.go | 9 +++++---- cmd/ocm/plugin/list/cmd.go | 10 +++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/ocm/plugin/cmd.go b/cmd/ocm/plugin/cmd.go index 83cbffa5..f6c667fc 100644 --- a/cmd/ocm/plugin/cmd.go +++ b/cmd/ocm/plugin/cmd.go @@ -23,10 +23,11 @@ import ( ) var Cmd = &cobra.Command{ - Use: "plugin COMMAND", - Short: "Get information about plugins", - Long: "Get information about installed ocm plugins", - Args: cobra.MinimumNArgs(1), + Use: "plugin COMMAND", + Aliases: []string{"plugins"}, + Short: "Get information about plugins", + Long: "Get information about installed ocm plugins", + Args: cobra.MinimumNArgs(1), } func init() { diff --git a/cmd/ocm/plugin/list/cmd.go b/cmd/ocm/plugin/list/cmd.go index d626d9e9..6d5a966a 100644 --- a/cmd/ocm/plugin/list/cmd.go +++ b/cmd/ocm/plugin/list/cmd.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "runtime" + "sort" "strings" "github.com/openshift-online/ocm-cli/pkg/table" @@ -64,6 +65,11 @@ func run(cmd *cobra.Command, argv []string) error { if _, err := os.Stat(dir); os.IsNotExist(err) { continue } + + if dir == "" { + dir = "." + } + items, err := ioutil.ReadDir(dir) if err != nil { return err @@ -80,7 +86,7 @@ func run(cmd *cobra.Command, argv []string) error { plugin := f.Name() var pluginOutput []string - absPath := dir + "/" + plugin + absPath := filepath.Join(dir, plugin) if isExec, err := isExecutable(absPath); err == nil && !isExec { defer fmt.Printf("Warning: %s identified as an ocm plugin, but it is not executable. \n", absPath) } else if err != nil { @@ -111,6 +117,8 @@ func uniquePath(path []string) []string { uniPath = append(uniPath, element) } + sort.Strings(uniPath) + return uniPath } From 8c0e185606fa2cadd03033d7019430e07e7c09e3 Mon Sep 17 00:00:00 2001 From: Bo Meng Date: Thu, 17 Sep 2020 13:45:54 +0800 Subject: [PATCH 3/3] move the blank PATH detector to uniquePath func --- cmd/ocm/plugin/list/cmd.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/ocm/plugin/list/cmd.go b/cmd/ocm/plugin/list/cmd.go index 6d5a966a..259a56a0 100644 --- a/cmd/ocm/plugin/list/cmd.go +++ b/cmd/ocm/plugin/list/cmd.go @@ -66,10 +66,6 @@ func run(cmd *cobra.Command, argv []string) error { continue } - if dir == "" { - dir = "." - } - items, err := ioutil.ReadDir(dir) if err != nil { return err @@ -110,6 +106,9 @@ func uniquePath(path []string) []string { uniPath := make([]string, 0) for _, p := range path { + if p == "" { + p = "." + } keys[p] = 1 }