diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml new file mode 100644 index 0000000..f0ba7bf --- /dev/null +++ b/.github/workflows/cd.yaml @@ -0,0 +1,51 @@ +name: Release + +on: + release: + types: [published] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + outputs: + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + steps: + - uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@v2.2.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + flavor: | + latest=false + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=raw,value=latest + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: VERSION=${{ github.event.release.tag_name }} \ No newline at end of file diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..7b6ef06 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,22 @@ +name: build +on: + push: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '1.13.x' + + - name: Install dependencies + run: go get . + + - name: Build + run: go build -a -v \ No newline at end of file diff --git a/README.md b/README.md index bf5cec3..e62a729 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,21 @@ Generates `GraphQL` schemas. ## Usage +### Exclude +To exclude classes, relations, and attributes, use the --exclude flag. It performs a case-insensitive match. + +Example: +``` +--exclude Fravar --exclude OTUngdom +``` + +To exclude from the schema, use the --exclude-schema flag. + +Example: +``` +--exclude-schema OTUngdom +``` ## Install @@ -19,12 +33,12 @@ Mount the directory where you want the generated source code to be written as `/ Linux / MacOS: ```bash -docker run -v $(pwd):/src fint/graphql-cli:latest +docker run -v $(pwd):/src ghcr.io/fintlabs/fint-graphql-cli:latest ``` Windows PowerShell: ```ps1 -docker run -v ${pwd}:/src fint/graphql-cli:latest +docker run -v ${pwd}:/src ghcr.io/fintlabs/fint-graphql-cli:latest ``` ### Source diff --git a/commands.go b/commands.go index ed56e12..6a2d4d6 100644 --- a/commands.go +++ b/commands.go @@ -63,6 +63,10 @@ var Commands = []cli.Command{ Name: "exclude, x", Usage: "Classes to exclude from the generated schema and classes.", }, + cli.StringSliceFlag{ + Name: "exclude-schema, xs", + Usage: "Classes to exclude from the generated schema and classes.", + }, }, }, { diff --git a/generate/command.go b/generate/command.go index 760e481..4a1464f 100644 --- a/generate/command.go +++ b/generate/command.go @@ -37,36 +37,34 @@ func CmdGenerate(c *cli.Context) { classes = classesOrig } else { for _, class := range classesOrig { - for _, s := range exclude { - if !strings.Contains(class.Name, s) { - var rel []types.Association - for _, r := range class.Relations { - if !strings.Contains(r.Target, s) { - rel = append(rel, r) - } else { - fmt.Printf("Excluding %s.%s from %s.%s\n", r.TargetPackage, r.Target, class.Package, class.Name) - } + if !isStringInList(class.Name, exclude) { + var rel []types.Association + for _, r := range class.Relations { + if !isStringInList(r.Target, exclude) { + rel = append(rel, r) + } else { + fmt.Printf("Excluding %s.%s from %s.%s\n", r.TargetPackage, r.Target, class.Package, class.Name) } - class.Relations = rel - var att []types.Attribute - for _, a := range class.Attributes { - if !strings.Contains(a.Type, s) { - att = append(att, a) - } else { - fmt.Printf("Excluding %s (%s) from %s.%s\n", a.Name, a.Type, class.Package, class.Name) - } + } + class.Relations = rel + var att []types.Attribute + for _, a := range class.Attributes { + if !isStringInList(a.Type, exclude) { + att = append(att, a) + } else { + fmt.Printf("Excluding %s (%s) from %s.%s\n", a.Name, a.Type, class.Package, class.Name) } - class.Attributes = att - classes = append(classes, class) - } else { - fmt.Printf("Excluding class %s.%s\n", class.Package, class.Name) } + class.Attributes = att + classes = append(classes, class) + } else { + fmt.Printf("Excluding class %s.%s\n", class.Package, class.Name) } } } setupGraphQlSchemaDirStructure() - generateGraphQlSchema(classes) + generateGraphQlSchema(classes, c) generateGraphQlQueryResolver(classes) generateGraphQlService(classes) generateGraphQlResolver(classes) @@ -107,14 +105,14 @@ func writeResolver(pkg string, className string, content []byte) error { return writeFile(path, fmt.Sprintf("%sResolver.java", className), []byte(content)) } -func generateGraphQlSchema(classes []*types.Class) { +func generateGraphQlSchema(classes []*types.Class, cli *cli.Context) { fmt.Println("Generating GraphQL Schema") var roots []*types.Class for _, c := range classes { - if !c.Abstract && includePackage(c.Package) { + if !c.Abstract && includePackage(c.Package) && !excludeFromSchema(cli, c.Name) { fmt.Printf(" > Creating schema: %s.graphqls\n", c.Name) schema := GetGraphQlSchema(c) err := writeSchema(c.Package, c.Name, []byte(schema)) @@ -211,3 +209,27 @@ func setupGraphQlSchemaDirStructure() { func includePackage(p string) bool { return strings.Contains(p, "administrasjon") || strings.Contains(p, "utdanning") || strings.Contains(p, "felles") } + +func excludeFromSchema(c *cli.Context, p string) bool { + excludeSchema := c.StringSlice("exclude-schema") + + if len(excludeSchema) == 0 { + return false + } + + for _, s := range excludeSchema { + if strings.EqualFold(p, s) { + return true + } + } + return false +} + +func isStringInList(s string, list []string) bool { + for _, v := range list { + if strings.EqualFold(s, v) { + return true + } + } + return false +}