From 6236425496517b70fd7d9587670d03815cbcf073 Mon Sep 17 00:00:00 2001 From: Deepak Sharma Date: Tue, 6 Apr 2021 15:00:49 +0530 Subject: [PATCH] feat: relative manifest file path support (#42) --- cmd/analyse.go | 12 +++++++++++- cmd/analyse_test.go | 17 +++++++++++++++++ cmd/testdata/requirements.txt | 18 ++++++++++++++++++ cmd/validators.go | 17 ++++------------- 4 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 cmd/analyse_test.go create mode 100755 cmd/testdata/requirements.txt diff --git a/cmd/analyse.go b/cmd/analyse.go index 5beb9d1..bfcf000 100644 --- a/cmd/analyse.go +++ b/cmd/analyse.go @@ -66,11 +66,12 @@ func runAnalyse(cmd *cobra.Command, args []string) { ) os.Exit(1) } + manifestPath := getAbsPath(args[0]) requestParams := driver.RequestType{ UserID: viper.GetString("crda_key"), ThreeScaleToken: viper.GetString("auth_token"), Host: viper.GetString("host"), - RawManifestFile: args[0], + RawManifestFile: manifestPath, } if !jsonOut { fmt.Fprintln(os.Stdout, "Analysing your Dependency Stack! Please wait...") @@ -81,3 +82,12 @@ func runAnalyse(cmd *cobra.Command, args []string) { os.Exit(2) } } + +// getAbsPath converts relative path to Abs +func getAbsPath(givenPath string) string { + manifestPath, err := filepath.Abs(givenPath) + if err != nil { + log.Fatal().Err(err).Msgf("Unable to convert to Absolute file path.") + } + return manifestPath +} diff --git a/cmd/analyse_test.go b/cmd/analyse_test.go new file mode 100644 index 0000000..a43a7cc --- /dev/null +++ b/cmd/analyse_test.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "path/filepath" + "testing" + + "github.com/google/go-cmp/cmp" +) + +// TestGetAbsPath tests Absolute Path func. +func TestGetAbsPath(t *testing.T) { + got := getAbsPath("testdata/requirements.txt") + want, _ := filepath.Abs("testdata/requirements.txt") + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("Error in getAbsPath func. (-want, +got):\n%s", diff) + } +} diff --git a/cmd/testdata/requirements.txt b/cmd/testdata/requirements.txt new file mode 100755 index 0000000..d52e094 --- /dev/null +++ b/cmd/testdata/requirements.txt @@ -0,0 +1,18 @@ +# +# This file is autogenerated by pip-compile +# To update, run: +# +# pip-compile --output-file=requirements.txt requirements.in +# +click==7.0 # via flask +codecov==2.1.11 +flask==1.0.2 +gunicorn==19.9.0 +itsdangerous==1.1.0 # via flask +jinja2==2.11.3 # via flask +markupsafe==1.1.1 # via jinja2 +prometheus_client==0.6.0 +werkzeug==0.15.4 # via flask +raven[flask]==6.10.0 +contextlib2==0.5.5 # via raven +blinker==1.4 # via raven \ No newline at end of file diff --git a/cmd/validators.go b/cmd/validators.go index ae99d49..3146dd3 100644 --- a/cmd/validators.go +++ b/cmd/validators.go @@ -4,25 +4,16 @@ import ( "errors" "fmt" "os" - "path/filepath" "github.com/spf13/cobra" ) -func checkFileExists(file string) bool { - isAbsPath := filepath.IsAbs(file) - if _, err := os.Stat(file); os.IsNotExist(err) { - return false - } - return isAbsPath -} - func validateFileArg(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return errors.New("requires absolute file path ") + return errors.New("Requires valid manifest file path.") } - if checkFileExists(args[0]) { - return nil + if _, err := os.Stat(args[0]); os.IsNotExist(err) { + return errors.New(fmt.Sprintf("Invalid file path: %s", args[0])) } - return fmt.Errorf("Invalid file path: %s", args[0]) + return nil }