Skip to content

Commit 6236425

Browse files
author
Deepak Sharma
authored
feat: relative manifest file path support (#42)
1 parent a9e3489 commit 6236425

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

cmd/analyse.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ func runAnalyse(cmd *cobra.Command, args []string) {
6666
)
6767
os.Exit(1)
6868
}
69+
manifestPath := getAbsPath(args[0])
6970
requestParams := driver.RequestType{
7071
UserID: viper.GetString("crda_key"),
7172
ThreeScaleToken: viper.GetString("auth_token"),
7273
Host: viper.GetString("host"),
73-
RawManifestFile: args[0],
74+
RawManifestFile: manifestPath,
7475
}
7576
if !jsonOut {
7677
fmt.Fprintln(os.Stdout, "Analysing your Dependency Stack! Please wait...")
@@ -81,3 +82,12 @@ func runAnalyse(cmd *cobra.Command, args []string) {
8182
os.Exit(2)
8283
}
8384
}
85+
86+
// getAbsPath converts relative path to Abs
87+
func getAbsPath(givenPath string) string {
88+
manifestPath, err := filepath.Abs(givenPath)
89+
if err != nil {
90+
log.Fatal().Err(err).Msgf("Unable to convert to Absolute file path.")
91+
}
92+
return manifestPath
93+
}

cmd/analyse_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmd
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
)
9+
10+
// TestGetAbsPath tests Absolute Path func.
11+
func TestGetAbsPath(t *testing.T) {
12+
got := getAbsPath("testdata/requirements.txt")
13+
want, _ := filepath.Abs("testdata/requirements.txt")
14+
if diff := cmp.Diff(want, got); diff != "" {
15+
t.Errorf("Error in getAbsPath func. (-want, +got):\n%s", diff)
16+
}
17+
}

cmd/testdata/requirements.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# This file is autogenerated by pip-compile
3+
# To update, run:
4+
#
5+
# pip-compile --output-file=requirements.txt requirements.in
6+
#
7+
click==7.0 # via flask
8+
codecov==2.1.11
9+
flask==1.0.2
10+
gunicorn==19.9.0
11+
itsdangerous==1.1.0 # via flask
12+
jinja2==2.11.3 # via flask
13+
markupsafe==1.1.1 # via jinja2
14+
prometheus_client==0.6.0
15+
werkzeug==0.15.4 # via flask
16+
raven[flask]==6.10.0
17+
contextlib2==0.5.5 # via raven
18+
blinker==1.4 # via raven

cmd/validators.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,16 @@ import (
44
"errors"
55
"fmt"
66
"os"
7-
"path/filepath"
87

98
"github.com/spf13/cobra"
109
)
1110

12-
func checkFileExists(file string) bool {
13-
isAbsPath := filepath.IsAbs(file)
14-
if _, err := os.Stat(file); os.IsNotExist(err) {
15-
return false
16-
}
17-
return isAbsPath
18-
}
19-
2011
func validateFileArg(cmd *cobra.Command, args []string) error {
2112
if len(args) < 1 {
22-
return errors.New("requires absolute file path ")
13+
return errors.New("Requires valid manifest file path.")
2314
}
24-
if checkFileExists(args[0]) {
25-
return nil
15+
if _, err := os.Stat(args[0]); os.IsNotExist(err) {
16+
return errors.New(fmt.Sprintf("Invalid file path: %s", args[0]))
2617
}
27-
return fmt.Errorf("Invalid file path: %s", args[0])
18+
return nil
2819
}

0 commit comments

Comments
 (0)