Skip to content

Commit

Permalink
Merge pull request #287 from accurics/bugix/fix-version
Browse files Browse the repository at this point in the history
fix version and help usage
  • Loading branch information
Cesar Rodriguez authored Aug 16, 2020
2 parents c630fad + fe52997 commit 64c03e9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
15 changes: 15 additions & 0 deletions pkg/cli/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cli

import (
"flag"
"fmt"
"os"

Expand Down Expand Up @@ -56,6 +57,20 @@ func Execute() {
rootCmd.PersistentFlags().StringVarP(&OutputType, "output", "o", "yaml", "output type (json, yaml, xml)")
rootCmd.PersistentFlags().StringVarP(&ConfigFile, "config-path", "c", "", "config file path")

// parse the flags but hack around to avoid exiting with error code 2 on help
// override usage so that flag.Parse uses root command's usage instead of default one when invoked with -h
flag.Usage = func() {
_ = rootCmd.Help()
}

flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
args := os.Args[1:]
if err := flag.CommandLine.Parse(args); err != nil {
if err == flag.ErrHelp {
os.Exit(0)
}
}

setDefaultCommandIfNonePresent()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
2 changes: 0 additions & 2 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package cli

import (
"github.com/accurics/terrascan/pkg/version"
"github.com/spf13/cobra"
)

Expand All @@ -40,5 +39,4 @@ var rootCmd = &cobra.Command{
Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure.
For more information, please visit https://docs.accurics.com
`,
Version: version.Get(),
}
42 changes: 42 additions & 0 deletions pkg/cli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright (C) 2020 Accurics, 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 cli

import (
"fmt"

"github.com/accurics/terrascan/pkg/version"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Terrascan version",
Long: `Terrascan
Displays the version of this Terrascan binary
`,
Run: getVersion,
}

func getVersion(cmd *cobra.Command, args []string) {
fmt.Printf("version: %s\n", version.Get())
}

func init() {
RegisterCommand(rootCmd, versionCmd)
}

0 comments on commit 64c03e9

Please sign in to comment.