-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* validationPOST: explore a first validation step * validationPOST: woopsy on LICENSE * validationPOST: set isDebug from env * validationPOST: bump timeouts of small commands for Windows * add shellcheck on quickinstall script * docs validation * validationPOST: require bash for declare-p things * validationPOST: fmt * validationPOST: less awkward bash requirement * validationPOST: naming things is hard
- Loading branch information
Showing
10 changed files
with
157 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,5 @@ vendor/ | |
# End of https://www.gitignore.io/api/go | ||
|
||
testman | ||
testman-* | ||
schemas.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func validateDocs(apiKey string, yml []byte) []byte { | ||
docs := struct { | ||
V uint `json:"v"` | ||
Blobs map[string]string `json:"blobs"` | ||
}{ | ||
V: 1, | ||
Blobs: blobs(yml), | ||
} | ||
|
||
payload, err := json.Marshal(docs) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return validationPOST(apiKey, payload) | ||
} | ||
|
||
func validationPOST(apiKey string, JSON []byte) []byte { | ||
r, err := http.NewRequest(http.MethodPost, docsURL, bytes.NewBuffer(JSON)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
r.Header.Set("Content-Type", mimeJSON) | ||
r.Header.Set("Accept", mimeJSON) | ||
r.Header.Set("Accept-Encoding", "gzip, deflate, br") | ||
r.Header.Set(xAPIKeyHeader, apiKey) | ||
client := &http.Client{} | ||
|
||
start := time.Now() | ||
resp, err := client.Do(r) | ||
us := uint64(time.Since(start) / time.Microsecond) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatal("!read body: ", err) | ||
} | ||
log.Printf("🡱 %vμs POST %s\n 🡱 %s\n 🡳 %s\n", us, docsURL, JSON, body) | ||
|
||
if resp.StatusCode == 400 { | ||
reportValidationErrors(body) | ||
log.Fatal("Documentation validation failed") | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
log.Fatal("!200: ", resp.Status) | ||
} | ||
|
||
var validated struct { | ||
V uint `json:"v"` | ||
Token string `json:"token"` | ||
} | ||
if err := json.Unmarshal(body, &validated); err != nil { | ||
log.Fatal(err) | ||
} | ||
if validated.Token == "" { | ||
log.Fatal("Could not acquire a validation token") | ||
} | ||
|
||
return body | ||
} | ||
|
||
func reportValidationErrors(errors []byte) { | ||
fmt.Println("Validation errors:") | ||
|
||
var out bytes.Buffer | ||
err := json.Indent(&out, errors, "", " ") | ||
if err != nil { | ||
fmt.Println(string(errors)) | ||
} | ||
|
||
fmt.Println(out.String()) | ||
} | ||
|
||
func blobs(yml []byte) map[string]string { | ||
blobs := map[string]string{localYML: string(yml)} | ||
|
||
var ymlConfPartial struct { | ||
Doc struct { | ||
File string `yaml:"file"` | ||
} `yaml:"documentation"` | ||
} | ||
// YML is not yet validated, so ignore errors | ||
yaml.Unmarshal(yml, &ymlConfPartial) | ||
|
||
filePath := ymlConfPartial.Doc.File | ||
if "" == filePath { | ||
return blobs | ||
} | ||
|
||
fileData, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
blobs[filePath] = string(fileData) | ||
|
||
return blobs | ||
} |