Skip to content

Commit

Permalink
Move configuration package to go-sona-types for reuse (#200)
Browse files Browse the repository at this point in the history
* use `configuration` package from go-sona-types
  • Loading branch information
bhamail committed Nov 3, 2020
1 parent 78bc610 commit ab916e3
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 362 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/sdboyer/constext v0.0.0-20170321163424-836a14457353 // indirect
github.com/shopspring/decimal v1.2.0
github.com/sirupsen/logrus v1.6.0
github.com/sonatype-nexus-community/go-sona-types v0.0.7
github.com/sonatype-nexus-community/go-sona-types v0.0.8
github.com/spf13/afero v1.3.4 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.0.0
Expand All @@ -34,7 +34,6 @@ require (
golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/ini.v1 v1.60.1 // indirect
gopkg.in/yaml.v2 v2.3.0
)

// fix vulnerability: CVE-2020-15114 in etcd v3.3.13+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/sonatype-nexus-community/go-sona-types v0.0.7 h1:ZH00KCTlLnI0LBIs+Juv9WBCtamKKdoFeryp1p5UB6s=
github.com/sonatype-nexus-community/go-sona-types v0.0.7/go.mod h1:voGD7LEwz9TSvjZWNxST56pFPa0hdNQ71WsuMwBki3k=
github.com/sonatype-nexus-community/go-sona-types v0.0.8 h1:6xb9BIC2w3y4kF/xQA25Zs6Yrl8ZqFkfH77AKPaG4RA=
github.com/sonatype-nexus-community/go-sona-types v0.0.8/go.mod h1:uou8FGf9R5Nz1c6BfSM3v9K7g0R6faTYoxLh9Ybeht8=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.3.4 h1:8q6vk3hthlpb2SouZcnBVKboxWQWMDNF38bwholZrJc=
Expand Down
11 changes: 9 additions & 2 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"fmt"
"os"

"github.com/sonatype-nexus-community/go-sona-types/configuration"
"github.com/sonatype-nexus-community/nancy/internal/customerrors"
"github.com/sonatype-nexus-community/nancy/internal/logger"

"github.com/sonatype-nexus-community/nancy/internal/configuration"
"github.com/spf13/cobra"
)

Expand All @@ -47,7 +48,13 @@ func doConfig(cmd *cobra.Command, args []string) (err error) {
}
}()

