From 5910b5b660d1b54ca82ee6a534d75e4979387a72 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Wed, 3 Mar 2021 11:12:47 -0500 Subject: [PATCH 1/2] clean up commits Signed-off-by: Stephanie --- .github/workflows/go.yml | 1 + .gitignore | 3 ++ Makefile | 2 +- README.md | 13 ++++++++- go.mod | 2 +- go.sum | 4 +-- main.go | 22 ++++++++++----- replaceSchemaFile.go | 38 ++++++++++++++++++++++++++ updateApi.sh | 59 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 replaceSchemaFile.go create mode 100755 updateApi.sh diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index fac2bcfe..30a62085 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -28,6 +28,7 @@ jobs: if [[ ! -z $(git status -s) ]] then echo "Go mod state is not clean" + git diff "$GITHUB_SHA" exit 1 fi - name: Build Binary diff --git a/.gitignore b/.gitignore index 8b08cc53..215c2f2f 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,9 @@ main # Files used for debugging .vscode/ +# Files created by GoLand +.idea/ + # File created running tests tests/**/tmp/ diff --git a/Makefile b/Makefile index a5b39d44..bcf06815 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ gofmt: .PHONY: bin bin: - go build main.go + go build *.go .PHONY: test test: diff --git a/README.md b/README.md index 144157c1..8f717367 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,17 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g } deployment := generator.GetDeployment(deployParams) ``` + +

