Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions cmd/cca/cca.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cca/completion/bash/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions cmd/cca/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
}
4 changes: 2 additions & 2 deletions cmd/cca/completion/zsh/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions cmd/cca/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions pkg/cli/wrapper.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 0 additions & 14 deletions pkg/cmdutil/description.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/cmdutil/defaults.go → pkg/flags/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmdutil/flags.go → pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/output/util.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
29 changes: 29 additions & 0 deletions pkg/util/description.go
Original file line number Diff line number Diff line change
@@ -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")
}