if err = configuration.GetConfigFromCommandLine(os.Stdin); err != nil {
logLady = logger.GetLogger("", configOssi.LogLevel)
var configSet *configuration.ConfigSet
if configSet, err = configuration.New(logLady); err != nil {
panic(err)
}

if err = configSet.GetConfigFromCommandLine(os.Stdin); err != nil {
panic(err)
}
return
Expand Down
15 changes: 8 additions & 7 deletions internal/cmd/iq.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
"path"

"github.com/mitchellh/go-homedir"
"github.com/sonatype-nexus-community/go-sona-types/configuration"
"github.com/sonatype-nexus-community/go-sona-types/iq"
"github.com/sonatype-nexus-community/nancy/internal/configuration"
ossIndexTypes "github.com/sonatype-nexus-community/go-sona-types/ossindex/types"
"github.com/sonatype-nexus-community/nancy/internal/customerrors"
"github.com/sonatype-nexus-community/nancy/internal/logger"
"github.com/sonatype-nexus-community/nancy/packages"
Expand Down Expand Up @@ -179,7 +180,7 @@ func bindViperIq(cmd *cobra.Command) {
// need to defer bind call until command is run. see: https://github.com/spf13/viper/issues/233

// need to ensure ossi CLI flags will override ossi config file values when running IQ command
bindViper(rootCmd)
bindViperRootCmd()

// Bind viper to the flags passed in via the command line, so it will override config from file
if err := viper.BindPFlag(configuration.ViperKeyIQUsername, lookupFlagNotNil(flagNameIqUsername, cmd)); err != nil {
Expand All @@ -206,21 +207,21 @@ func initIQConfig() {
var cfgFileToCheck string
if cfgFileIQ != "" {
viper.SetConfigFile(cfgFileIQ)
viper.SetConfigType(configTypeYaml)
viper.SetConfigType(configuration.ConfigTypeYaml)
cfgFileToCheck = cfgFileIQ
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configPath := path.Join(home, types.IQServerDirName)
configPath := path.Join(home, ossIndexTypes.IQServerDirName)

viper.AddConfigPath(configPath)
viper.SetConfigType(configTypeYaml)
viper.SetConfigName(types.IQServerConfigFileName)
viper.SetConfigType(configuration.ConfigTypeYaml)
viper.SetConfigName(ossIndexTypes.IQServerConfigFileName)

cfgFileToCheck = path.Join(configPath, types.IQServerConfigFileName)
cfgFileToCheck = path.Join(configPath, ossIndexTypes.IQServerConfigFileName)
}

if fileExists(cfgFileToCheck) {
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/iq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (

"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/sonatype-nexus-community/go-sona-types/configuration"
"github.com/sonatype-nexus-community/go-sona-types/iq"
"github.com/sonatype-nexus-community/nancy/internal/configuration"
ossIndexTypes "github.com/sonatype-nexus-community/go-sona-types/ossindex/types"
"github.com/sonatype-nexus-community/nancy/internal/customerrors"
"github.com/sonatype-nexus-community/nancy/types"
"github.com/spf13/viper"
Expand Down Expand Up @@ -77,10 +78,10 @@ func TestIqCommandPathInvalidFile(t *testing.T) {
}

func setupIQConfigFile(t *testing.T, tempDir string) {
cfgDirIQ := path.Join(tempDir, types.IQServerDirName)
cfgDirIQ := path.Join(tempDir, ossIndexTypes.IQServerDirName)
assert.Nil(t, os.Mkdir(cfgDirIQ, 0700))

cfgFileIQ = path.Join(tempDir, types.IQServerDirName, types.IQServerConfigFileName)
cfgFileIQ = path.Join(tempDir, ossIndexTypes.IQServerDirName, ossIndexTypes.IQServerConfigFileName)
}
func resetIQConfigFile() {
cfgFileIQ = ""
Expand Down
21 changes: 9 additions & 12 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import (
"strings"
"time"

"github.com/sonatype-nexus-community/nancy/internal/configuration"

"github.com/common-nighthawk/go-figure"
"github.com/golang/dep"
"github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
"github.com/sonatype-nexus-community/go-sona-types/configuration"
"github.com/sonatype-nexus-community/go-sona-types/ossindex"
ossIndexTypes "github.com/sonatype-nexus-community/go-sona-types/ossindex/types"
"github.com/sonatype-nexus-community/nancy/buildversion"
Expand Down Expand Up @@ -178,14 +177,14 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&configOssi.Path, "path", "p", "", "Specify a path to a dep "+GopkgLockFilename+" file for scanning")
}

func bindViper(cmd *cobra.Command) {
func bindViperRootCmd() {
// need to defer bind call until command is run. see: https://github.com/spf13/viper/issues/233

// Bind viper to the flags passed in via the command line, so it will override config from file
if err := viper.BindPFlag(configuration.ViperKeyUsername, lookupPersistentFlagNotNil(flagNameOssiUsername, cmd)); err != nil {
if err := viper.BindPFlag(configuration.ViperKeyUsername, lookupPersistentFlagNotNil(flagNameOssiUsername, rootCmd)); err != nil {
panic(err)
}
if err := viper.BindPFlag(configuration.ViperKeyToken, lookupPersistentFlagNotNil(flagNameOssiToken, cmd)); err != nil {
if err := viper.BindPFlag(configuration.ViperKeyToken, lookupPersistentFlagNotNil(flagNameOssiToken, rootCmd)); err != nil {
panic(err)
}
}
Expand All @@ -199,27 +198,25 @@ func lookupPersistentFlagNotNil(flagName string, cmd *cobra.Command) *pflag.Flag
return foundFlag
}

const configTypeYaml = "yaml"

func initConfig() {
var cfgFileToCheck string
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
viper.SetConfigType(configTypeYaml)
viper.SetConfigType(configuration.ConfigTypeYaml)
cfgFileToCheck = cfgFile
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configPath := path.Join(home, types.OssIndexDirName)
configPath := path.Join(home, ossIndexTypes.OssIndexDirName)

viper.AddConfigPath(configPath)
viper.SetConfigType(configTypeYaml)
viper.SetConfigName(types.OssIndexConfigFileName)
viper.SetConfigType(configuration.ConfigTypeYaml)
viper.SetConfigName(ossIndexTypes.OssIndexConfigFileName)

cfgFileToCheck = path.Join(configPath, types.OssIndexConfigFileName)
cfgFileToCheck = path.Join(configPath, ossIndexTypes.OssIndexConfigFileName)
}

if fileExists(cfgFileToCheck) {
Expand Down
8 changes: 3 additions & 5 deletions internal/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import (
"strings"
"testing"

"github.com/sonatype-nexus-community/go-sona-types/configuration"
"github.com/sonatype-nexus-community/go-sona-types/ossindex"
ossIndexTypes "github.com/sonatype-nexus-community/go-sona-types/ossindex/types"
"github.com/sonatype-nexus-community/nancy/types"

"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/sonatype-nexus-community/nancy/internal/configuration"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -316,13 +316,11 @@ func TestConfigOssi_cleanCache(t *testing.T) {
func setupConfig(t *testing.T) (tempDir string) {
tempDir, err := ioutil.TempDir("", "config-test")
assert.NoError(t, err)
configuration.HomeDir = tempDir
return tempDir
}

func resetConfig(t *testing.T, tempDir string) {
var err error
configuration.HomeDir, err = os.UserHomeDir()
assert.NoError(t, err)
_ = os.RemoveAll(tempDir)
}
Expand Down Expand Up @@ -366,10 +364,10 @@ func TestInitConfigWithNoConfigFile(t *testing.T) {
}

func setupTestOSSIConfigFile(t *testing.T, tempDir string) {
cfgDir := path.Join(tempDir, types.OssIndexDirName)
cfgDir := path.Join(tempDir, ossIndexTypes.OssIndexDirName)
assert.Nil(t, os.Mkdir(cfgDir, 0700))

cfgFile = path.Join(tempDir, types.OssIndexDirName, types.OssIndexConfigFileName)
cfgFile = path.Join(tempDir, ossIndexTypes.OssIndexDirName, ossIndexTypes.OssIndexConfigFileName)
}

func resetOSSIConfigFile() {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/sleuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var sleuthCmd = &cobra.Command{
nancy sleuth -p Gopkg.lock --` + flagNameOssiUsername + ` your_user --` + flagNameOssiToken + ` your_token`,
Short: "Check for vulnerabilities in your Golang dependencies using Sonatype's OSS Index",
Long: `'nancy sleuth' is a command to check for vulnerabilities in your Golang dependencies, powered by the 'Sonatype OSS Index'.`,
PreRun: func(cmd *cobra.Command, args []string) { bindViper(rootCmd) },
PreRun: func(cmd *cobra.Command, args []string) { bindViperRootCmd() },
RunE: doOSSI,
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/sleuth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestConfigOssi_exclude_vulnerabilities(t *testing.T) {
[]string{sleuthCmd.Use, "--exclude-vulnerability=CVE123,CVE988"}...)
}

const testdataDir = "../../internal/configuration/testdata"
const testdataDir = "testdata/sleuth"

func TestConfigOssi_exclude_vulnerabilities_with_sane_file(t *testing.T) {
file, _ := os.Open(testdataDir + "/normalIgnore")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit ab916e3

Please sign in to comment.