Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding helm 3 support and refactoring a bit #2

Merged
merged 13 commits into from
Mar 26, 2020
118 changes: 77 additions & 41 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (

"gopkg.in/yaml.v3"

"github.com/fairwindsops/api-version-finder/pkg/api"
"github.com/fairwindsops/api-version-finder/pkg/finder"
"github.com/fairwindsops/api-version-finder/pkg/helm"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog"
Expand All @@ -35,19 +38,28 @@ var (
directory string
outputFormat string
showNonDeprecated bool
helmVersion string
)

func init() {
rootCmd.AddCommand(detectCmd)
detectCmd.PersistentFlags().StringVarP(&directory, "directory", "d", "", "The directory to scan. If blank, defaults to current workding dir.")
detectCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "tabular", "The output format to use. (tabular|json|yaml)")
detectCmd.PersistentFlags().BoolVar(&showNonDeprecated, "show-non-deprecated", false, "If enabled, will show files that have non-deprecated apiVersion. Only applies to tabular output.")
rootCmd.AddCommand(detectFilesCmd)
detectFilesCmd.PersistentFlags().StringVarP(&directory, "directory", "d", "", "The directory to scan. If blank, defaults to current workding dir.")
detectFilesCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "tabular", "The output format to use. (tabular|json|yaml)")
detectFilesCmd.PersistentFlags().BoolVar(&showNonDeprecated, "show-non-deprecated", false, "If enabled, will show files that have non-deprecated apiVersion. Only applies to tabular output.")

rootCmd.AddCommand(detectHelmCmd)
detectHelmCmd.PersistentFlags().StringVar(&helmVersion, "helm-version", "3", "Helm version in current cluster (2|3)")
detectHelmCmd.PersistentFlags().BoolVar(&showNonDeprecated, "show-non-deprecated", false, "If enabled, will show files that have non-deprecated apiVersion. Only applies to tabular output.")

klog.InitFlags(nil)
flag.Parse()
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
}

type Checker interface {
FindVersions() error
}

var rootCmd = &cobra.Command{
Use: "api-version-finder",
Short: "api-version-finder",
Expand All @@ -62,58 +74,46 @@ var rootCmd = &cobra.Command{
},
}

var detectCmd = &cobra.Command{
var detectFilesCmd = &cobra.Command{
Use: "detect-files",
Short: "detect-files",
Long: `Detect Kubernetes apiVersions in a directory.`,
Run: func(cmd *cobra.Command, args []string) {
dir := finder.NewFinder(directory)
err := dir.FindVersions()
if err != nil {
fmt.Printf("Error running finder: %s\n", err.Error())
fmt.Println("Error running finder:", err)
os.Exit(1)
}

if dir.APIFiles == nil {
if dir.Outputs == nil {
fmt.Println("No api-versioned files found in specified directory.")
os.Exit(0)
}

switch outputFormat {
case "tabular":
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintln(w, "KIND\t VERSION\t DEPRECATED\t FILE")

for _, file := range dir.APIFiles {
// Don't show non-deprecated apis if we have them disabled
if !showNonDeprecated {
if !file.APIVersion.Deprecated {
continue
}
}
kind := file.APIVersion.Kind
deprecated := fmt.Sprintf("%t", file.APIVersion.Deprecated)
version := file.APIVersion.Name
fileName := file.Name
err = parseOutput(dir.Outputs)
if err != nil {
fmt.Println("Error Parsing Output:", err)
os.Exit(1)
}
},
}

fmt.Fprintf(w, "%s\t %s\t %s\t %s\t\n", kind, version, deprecated, fileName)
}
w.Flush()
case "json":
outData, err := json.Marshal(dir.APIFiles)
if err != nil {
fmt.Printf("Error generating JSON: %s\n", err.Error())
os.Exit(1)
}
fmt.Println(string(outData))
case "yaml":
outData, err := yaml.Marshal(dir.APIFiles)
if err != nil {
fmt.Printf("Error generating yaml: %s\n", err.Error())
os.Exit(1)
}
fmt.Println(string(outData))
var detectHelmCmd = &cobra.Command{
Use: "detect-helm",
Short: "detect-helm",
Long: `Detect Kubernetes apiVersions in a helm release (in cluster)`,
Run: func(cmd *cobra.Command, args []string) {
h := helm.NewHelm(helmVersion)
err := h.FindVersions()
if err != nil {
fmt.Println("Error running helm-detect:", err)
os.Exit(1)
}
err = parseOutput(h.Outputs)
if err != nil {
fmt.Println("Error Parsing Output:", err)
os.Exit(1)
}
},
}
Expand All @@ -127,3 +127,39 @@ func Execute(VERSION string, COMMIT string) {
os.Exit(1)
}
}

func parseOutput(outputs []*api.Output) error {
sudermanjr marked this conversation as resolved.
Show resolved Hide resolved
var err error
var outData []byte
switch outputFormat {
case "tabular":
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 2, ' ', 0)
_, err = fmt.Fprintln(w, "KIND\t VERSION\t DEPRECATED\t RESOURCE NAME")

for _, output := range outputs {
// Don't show non-deprecated apis if we have them disabled
if !showNonDeprecated {
if !output.APIVersion.Deprecated {
continue
}
}
kind := output.APIVersion.Kind
deprecated := fmt.Sprintf("%t", output.APIVersion.Deprecated)
version := output.APIVersion.Name
fileName := output.Name

_, err = fmt.Fprintf(w, "%s\t %s\t %s\t %s\t\n", kind, version, deprecated, fileName)
}
err = w.Flush()
case "json":
outData, err = json.Marshal(outputs)
case "yaml":
outData, err = yaml.Marshal(outputs)
}
if err != nil {
return err
}
fmt.Println(string(outData))
return nil
}
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ module github.com/fairwindsops/api-version-finder
go 1.14

require (
github.com/google/gofuzz v1.1.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/onsi/ginkgo v1.11.0 // indirect
github.com/onsi/gomega v1.8.1 // indirect
github.com/spf13/cobra v0.0.6
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.4.0
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
k8s.io/apimachinery v0.17.4
k8s.io/client-go v0.17.4
k8s.io/klog v1.0.0
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89 // indirect
sigs.k8s.io/controller-runtime v0.2.0-alpha.0
sigs.k8s.io/yaml v1.2.0 // indirect
)
Loading