Skip to content

Commit b68f3e6

Browse files
authored
Add info.Doc to plugin help documentation (#6)
1 parent 70a4bf4 commit b68f3e6

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

check/internal/example/cmd/buf-plugin-timestamp-suffix/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ var (
8282
URL: "https://github.com/bufbuild/bufplugin-go",
8383
SPDXLicenseID: "apache-2.0",
8484
LicenseURL: "https://github.com/bufbuild/bufplugin-go/blob/main/LICENSE",
85+
DocShort: `A simple plugin that checks that all google.protobuf.Timestamp fields end in a specific suffix (default is "_time").`,
8586
},
8687
}
8788
)

check/server.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,21 @@ func NewServer(spec *Spec, options ...ServerOption) (pluginrpc.Server, error) {
7777
infov1pluginrpc.RegisterPluginInfoServiceServer(serverRegistrar, pluginInfoServiceServer)
7878
}
7979

80-
return pluginrpc.NewServer(pluginrpcSpec, serverRegistrar)
80+
// Add documentation to -h/--help.
81+
var pluginrpcServerOptions []pluginrpc.ServerOption
82+
if spec.Info != nil {
83+
pluginInfo, err := info.NewPluginInfoForSpec(spec.Info)
84+
if err != nil {
85+
return nil, err
86+
}
87+
if doc := pluginInfo.Doc(); doc != nil {
88+
pluginrpcServerOptions = append(
89+
pluginrpcServerOptions,
90+
pluginrpc.ServerWithDoc(doc.String()),
91+
)
92+
}
93+
}
94+
return pluginrpc.NewServer(pluginrpcSpec, serverRegistrar, pluginrpcServerOptions...)
8195
}
8296

8397
// ServerOption is an option for Server.

info/doc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func (d *doc) toProto() *infov1.Doc {
9393

9494
func (*doc) isDoc() {}
9595

96-
func docForProtoDoc(protoDoc *infov1.Doc) (Doc, error) {
96+
// Need to keep as pointer for Go nil is not nil problem.
97+
func docForProtoDoc(protoDoc *infov1.Doc) (*doc, error) {
9798
if protoDoc == nil {
9899
return nil, nil
99100
}

info/license.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func (l *license) toProto() *infov1.License {
118118

119119
func (*license) isLicense() {}
120120

121-
func licenseForProtoLicense(protoLicense *infov1.License) (License, error) {
121+
// Need to keep as pointer for Go nil is not nil problem.
122+
func licenseForProtoLicense(protoLicense *infov1.License) (*license, error) {
122123
if protoLicense == nil {
123124
return nil, nil
124125
}

info/plugin_info.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ func NewPluginInfoForSpec(spec *Spec) (PluginInfo, error) {
9292
// *** PRIVATE ***
9393

9494
type pluginInfo struct {
95-
url *url.URL
96-
license License
97-
doc Doc
95+
url *url.URL
96+
// Need to keep as pointer for Go nil is not nil problem.
97+
license *license
98+
// Need to keep as pointer for Go nil is not nil problem.
99+
doc *doc
98100
}
99101

100102
func newPluginInfo(
101103
url *url.URL,
102-
license License,
103-
doc Doc,
104+
license *license,
105+
doc *doc,
104106
) (*pluginInfo, error) {
105107
if url != nil && url.Host == "" {
106108
return nil, fmt.Errorf("url %v must be absolute", url)
@@ -117,10 +119,18 @@ func (p *pluginInfo) URL() *url.URL {
117119
}
118120

119121
func (p *pluginInfo) License() License {
122+
// Go nil is not nil problem.
123+
if p.license == nil {
124+
return nil
125+
}
120126
return p.license
121127
}
122128

123129
func (p *pluginInfo) Doc() Doc {
130+
// Go nil is not nil problem.
131+
if p.doc == nil {
132+
return nil
133+
}
124134
return p.doc
125135
}
126136

0 commit comments

Comments
 (0)