Skip to content

Commit

Permalink
Add json schema generator
Browse files Browse the repository at this point in the history
  • Loading branch information
vladarts committed Jan 30, 2022
1 parent f0d3b94 commit 550fd8e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 13 deletions.
43 changes: 43 additions & 0 deletions cmd/command-jsonschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"github.com/alecthomas/jsonschema"
"github.com/spf13/cobra"
"github.com/xxxbobrxxx/ide-gen/pkg/config"
)

type JsonSchemaCommand struct {
cmd *cobra.Command
}

func NewJsonSchemaCommandCommand() *JsonSchemaCommand {
command := &JsonSchemaCommand{}

cmd := &cobra.Command{
Use: "json-schema",
Aliases: []string{"gen"},
Short: "Generate and output json schema for the config file",
SilenceUsage: true,
Args: cobra.NoArgs,
RunE: command.Execute,
}
command.cmd = cmd

return command
}

func (command *JsonSchemaCommand) Register() *cobra.Command {
return command.cmd
}

func (command *JsonSchemaCommand) Execute(_ *cobra.Command, _ []string) (err error) {
reflector := jsonschema.Reflector{
ExpandedStruct: true,
}
schema := reflector.Reflect(&config.Config{})
b, _ := schema.MarshalJSON()

fmt.Println(string(b))
return nil
}
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func init() {

rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(NewGenerateCommand().Register())
rootCmd.AddCommand(NewJsonSchemaCommandCommand().Register())
}

func main() {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ require (
)

require (
github.com/alecthomas/jsonschema v0.0.0-20211228220459-151e3c21f49d // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/jsonschema v0.0.0-20211228220459-151e3c21f49d h1:4BQNwS4T13UU3Yee4GfzZH3Q9SNpKeJvLigfw8fDjX0=
github.com/alecthomas/jsonschema v0.0.0-20211228220459-151e3c21f49d/go.mod h1:/n6+1/DWPltRLWL/VKyUxg6tzsl5kHUCcraimt4vr60=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down Expand Up @@ -230,6 +232,8 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -330,6 +334,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
1 change: 1 addition & 0 deletions hack/gen-schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go run github.com/xxxbobrxxx/ide-gen/cmd json-schema > resources/config.schema.json
6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type Config struct {
GitlabEntries []*gitlab.DiscoveryConfig `json:"gitlab"`
GitEntries []*repository.GitSourcesRootConfig `json:"git"`
RawEntries []*repository.RawSourcesRootConfig `json:"directory"`
GitlabEntries []*gitlab.DiscoveryConfig `json:"gitlab,omitempty"`
GitEntries []*repository.GitSourcesRootConfig `json:"git,omitempty"`
RawEntries []*repository.RawSourcesRootConfig `json:"directory,omitempty"`
}

func (c *Config) GetProjectEntries(flags repository.SourcesRootFlags) ([]repository.ProjectEntry, error) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/gitlab/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
)

type DiscoveryConfig struct {
Url string `json:"url"`
Token string `json:"token"`
TokenEnvVar string `json:"tokenEnvVar"`
Url string `json:"url,omitempty"`
Token string `json:"token,omitempty" jsonschema:"oneof_required=token"`
TokenEnvVar string `json:"tokenEnvVar,omitempty" jsonschema:"oneof_required=tokenEnvVar"`

IncludeArchived bool `json:"includeArchived"`
HttpsUrl bool `json:"httpsUrl"`
FastForward bool `json:"fastForward"`
IncludeArchived bool `json:"includeArchived,omitempty"`
HttpsUrl bool `json:"httpsUrl,omitempty"`
FastForward bool `json:"fastForward,omitempty"`

Include *[]string `json:"include"`
Exclude *[]string `json:"exclude"`
Include *[]string `json:"include,omitempty"`
Exclude *[]string `json:"exclude,omitempty"`

client *gitlab.Client
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/repository/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func (r *GitSourcesRoot) Exists(path string) (bool, error) {

type GitSourcesRootConfig struct {
Url string `json:"url"`
FastForward *bool `json:"fastForward"`
Remotes *map[string]string `json:"remotes"`
FastForward *bool `json:"fastForward,omitempty"`
Remotes *map[string]string `json:"remotes,omitempty"`
}

func (c *GitSourcesRootConfig) Directory(root string) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions resources/config.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"$schema":"http://json-schema.org/draft-04/schema#","properties":{"gitlab":{"items":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/DiscoveryConfig"},"type":"array"},"git":{"items":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/GitSourcesRootConfig"},"type":"array"},"directory":{"items":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/RawSourcesRootConfig"},"type":"array"}},"additionalProperties":false,"type":"object","definitions":{"DiscoveryConfig":{"properties":{"url":{"type":"string"},"token":{"type":"string"},"tokenEnvVar":{"type":"string"},"includeArchived":{"type":"boolean"},"httpsUrl":{"type":"boolean"},"fastForward":{"type":"boolean"},"include":{"items":{"type":"string"},"type":"array"},"exclude":{"items":{"type":"string"},"type":"array"}},"additionalProperties":false,"type":"object","oneOf":[{"required":["token"],"title":"token"},{"required":["tokenEnvVar"],"title":"tokenEnvVar"}]},"GitSourcesRootConfig":{"required":["url"],"properties":{"url":{"type":"string"},"fastForward":{"type":"boolean"},"remotes":{"patternProperties":{".*":{"type":"string"}},"type":"object"}},"additionalProperties":false,"type":"object"},"RawSourcesRootConfig":{"required":["path"],"properties":{"path":{"type":"string"}},"additionalProperties":false,"type":"object"}}}

0 comments on commit 550fd8e

Please sign in to comment.