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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
44 changes: 38 additions & 6 deletions cmd/cca/cca.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,48 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package cca implements root command
package cca

import (
"os"

"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/output"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
logutil "sigs.k8s.io/kind/pkg/log"
)

// NewCommand returns a new cobra.Command implementing the root command for cca
func NewCommand() *cobra.Command {
flags := &cmdutil.GlobalFlags{}
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "cca",
Short: "cca CLI manages authentication, configurations and interactions with the cloud.ca APIs.",
Long: "cca CLI manages authentication, configurations and interactions with the cloud.ca APIs.",
Short: "cca manages authentication, configurations and interactions with the cloud.ca APIs.",
Long: "cca manages authentication, configurations and interactions with the cloud.ca APIs.",
SilenceUsage: true,
Version: version.Version(),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := flags.Normalize(cmd, viper.Get, args); err != nil {
return err
}
return nil
},
}

// add all top level subcommands
cmd.AddCommand(completion.NewCommand())
cmd.AddCommand(version.NewCommand())
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", cmdutil.DefaultOutputFormat, "output format "+output.FormatString())
cmd.PersistentFlags().BoolVar(new(bool), "nocolor", false, "Disable colored output")
cmd.PersistentFlags().StringVar(&flags.LogLevel, "loglevel", cmdutil.DefaultLogLevel.String(), "log level "+logutil.LevelsString())

cmd.AddCommand(completion.NewCommand(flags))
cmd.AddCommand(version.NewCommand(flags))

return cmd
}
Expand All @@ -45,8 +63,22 @@ func Run() error {
return NewCommand().Execute()
}

// Main wraps Run
// Main wraps Run and sets the log formatter
func Main() {
// let's explicitly set stdout
logrus.SetOutput(os.Stdout)

// this formatter is the default, but the timestamps output aren't
// particularly useful, they're relative to the command start
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "15:04:05",
// we force colors because this only forces over the isTerminal check
// and this will not be accurately checkable later on when we wrap
// the logger output with our logutil.StatusFriendlyWriter
ForceColors: logutil.IsTerminal(logrus.StandardLogger().Out),
})

if err := Run(); err != nil {
os.Exit(1)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/cca/completion/bash/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package bash
import (
"os"

"github.com/cloud-ca/cca/pkg/cmdutil"
"github.com/spf13/cobra"
)

// NewCommand returns a new cobra.Command for cluster creation
func NewCommand() *cobra.Command {
// NewCommand returns a new cobra.Command for bash completion
func NewCommand(gf *cmdutil.GlobalFlags) *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 @@ -21,16 +21,17 @@ 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/lithammer/dedent"
"github.com/spf13/cobra"
)

// NewCommand returns a new cobra.Command for cluster creation
func NewCommand() *cobra.Command {
// NewCommand returns a new cobra.Command for shell completion
func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "completion",
Short: "Output shell completion code for the specified shell (bash or zsh)",
Short: "Output completion code for the specified shell (bash or zsh)",
Long: strings.TrimLeft(dedent.Dedent(fmt.Sprint(`
Outputs cca shell completion for the given shell (bash or zsh)
This depends on the bash-completion binary. Example installation instructions:
Expand All @@ -48,8 +49,8 @@ func NewCommand() *cobra.Command {
`)), "\n"),
}

cmd.AddCommand(zsh.NewCommand())
cmd.AddCommand(bash.NewCommand())
cmd.AddCommand(zsh.NewCommand(gf))
cmd.AddCommand(bash.NewCommand(gf))

return cmd
}
5 changes: 3 additions & 2 deletions cmd/cca/completion/zsh/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package zsh
import (
"os"

"github.com/cloud-ca/cca/pkg/cmdutil"
"github.com/spf13/cobra"
)

// NewCommand returns a new cobra.Command for cluster creation
func NewCommand() *cobra.Command {
// NewCommand returns a new cobra.Command for zsh completion
func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "zsh",
Expand Down
8 changes: 5 additions & 3 deletions cmd/cca/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"time"

"github.com/cloud-ca/cca/pkg/cmdutil"
"github.com/spf13/cobra"
)

Expand All @@ -35,17 +36,18 @@ var (
)

// NewCommand returns a new cobra.Command for version
func NewCommand() *cobra.Command {
func NewCommand(gf *cmdutil.GlobalFlags) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "version",
Short: "prints the cca CLI version",
Long: "prints the cca CLI version",
Short: "Print the cca CLI version",
Long: "Print the cca CLI version",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(fmt.Sprintf("cca version %s", Version()))
return nil
},
}

return cmd
}

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ go 1.12

require (
github.com/lithammer/dedent v1.1.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
sigs.k8s.io/kind v0.4.0
)
Loading