From 662e15dd9acd46f7654d34c9cad6f518f61514f7 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 15 May 2019 23:54:57 -0400 Subject: [PATCH] start scan --- go.mod | 1 + go.sum | 2 ++ main.go | 9 ++++++++- scan.go | 34 ++++++++++++++++++++++++++++++++++ scan_test.go | 10 ++++++++++ test/good_config/yarn.lock | 1 + tidelift_yml.go | 6 ------ 7 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 scan.go create mode 100644 scan_test.go diff --git a/go.mod b/go.mod index 1dcde20..87e0936 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( github.com/bmatcuk/doublestar v1.1.1 github.com/davecgh/go-spew v1.1.1 + github.com/ddliu/go-httpclient v0.5.1 github.com/fatih/color v1.7.0 // indirect github.com/mattn/go-colorable v0.1.1 // indirect github.com/mattn/go-isatty v0.0.7 // indirect diff --git a/go.sum b/go.sum index 5b8cf88..70044c6 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvF github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ddliu/go-httpclient v0.5.1 h1:ys4KozrhBaGdI1yuWIFwNNILqhnMU9ozTvRNfCTorvs= +github.com/ddliu/go-httpclient v0.5.1/go.mod h1:8QVbjq00YK2f2MQyiKuWMdaKOFRcoD9VuubkNCNOuZo= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= diff --git a/main.go b/main.go index 851de5a..7758eed 100644 --- a/main.go +++ b/main.go @@ -75,7 +75,7 @@ func scan(directory string) error { return cli.NewExitError("no .tidelift.yml at supplied directory path", 6) } fmt.Println(strings.Join(getListOfManifestFilenames(directory), ", ")) - + upload(directory) // TODO, actually upload scan, don't just print names return nil } @@ -98,3 +98,10 @@ func verify(directory string) error { fmt.Println("Your team-name, repo-name and TIDELIFT_API_KEY are all set! Feel free to use `scan`") return nil } + +// Check error and panic if so +func check(e error) { + if e != nil { + panic(e) + } +} diff --git a/scan.go b/scan.go new file mode 100644 index 0000000..ada1f77 --- /dev/null +++ b/scan.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "os" + + "github.com/ddliu/go-httpclient" +) + +func upload(directory string) { + tideliftYML := getKeysFromYaml(readTideliftYamlFile(directory)) + fileNames := getListOfManifestFilenames(directory) + auth := fmt.Sprintf("Bearer %s", os.Getenv("TIDELIFT_API_KEY")) + // url := fmt.Sprintf("https://api.tidelift.com/subscriber/%s/%s/manifest/upload", team, repository) + url := fmt.Sprintf("http://127.0.0.1:8000/subscriber/%s/%s/manifest/upload", string(tideliftYML.TeamName), string(tideliftYML.RepositoryName)) + // TODO: Add revision + + var body = map[string]string{ + "@files[]": fileNames[0], + } + + _, err := httpclient.WithHeaders(map[string]string{ + "Authorization": auth, + }).Post(url, body) + check(err) + + fmt.Println(body) +} + +func openFile(f string) *os.File { + handle, err := os.Open(f) + check(err) + return handle +} diff --git a/scan_test.go b/scan_test.go new file mode 100644 index 0000000..ba989a4 --- /dev/null +++ b/scan_test.go @@ -0,0 +1,10 @@ +package main + +import ( + "testing" +) + +func TestUpload(t *testing.T) { + //assert getManifestMatches gets all requirements.txt + upload("test/good_config") +} diff --git a/test/good_config/yarn.lock b/test/good_config/yarn.lock index e69de29..950cdeb 100644 --- a/test/good_config/yarn.lock +++ b/test/good_config/yarn.lock @@ -0,0 +1 @@ +// lock file \ No newline at end of file diff --git a/tidelift_yml.go b/tidelift_yml.go index d7464d7..51af8ff 100644 --- a/tidelift_yml.go +++ b/tidelift_yml.go @@ -60,12 +60,6 @@ func passesMinimumRequirements(yamlText []byte) string { return "" } -func check(e error) { - if e != nil { - panic(e) - } -} - func fileExists(filename string) bool { info, err := os.Stat(filename)