+Run `updateApi.sh` can update to use latest `github.com/devfile/api` and update the schema saved under `pkg/devfile/parser/data` + +The script also accepts version number as an argument to update devfile schema for a specific devfile version. +For example, run the following command will update devfile schema for 2.0.0 +``` +./updateApi.sh 2.0.0 +``` +Running the script with no arguments will default to update the latest devfile version + ## Usage @@ -54,4 +65,4 @@ In the future, the following projects will be consuming this library as a Golang ## Issues -Issues are tracked in the [devfile/api](https://github.com/devfile/api) repo with the label [area/library](https://github.com/devfile/api/issues?q=is%3Aopen+is%3Aissue+label%3Aarea%2Flibrary) \ No newline at end of file +Issues are tracked in the [devfile/api](https://github.com/devfile/api) repo with the label [area/library](https://github.com/devfile/api/issues?q=is%3Aopen+is%3Aissue+label%3Aarea%2Flibrary) diff --git a/go.mod b/go.mod index 7e886bf3..8f010c86 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/devfile/library go 1.13 require ( - github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835 + github.com/devfile/api/v2 v2.0.0-20210223145532-81859eaef987 github.com/fatih/color v1.7.0 github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 github.com/gobwas/glob v0.2.3 diff --git a/go.sum b/go.sum index 0fae7b99..252c1324 100644 --- a/go.sum +++ b/go.sum @@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do 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/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835 h1:PalHtpqhvX/yu5DKFqwTWa1h7UJBd0H7+veJStur/wg= -github.com/devfile/api/v2 v2.0.0-20210219185433-585f5fe35835/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg= +github.com/devfile/api/v2 v2.0.0-20210223145532-81859eaef987 h1:3wclWpWL/+IP6oAMY1M+RECQYa/4ZBkff7jDX1RyLxg= +github.com/devfile/api/v2 v2.0.0-20210223145532-81859eaef987/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= diff --git a/main.go b/main.go index b673e4ce..60cedcee 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,21 @@ import ( ) func main() { + if len(os.Args) > 1 && os.Args[1] == "updateSchema" { + ReplaceSchemaFile() + } else { + parserTest() + } +} + +//ParseDevfile to parse devfile from library +func ParseDevfile(devfileLocation string) (parser.DevfileObj, error) { + + devfile, err := devfilepkg.ParseAndValidate(devfileLocation) + return devfile, err +} + +func parserTest() { var devfile parser.DevfileObj var err error if len(os.Args) > 1 { @@ -107,10 +122,3 @@ func main() { } } - -//ParseDevfile to parse devfile from library -func ParseDevfile(devfileLocation string) (parser.DevfileObj, error) { - - devfile, err := devfilepkg.ParseAndValidate(devfileLocation) - return devfile, err -} diff --git a/replaceSchemaFile.go b/replaceSchemaFile.go new file mode 100644 index 00000000..5607c893 --- /dev/null +++ b/replaceSchemaFile.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strings" +) + +func ReplaceSchemaFile() { + if len(os.Args) != 7 { + printErr(fmt.Errorf("ReplaceSchemaFile() expect 7 arguments")) + os.Exit(1) + } + originalSchema := os.Args[2] + schemaURL := os.Args[3] + packageVersion := os.Args[4] + jsonSchemaVersion := os.Args[5] + filePath := os.Args[6] + + // replace all ` with ' to convert schema content from json file format to json format in golang + newSchema := strings.ReplaceAll(originalSchema, "`", "'") + fmt.Printf("Writing to file: %s\n", filePath) + fileContent := fmt.Sprintf("package %s\n\n// %s\nconst %s = `%s\n`\n", packageVersion, schemaURL, jsonSchemaVersion, newSchema) + + if err := ioutil.WriteFile(filePath, []byte(fileContent), 0755); err != nil { + printErr(err) + os.Exit(1) + } +} + +func printErr(err error) { + // prints error in red + colorRed := "\033[31m" + colorReset := "\033[0m" + + fmt.Println(string(colorRed), err, string(colorReset)) +} diff --git a/updateApi.sh b/updateApi.sh new file mode 100755 index 00000000..abaaaae8 --- /dev/null +++ b/updateApi.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +BLUE='\033[1;34m' +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' +BOLD='\033[1m' + +set -e + +CURRENT_DIR=$(pwd) +API_PKG="github.com/devfile/api/v2" +SCHEMA_URL_MASTER="https://raw.githubusercontent.com/devfile/api/master/schemas/latest/devfile.json" + +# 2.0.0 devfile +SCHEMA_URL_200="https://raw.githubusercontent.com/devfile/api/2.0.x/schemas/latest/devfile.json" +PACKAGE_VERSION_200="version200" +JSON_SCHEMA_200="JsonSchema200" +FILE_PATH_200="./pkg/devfile/parser/data/v2/2.0.0/devfileJsonSchema200.go" + +# 2.1.0 devfile +PACKAGE_VERSION_210="version210" +JSON_SCHEMA_210="JsonSchema210" +FILE_PATH_210="./pkg/devfile/parser/data/v2/2.1.0/devfileJsonSchema210.go" + + +onError() { + cd "${CURRENT_DIR}" +} +trap 'onError' ERR + + +echo -e "${GREEN}Updating devfile/api in go.mod${NC}" +go get "${API_PKG}@master" + +echo -e "${GREEN}Get latest schema${NC}" + +case "${1}" in + "2.0.0") + SCHEMA_URL=${SCHEMA_URL_200} + PACKAGE_VERSION=${PACKAGE_VERSION_200} + JSON_SCHEMA=${JSON_SCHEMA_200} + FILE_PATH=${FILE_PATH_200} + ;; + *) + # default + SCHEMA_URL=${SCHEMA_URL_MASTER} + PACKAGE_VERSION=${PACKAGE_VERSION_210} + JSON_SCHEMA=${JSON_SCHEMA_210} + FILE_PATH=${FILE_PATH_210} + ;; +esac + +schema=$(curl -L "${SCHEMA_URL}") + +#replace all ` with ' and write to schema file +echo -e "${GREEN}Write to go file${NC}" +go build *.go +./main updateSchema "${schema}" "${SCHEMA_URL}" "${PACKAGE_VERSION}" "${JSON_SCHEMA}" "${FILE_PATH}" From f73cb922e50bf4885a4c6c44ce3123bb8e53f21f Mon Sep 17 00:00:00 2001 From: Stephanie Date: Wed, 3 Mar 2021 12:03:29 -0500 Subject: [PATCH 2/2] undo go.sum change, and use go 1.13 in git workflow Signed-off-by: Stephanie --- .github/workflows/go.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 30a62085..e7e45364 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -13,10 +13,10 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Go 1.x - uses: actions/setup-go@v2 + - name: Setup Go environment + uses: actions/setup-go@v2.1.3 with: - go-version: ^1.13 + go-version: 1.13 id: go - name: Check out code into the Go module directory