Skip to content

Commit

Permalink
Merge pull request #131 from jstrachan/changes2
Browse files Browse the repository at this point in the history
fix: auto-generate the json schema
  • Loading branch information
jenkins-x-bot-test authored Apr 6, 2021
2 parents deff0fe + bd81ea1 commit 8693404
Show file tree
Hide file tree
Showing 4 changed files with 549 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .lighthouse/jenkins-x/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ spec:
resources: {}
- name: build-make-build
resources: {}
- image: golang:1.15
name: build-gen-schema
resources: {}
script: |
#!/bin/sh
make gen-schema
- name: promote-changelog
resources: {}
podTemplate: {}
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ GOTEST := $(GO) test
PACKAGE_DIRS := $(shell $(GO) list ./... | grep -v /vendor/)
GO_DEPENDENCIES := $(shell find . -type f -name '*.go')

.PHONY: build
build:
$(GO) build ./...

.PHONY: linux
linux: build

test: build
$(GOTEST) -coverprofile=coverage.out ./...

Expand Down Expand Up @@ -46,8 +50,13 @@ cover:
code-generate:
./hack/generate.sh

.PHONY: docs
.PHONY: docs gen-schema
docs: generate-refdocs

.PHONY: gen-schema
gen-schema:
mkdir -p schema
go run cmd/schemagen/main.go

include Makefile.codegen

83 changes: 83 additions & 0 deletions cmd/schemagen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/jx-api/v4/pkg/util"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"io/ioutil"
"os"
)

const ()

func main() {
o := &Options{}
if len(os.Args) > 1 {
o.Out = os.Args[1]
}
err := o.Run()
if err != nil {
log.Logger().Errorf("failed: %v", err)
os.Exit(1)
}
log.Logger().Infof("completed the plugin generator")
os.Exit(0)
}

type Options struct {
Out string
}

// Run implements this command
func (o *Options) Run() error {
if o.Out == "" {
o.Out = "schema/jx-requirements.json"
}
return o.Generate("jx-requirements.yml", &v4beta1.Requirements{})
}

// Generate generates the schema document
func (o *Options) Generate(schemaName string, schemaTarget interface{}) error {
schema := util.GenerateSchema(schemaTarget)
if schema == nil {
return fmt.Errorf("could not generate schema for %s", schemaName)
}

output := prettyPrintJSON(schema)

if output == "" {
tempOutput, err := json.Marshal(schema)
if err != nil {
return errors.Wrapf(err, "error outputting schema for %s", schemaName)
}
output = string(tempOutput)
}
log.Logger().Infof("JSON schema for %s:", schemaName)

if o.Out != "" {
err := ioutil.WriteFile(o.Out, []byte(output), util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", o.Out)
}
log.Logger().Infof("wrote file %s", o.Out)
return nil
}
log.Logger().Infof("%s", output)
return nil
}

func prettyPrintJSON(input interface{}) string {
output := &bytes.Buffer{}
if err := json.NewEncoder(output).Encode(input); err != nil {
return ""
}
formatted := &bytes.Buffer{}
if err := json.Indent(formatted, output.Bytes(), "", " "); err != nil {
return ""
}
return string(formatted.Bytes())
}
Loading

0 comments on commit 8693404

Please sign in to comment.