Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic update schema #64

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ main
# Files used for debugging
.vscode/

# Files created by GoLand
.idea/

# File created running tests
tests/**/tmp/

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gofmt:

.PHONY: bin
bin:
go build main.go
go build *.go

.PHONY: test
test:
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g
}
deployment := generator.GetDeployment(deployParams)
```

<br></br>
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

Expand All @@ -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)
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)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
38 changes: 38 additions & 0 deletions replaceSchemaFile.go
Original file line number Diff line number Diff line change
@@ -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))
}
59 changes: 59 additions & 0 deletions updateApi.sh
Original file line number Diff line number Diff line change
@@ -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}"