From 7189485a9e0e017573f802cce5718789a7914472 Mon Sep 17 00:00:00 2001 From: Khosrow Moossavi Date: Mon, 5 Aug 2019 20:37:36 -0400 Subject: [PATCH] Use cli.Wrapper struct to hold references of 'things' --- cmd/cca/cca.go | 23 +++++++++++++---------- cmd/cca/completion/bash/bash.go | 4 ++-- cmd/cca/completion/completion.go | 11 ++++++----- cmd/cca/completion/zsh/zsh.go | 4 ++-- cmd/cca/version/version.go | 4 ++-- pkg/cli/wrapper.go | 25 +++++++++++++++++++++++++ pkg/cmdutil/description.go | 14 -------------- pkg/{cmdutil => flags}/defaults.go | 4 ++-- pkg/{cmdutil => flags}/flags.go | 5 ++--- pkg/output/util.go | 14 ++++++++++++++ pkg/util/description.go | 29 +++++++++++++++++++++++++++++ 11 files changed, 97 insertions(+), 40 deletions(-) create mode 100644 pkg/cli/wrapper.go delete mode 100644 pkg/cmdutil/description.go rename pkg/{cmdutil => flags}/defaults.go (92%) rename pkg/{cmdutil => flags}/flags.go (95%) create mode 100644 pkg/util/description.go diff --git a/cmd/cca/cca.go b/cmd/cca/cca.go index 146aa01..e728c34 100644 --- a/cmd/cca/cca.go +++ b/cmd/cca/cca.go @@ -20,7 +20,8 @@ import ( "github.com/cloud-ca/cca/cmd/cca/completion" "github.com/cloud-ca/cca/cmd/cca/version" - "github.com/cloud-ca/cca/pkg/cmdutil" + "github.com/cloud-ca/cca/pkg/cli" + "github.com/cloud-ca/cca/pkg/flags" "github.com/cloud-ca/cca/pkg/output" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -30,7 +31,8 @@ import ( // NewCommand returns a new cobra.Command implementing the root command for cca func NewCommand() *cobra.Command { - flags := &cmdutil.GlobalFlags{} + cli := &cli.Wrapper{} + flg := &flags.GlobalFlags{} cmd := &cobra.Command{ Args: cobra.NoArgs, Use: "cca", @@ -39,21 +41,22 @@ func NewCommand() *cobra.Command { SilenceUsage: true, Version: version.Version(), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := flags.Normalize(cmd, viper.Get, args); err != nil { + if err := flg.Normalize(cmd, viper.Get, args); err != nil { return err } + cli.Flags = flg return nil }, } - cmd.PersistentFlags().StringVar(&flags.APIURL, "api-url", cmdutil.DefaultAPIURL, "API url cloud.ca resources") - cmd.PersistentFlags().StringVar(&flags.APIKey, "api-key", "", "API Key to access cloud.ca resources") - cmd.PersistentFlags().StringVar(&flags.OutputFormat, "output-format", cmdutil.DefaultOutputFormat, "output format "+output.FormatStrings()) - cmd.PersistentFlags().BoolVar(&flags.OutputColored, "output-colored", false, "Enable or disable colored output") - cmd.PersistentFlags().StringVar(&flags.LogLevel, "loglevel", cmdutil.DefaultLogLevel.String(), "log level "+logutil.LevelsString()) + cmd.PersistentFlags().StringVar(&flg.APIURL, "api-url", flags.DefaultAPIURL, "API url cloud.ca resources") + cmd.PersistentFlags().StringVar(&flg.APIKey, "api-key", "", "API Key to access cloud.ca resources") + cmd.PersistentFlags().StringVar(&flg.OutputFormat, "output-format", flags.DefaultOutputFormat, "output format "+output.FormatStrings()) + cmd.PersistentFlags().BoolVar(&flg.OutputColored, "output-colored", false, "Enable or disable colored output") + cmd.PersistentFlags().StringVar(&flg.LogLevel, "loglevel", flags.DefaultLogLevel.String(), "log level "+logutil.LevelsString()) - cmd.AddCommand(completion.NewCommand(flags)) - cmd.AddCommand(version.NewCommand(flags)) + cmd.AddCommand(completion.NewCommand(cli)) + cmd.AddCommand(version.NewCommand(cli)) return cmd } diff --git a/cmd/cca/completion/bash/bash.go b/cmd/cca/completion/bash/bash.go index 98ce919..e9814cc 100644 --- a/cmd/cca/completion/bash/bash.go +++ b/cmd/cca/completion/bash/bash.go @@ -18,12 +18,12 @@ package bash import ( "os" - "github.com/cloud-ca/cca/pkg/cmdutil" + "github.com/cloud-ca/cca/pkg/cli" "github.com/spf13/cobra" ) // NewCommand returns a new cobra.Command for bash completion -func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command { +func NewCommand(cli *cli.Wrapper) *cobra.Command { cmd := &cobra.Command{ Args: cobra.NoArgs, Use: "bash", diff --git a/cmd/cca/completion/completion.go b/cmd/cca/completion/completion.go index 42c5999..746868f 100644 --- a/cmd/cca/completion/completion.go +++ b/cmd/cca/completion/completion.go @@ -18,17 +18,18 @@ package completion import ( "github.com/cloud-ca/cca/cmd/cca/completion/bash" "github.com/cloud-ca/cca/cmd/cca/completion/zsh" - "github.com/cloud-ca/cca/pkg/cmdutil" + "github.com/cloud-ca/cca/pkg/cli" + "github.com/cloud-ca/cca/pkg/util" "github.com/spf13/cobra" ) // NewCommand returns a new cobra.Command for shell completion -func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command { +func NewCommand(cli *cli.Wrapper) *cobra.Command { cmd := &cobra.Command{ Args: cobra.NoArgs, Use: "completion", Short: "Output completion code for the specified shell (bash or zsh)", - Long: cmdutil.LongDescription(` + Long: util.LongDescription(` Outputs cca shell completion for the given shell (bash or zsh) This depends on the bash-completion binary. Example installation instructions: @@ -45,8 +46,8 @@ func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command { `), } - cmd.AddCommand(zsh.NewCommand(gf)) - cmd.AddCommand(bash.NewCommand(gf)) + cmd.AddCommand(zsh.NewCommand(cli)) + cmd.AddCommand(bash.NewCommand(cli)) return cmd } diff --git a/cmd/cca/completion/zsh/zsh.go b/cmd/cca/completion/zsh/zsh.go index f21305f..f62f867 100644 --- a/cmd/cca/completion/zsh/zsh.go +++ b/cmd/cca/completion/zsh/zsh.go @@ -18,12 +18,12 @@ package zsh import ( "os" - "github.com/cloud-ca/cca/pkg/cmdutil" + "github.com/cloud-ca/cca/pkg/cli" "github.com/spf13/cobra" ) // NewCommand returns a new cobra.Command for zsh completion -func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command { +func NewCommand(cli *cli.Wrapper) *cobra.Command { cmd := &cobra.Command{ Args: cobra.NoArgs, Use: "zsh", diff --git a/cmd/cca/version/version.go b/cmd/cca/version/version.go index 13c40a8..73fc11b 100644 --- a/cmd/cca/version/version.go +++ b/cmd/cca/version/version.go @@ -21,7 +21,7 @@ import ( "strings" "time" - "github.com/cloud-ca/cca/pkg/cmdutil" + "github.com/cloud-ca/cca/pkg/cli" "github.com/spf13/cobra" ) @@ -36,7 +36,7 @@ var ( ) // NewCommand returns a new cobra.Command for version -func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command { +func NewCommand(cli *cli.Wrapper) *cobra.Command { cmd := &cobra.Command{ Args: cobra.NoArgs, Use: "version", diff --git a/pkg/cli/wrapper.go b/pkg/cli/wrapper.go new file mode 100644 index 0000000..3439bfe --- /dev/null +++ b/pkg/cli/wrapper.go @@ -0,0 +1,25 @@ +// Copyright © 2019 cloud.ca 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 cli wraps around and holds the references to different part of cca cli command +package cli + +import ( + "github.com/cloud-ca/cca/pkg/flags" +) + +// Wrapper of different parts of cca cli +type Wrapper struct { + Flags *flags.GlobalFlags +} diff --git a/pkg/cmdutil/description.go b/pkg/cmdutil/description.go deleted file mode 100644 index a4b0737..0000000 --- a/pkg/cmdutil/description.go +++ /dev/null @@ -1,14 +0,0 @@ -package cmdutil - -import ( - "fmt" - "strings" - - "github.com/lithammer/dedent" -) - -// LongDescription formats long multi-line description and removes -// the left empty space from the lines -func LongDescription(a interface{}) string { - return strings.TrimLeft(dedent.Dedent(fmt.Sprint(a)), "\n") -} diff --git a/pkg/cmdutil/defaults.go b/pkg/flags/defaults.go similarity index 92% rename from pkg/cmdutil/defaults.go rename to pkg/flags/defaults.go index 777f6c7..b6210be 100644 --- a/pkg/cmdutil/defaults.go +++ b/pkg/flags/defaults.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package cmdutil contains general utility of the cca command -package cmdutil +// Package flags contains general utility of the cca cli flags +package flags import ( "github.com/sirupsen/logrus" diff --git a/pkg/cmdutil/flags.go b/pkg/flags/flags.go similarity index 95% rename from pkg/cmdutil/flags.go rename to pkg/flags/flags.go index f85736a..d4eeba3 100644 --- a/pkg/cmdutil/flags.go +++ b/pkg/flags/flags.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package cmdutil contains general utility of the cca command -package cmdutil +// Package flags contains general utility of the cca cli flags +package flags import ( "github.com/cloud-ca/cca/pkg/output" @@ -39,7 +39,6 @@ func (gf *GlobalFlags) Normalize(cmd *cobra.Command, fn func(key string) interfa if err := gf.parseOutputFormat(cmd, args); err != nil { return err } - return nil } diff --git a/pkg/output/util.go b/pkg/output/util.go index e68ab35..be2e934 100644 --- a/pkg/output/util.go +++ b/pkg/output/util.go @@ -1,3 +1,17 @@ +// Copyright © 2019 cloud.ca 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 output import ( diff --git a/pkg/util/description.go b/pkg/util/description.go new file mode 100644 index 0000000..51e6956 --- /dev/null +++ b/pkg/util/description.go @@ -0,0 +1,29 @@ +// Copyright © 2019 cloud.ca 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 util contains general utility of the cca cli +package util + +import ( + "fmt" + "strings" + + "github.com/lithammer/dedent" +) + +// LongDescription formats long multi-line description and removes +// the left empty space from the lines +func LongDescription(a interface{}) string { + return strings.TrimLeft(dedent.Dedent(fmt.Sprint(a)), "\n") +}