Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

will not exit when no kubeconfig found #581

Merged
merged 1 commit into from
Jun 27, 2019
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
25 changes: 10 additions & 15 deletions pkg/synopsysctl/cmd_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package synopsysctl
import (
"fmt"
"os"
"strings"

homedir "github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
Expand All @@ -48,23 +49,17 @@ var rootCmd = &cobra.Command{
return nil
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Set the Log Level
lvl, err := log.ParseLevel(logLevelCtl)
if err != nil {
log.Errorf("ctl-log-Level '%s' is not a valid level: %s", logLevelCtl, err)
}
log.SetLevel(lvl)
if !cmd.Flags().Lookup("kubeconfig").Changed { // if kubeconfig wasn't set, check the environ
if kubeconfigEnvVal, exists := os.LookupEnv("KUBECONFIG"); exists { // set kubeconfig if environ is set
kubeconfig = kubeconfigEnvVal
}
mockModeFlagExists := cmd.Flags().Lookup("mock")
mockMode := false
if mockModeFlagExists != nil && mockModeFlagExists.Changed {
mockMode = true
}
// Sets kubeconfig and initializes resource client libraries
if err := setResourceClients(); err != nil {
log.Error(err)
os.Exit(1)
nativeMode := strings.Contains(cmd.CommandPath(), "native")
// only set resource clients if we are not in mock mode and we are not in native mode
if !mockMode && !nativeMode {
callSetResourceClients()
}
return nil
return parseLogLevelAndKubeConfig(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("must specify sub-command")
Expand Down
25 changes: 25 additions & 0 deletions pkg/synopsysctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
operatorutil "github.com/blackducksoftware/synopsys-operator/pkg/util"
util "github.com/blackducksoftware/synopsys-operator/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -48,6 +49,30 @@ var alertClient *alertclientset.Clientset
var blackDuckClient *blackduckclientset.Clientset
var opsSightClient *opssightclientset.Clientset

func parseLogLevelAndKubeConfig(cmd *cobra.Command) error {
// Set the Log Level
lvl, err := log.ParseLevel(logLevelCtl)
if err != nil {
log.Errorf("ctl-log-Level '%s' is not a valid level: %s", logLevelCtl, err)
return err
}
log.SetLevel(lvl)
if !cmd.Flags().Lookup("kubeconfig").Changed { // if kubeconfig wasn't set, check the environ
if kubeconfigEnvVal, exists := os.LookupEnv("KUBECONFIG"); exists { // set kubeconfig if environ is set
kubeconfig = kubeconfigEnvVal
}
}
return nil
}

func callSetResourceClients() {
// Sets kubeconfig and initializes resource client libraries
if err := setResourceClients(); err != nil {
log.Error(err)
os.Exit(1)
}
}

// setResourceClients sets the global variables for the Kuberentes rest config
// and the resource clients
func setResourceClients() error {
Expand Down