Skip to content

Commit 4c7a0c2

Browse files
Generate schema on build
The following generates the schema on build, this ensures that the schema is always is up to date for all potential clients
1 parent 18af971 commit 4c7a0c2

File tree

9 files changed

+37898
-154
lines changed

9 files changed

+37898
-154
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ dep: $(GOPATH)/bin/dep
5858
$(GOPATH)/bin/dep ensure -vendor-only $(verbose)
5959
endif
6060

61-
build: dep go-build
61+
build: dep rebuild-schema go-build
6262

6363
release-build: dep go-build
6464

@@ -73,7 +73,7 @@ check: dep pre-check test
7373
test: dep
7474
go test $(CHECK_ARGS) -test.timeout=$(TEST_TIMEOUT) $(PROJECT_PACKAGES) -check.v
7575

76-
install: dep go-install
76+
install: dep rebuild-schema go-install
7777

7878
clean:
7979
go clean -n -r --cache --testcache $(PROJECT_PACKAGES)
@@ -114,6 +114,9 @@ simplify:
114114
rebuild-dependencies:
115115
dep ensure -v -no-vendor $(dep-update)
116116

117+
rebuild-schema:
118+
go generate ./cmd/juju/commands/describeapi.go
119+
117120
# Install packages required to develop Juju and run tests. The stable
118121
# PPA includes the required mongodb-server binaries.
119122
install-dependencies:

cmd/juju/commands/describeapi.go

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,24 @@
44
package commands
55

66
import (
7-
"reflect"
8-
9-
"github.com/bcsaller/jsonschema"
107
"github.com/juju/cmd"
11-
"github.com/juju/errors"
128
"github.com/juju/gnuflag"
139

14-
"github.com/juju/juju/apiserver"
15-
"github.com/juju/juju/apiserver/facade"
1610
jujucmd "github.com/juju/juju/cmd"
1711
"github.com/juju/juju/cmd/modelcmd"
18-
"github.com/juju/juju/cmd/output"
19-
"github.com/juju/juju/rpc/rpcreflect"
2012
)
2113

14+
//go:generate go run ../../../generate/schemagen/schemagen.go allFacadesSchema commands describeapi_schemagen.go
15+
2216
// newDescribeAPICommand returns a full description of the api-servers
2317
// AllFacades information as a JSON schema.
2418
func newDescribeAPICommon() cmd.Command {
25-
return modelcmd.Wrap(&describeAPICommand{})
26-
}
27-
28-
//go:generate mockgen -package commands -destination describeapi_mock.go github.com/juju/juju/cmd/juju/commands APIServer,Registry
29-
type APIServer interface {
30-
AllFacades() Registry
31-
Close() error
32-
}
33-
34-
type Registry interface {
35-
List() []facade.Description
36-
GetType(name string, version int) (reflect.Type, error)
19+
return &describeAPICommand{}
3720
}
3821

3922
type describeAPICommand struct {
40-
modelcmd.ModelCommandBase
41-
out cmd.Output
42-
apiServer APIServer
23+
modelcmd.CommandBase
24+
out cmd.Output
4325
}
4426

4527
const describeAPIHelpDoc = `
@@ -62,8 +44,7 @@ func (c *describeAPICommand) Info() *cmd.Info {
6244

6345
// SetFlags implements Command.
6446
func (c *describeAPICommand) SetFlags(f *gnuflag.FlagSet) {
65-
c.ModelCommandBase.SetFlags(f)
66-
c.out.AddFlags(f, "json", output.DefaultFormatters)
47+
c.CommandBase.SetFlags(f)
6748
}
6849

6950
// Init implements Command.
@@ -73,51 +54,6 @@ func (c *describeAPICommand) Init(args []string) error {
7354

7455
// Run implements Command.
7556
func (c *describeAPICommand) Run(ctx *cmd.Context) error {
76-
client, err := c.getAPI()
77-
if err != nil {
78-
return errors.Trace(err)
79-
}
80-
defer client.Close()
81-
82-
registry := client.AllFacades()
83-
facades := registry.List()
84-
result := make([]FacadeSchema, len(facades))
85-
for i, facade := range facades {
86-
// select the latest version from the facade list
87-
version := facade.Versions[len(facade.Versions)-1]
88-
89-
result[i].Name = facade.Name
90-
result[i].Version = version
91-
92-
kind, err := registry.GetType(facade.Name, version)
93-
if err != nil {
94-
return errors.Annotatef(err, "getting type for facade %s at version %d", facade.Name, version)
95-
}
96-
objType := rpcreflect.ObjTypeOf(kind)
97-
result[i].Schema = jsonschema.ReflectFromObjType(objType)
98-
}
99-
return c.out.Write(ctx, result)
100-
}
101-
102-
func (c *describeAPICommand) getAPI() (APIServer, error) {
103-
if c.apiServer != nil {
104-
return c.apiServer, nil
105-
}
106-
return apiServerShim{}, nil
107-
}
108-
109-
type FacadeSchema struct {
110-
Name string
111-
Version int
112-
Schema *jsonschema.Schema
113-
}
114-
115-
type apiServerShim struct{}
116-
117-
func (apiServerShim) AllFacades() Registry {
118-
return apiserver.AllFacades()
119-
}
120-
121-
func (apiServerShim) Close() error {
122-
return nil
57+
_, err := ctx.Stdout.Write([]byte(allFacadesSchema))
58+
return err
12359
}

0 commit comments

Comments
 (0)