-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from jstrachan/changes2
fix: improve the schema generation
- Loading branch information
Showing
8 changed files
with
4,005 additions
and
65 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 |
---|---|---|
@@ -1,83 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/jenkins-x/jx-api/v4/pkg/apis/core" | ||
"github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1" | ||
"github.com/jenkins-x/jx-api/v4/pkg/util" | ||
jenkins_io "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io" | ||
"github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1" | ||
"github.com/jenkins-x/jx-api/v4/pkg/schemagen" | ||
"github.com/jenkins-x/jx-logging/v3/pkg/log" | ||
"github.com/pkg/errors" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
const () | ||
var ( | ||
resourceKinds = []schemagen.ResourceKind{ | ||
{ | ||
APIVersion: core.GroupAndVersion, | ||
Name: "requirements", | ||
Resource: &v4beta1.Requirements{}, | ||
}, | ||
{ | ||
APIVersion: jenkins_io.GroupAndVersion, | ||
Name: "environment", | ||
Resource: &v1.Environment{}, | ||
}, | ||
{ | ||
APIVersion: jenkins_io.GroupAndVersion, | ||
Name: "source-repository", | ||
Resource: &v1.SourceRepository{}, | ||
}, | ||
{ | ||
APIVersion: jenkins_io.GroupAndVersion, | ||
Name: "pipeline-activity", | ||
Resource: &v1.PipelineActivity{}, | ||
}, | ||
{ | ||
APIVersion: jenkins_io.GroupAndVersion, | ||
Name: "release", | ||
Resource: &v1.Release{}, | ||
}, | ||
} | ||
) | ||
|
||
func main() { | ||
o := &Options{} | ||
out := "schema" | ||
if len(os.Args) > 1 { | ||
o.Out = os.Args[1] | ||
out = os.Args[1] | ||
} | ||
err := o.Run() | ||
err := schemagen.GenerateSchemas(resourceKinds, out) | ||
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()) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v1 | ||
package jenkins_io | ||
|
||
const ( | ||
// GroupName is the Jenkins API group name | ||
|
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,86 @@ | ||
package schemagen | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"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" | ||
"path/filepath" | ||
) | ||
|
||
const ( | ||
// DefaultDirWritePermissions default permissions when creating a directory | ||
DefaultDirWritePermissions = 0766 | ||
) | ||
|
||
// ResourceKind defines a resource kind to have its schema generated | ||
type ResourceKind struct { | ||
APIVersion string | ||
Name string | ||
Resource interface{} | ||
} | ||
|
||
// GenerateSchemas generates the schemas for the given kinds | ||
func GenerateSchemas(resourceKinds []ResourceKind, out string) error { | ||
for _, k := range resourceKinds { | ||
name := k.Name | ||
out := filepath.Join(out, k.APIVersion, name+".json") | ||
dir := filepath.Dir(out) | ||
err := os.MkdirAll(dir, DefaultDirWritePermissions) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to create dir %s", dir) | ||
} | ||
|
||
err = generate(name, out, k.Resource) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to generate file %s", out) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Generate generates the schema document | ||
func generate(schemaName string, out 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 out != "" { | ||
err := ioutil.WriteFile(out, []byte(output), util.DefaultWritePermissions) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to save file %s", out) | ||
} | ||
log.Logger().Infof("wrote file %s", 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()) | ||
} |
File renamed without changes.
Oops, something went wrong.