Skip to content

Commit

Permalink
feat: relative manifest file path support (fabric8-analytics#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepak Sharma authored Apr 6, 2021
1 parent a9e3489 commit 6236425
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
12 changes: 11 additions & 1 deletion cmd/analyse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand All @@ -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
}
17 changes: 17 additions & 0 deletions cmd/analyse_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
18 changes: 18 additions & 0 deletions cmd/testdata/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
17 changes: 4 additions & 13 deletions cmd/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 6236425

Please sign in to comment.