Skip to content

Commit

Permalink
Modularize ec's Cobra commands
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Bestavros <[email protected]>
  • Loading branch information
mbestavros committed Sep 19, 2023
1 parent c5965b7 commit dd3f62c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 51 deletions.
8 changes: 6 additions & 2 deletions cmd/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import (
var FetchCmd *cobra.Command

func init() {
FetchCmd = &cobra.Command{
FetchCmd = NewFetchCmd()
FetchCmd.AddCommand(fetchPolicyCmd())
}

func NewFetchCmd() *cobra.Command {
return &cobra.Command{
Use: "fetch",
Short: "Fetch remote resources",
}
FetchCmd.AddCommand(fetchPolicyCmd())
}
8 changes: 6 additions & 2 deletions cmd/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import (
var InitCmd *cobra.Command

func init() {
InitCmd = &cobra.Command{
InitCmd = NewInitCmd()
InitCmd.AddCommand(initPoliciesCmd())
}

func NewInitCmd() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Initialize a directory for use",
}
InitCmd.AddCommand(initPoliciesCmd())
}
10 changes: 7 additions & 3 deletions cmd/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ import (
var InspectCmd *cobra.Command

func init() {
InspectCmd = &cobra.Command{
InspectCmd = NewInspectCmd()
InspectCmd.AddCommand(inspectPolicyCmd())
InspectCmd.AddCommand(inspectPolicyDataCmd())
}

func NewInspectCmd() *cobra.Command {
return &cobra.Command{
Use: "inspect",
Short: "Inspect policy rules",
}
InspectCmd.AddCommand(inspectPolicyCmd())
InspectCmd.AddCommand(inspectPolicyDataCmd())
}
49 changes: 12 additions & 37 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,22 @@ import (
"os"
"time"

hd "github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"

"github.com/enterprise-contract/ec-cli/cmd/fetch"
"github.com/enterprise-contract/ec-cli/cmd/initialize"
"github.com/enterprise-contract/ec-cli/cmd/inspect"
"github.com/enterprise-contract/ec-cli/cmd/root"
"github.com/enterprise-contract/ec-cli/cmd/test"
"github.com/enterprise-contract/ec-cli/cmd/track"
"github.com/enterprise-contract/ec-cli/cmd/validate"
"github.com/enterprise-contract/ec-cli/cmd/version"
"github.com/enterprise-contract/ec-cli/internal/kubernetes"
"github.com/enterprise-contract/ec-cli/internal/logging"
"github.com/enterprise-contract/ec-cli/internal/utils"
)

var cancel context.CancelFunc

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "ec",
Short: "Enterprise Contract CLI",

Long: hd.Doc(`
Enterprise Contract CLI
Set of commands to help validate resources with the Enterprise Contract.
`),

SilenceUsage: true,

PersistentPreRun: func(cmd *cobra.Command, _ []string) {
logging.InitLogging(verbose, quiet, debug, trace)

// Create a new context now that flags have been parsed so a custom timeout can be used.
ctx := cmd.Context()
ctx, cancel = context.WithTimeout(ctx, globalTimeout)
cmd.SetContext(ctx)
},

PersistentPostRun: func(cmd *cobra.Command, _ []string) {
if cancel != nil {
cancel()
}
},
}
var RootCmd = root.NewRootCmd(quiet, verbose, debug, trace, globalTimeout)

var quiet bool = false
var verbose bool = false
Expand All @@ -74,12 +45,16 @@ var trace bool = false
var globalTimeout = 5 * time.Minute

func init() {
RootCmd.PersistentFlags().BoolVar(&quiet, "quiet", quiet, "less verbose output")
RootCmd.PersistentFlags().BoolVar(&verbose, "verbose", verbose, "more verbose output")
RootCmd.PersistentFlags().BoolVar(&debug, "debug", debug, "same as verbose but also show function names and line numbers")
RootCmd.PersistentFlags().BoolVar(&trace, "trace", trace, "enable trace logging")
RootCmd.PersistentFlags().DurationVar(&globalTimeout, "timeout", globalTimeout, "max overall execution duration")
kubernetes.AddKubeconfigFlag(RootCmd)
setFlags(RootCmd)
}

func setFlags(rootCmd *cobra.Command) {
rootCmd.PersistentFlags().BoolVar(&quiet, "quiet", quiet, "less verbose output")
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", verbose, "more verbose output")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", debug, "same as verbose but also show function names and line numbers")
rootCmd.PersistentFlags().BoolVar(&trace, "trace", trace, "enable trace logging")
rootCmd.PersistentFlags().DurationVar(&globalTimeout, "timeout", globalTimeout, "max overall execution duration")
kubernetes.AddKubeconfigFlag(rootCmd)
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
61 changes: 61 additions & 0 deletions cmd/root/root_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright The Enterprise Contract Contributors
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0

package root

import (
"context"
"time"

hd "github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"

"github.com/enterprise-contract/ec-cli/internal/logging"
)

var cancel context.CancelFunc

func NewRootCmd(verbose bool, quiet bool, debug bool, trace bool, globalTimeout time.Duration) *cobra.Command {
rootCmd := &cobra.Command{
Use: "ec",
Short: "Enterprise Contract CLI",

Long: hd.Doc(`
Enterprise Contract CLI
Set of commands to help validate resources with the Enterprise Contract.
`),

SilenceUsage: true,

PersistentPreRun: func(cmd *cobra.Command, _ []string) {
logging.InitLogging(verbose, quiet, debug, trace)

// Create a new context now that flags have been parsed so a custom timeout can be used.
ctx := cmd.Context()
ctx, cancel = context.WithTimeout(ctx, globalTimeout)
cmd.SetContext(ctx)
},

PersistentPostRun: func(cmd *cobra.Command, _ []string) {
if cancel != nil {
cancel()
}
},
}

return rootCmd
}
8 changes: 6 additions & 2 deletions cmd/track/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ import (
var TrackCmd *cobra.Command

func init() {
TrackCmd = &cobra.Command{
TrackCmd = NewTrackCmd()
TrackCmd.AddCommand(trackBundleCmd(tracker.Track, tracker.PullImage, tracker.PushImage))
}

func NewTrackCmd() *cobra.Command {
return &cobra.Command{
Use: "track",
Short: "Record resource references for tracking purposes",
}
TrackCmd.AddCommand(trackBundleCmd(tracker.Track, tracker.PullImage, tracker.PushImage))
}
15 changes: 10 additions & 5 deletions cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ import (
var ValidateCmd *cobra.Command

func init() {
ValidateCmd = &cobra.Command{
Use: "validate",
Short: "Validate conformance with the Enterprise Contract",
}
ValidateCmd.PersistentFlags().Bool("show-successes", false, "")
ValidateCmd = NewValidateCmd()
}

func init() {
ValidateCmd.AddCommand(validateImageCmd(image.ValidateImage))
ValidateCmd.AddCommand(validateDefinitionCmd(definition.ValidateDefinition))
}

func NewValidateCmd() *cobra.Command {
validateCmd := &cobra.Command{
Use: "validate",
Short: "Validate conformance with the Enterprise Contract",
}
validateCmd.PersistentFlags().Bool("show-successes", false, "")
return validateCmd
}

0 comments on commit dd3f62c

Please sign in to comment.