-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds new registry commands to get, create, delete and update plugins.
- Loading branch information
1 parent
d12a559
commit 23680c3
Showing
14 changed files
with
708 additions
and
6 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
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
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
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
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
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
185 changes: 185 additions & 0 deletions
185
private/buf/cmd/buf/command/registry/plugin/plugincreate/plugincreate.go
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,185 @@ | ||
// Copyright 2020-2024 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package plugincreate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
ownerv1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/owner/v1" | ||
pluginv1beta1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/plugin/v1beta1" | ||
"connectrpc.com/connect" | ||
"github.com/bufbuild/buf/private/buf/bufcli" | ||
"github.com/bufbuild/buf/private/buf/bufprint" | ||
"github.com/bufbuild/buf/private/bufpkg/bufparse" | ||
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin" | ||
"github.com/bufbuild/buf/private/pkg/app/appcmd" | ||
"github.com/bufbuild/buf/private/pkg/app/appext" | ||
"github.com/bufbuild/buf/private/pkg/stringutil" | ||
"github.com/bufbuild/buf/private/pkg/syserror" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
const ( | ||
formatFlagName = "format" | ||
visibilityFlagName = "visibility" | ||
defaultLabeFlagName = "default-label-name" | ||
typeFlagName = "type" | ||
|
||
defaultDefaultLabel = "main" | ||
|
||
pluginTypeCheck = "check" | ||
) | ||
|
||
var ( | ||
allPluginTypeStrings = []string{ | ||
pluginTypeCheck, | ||
} | ||
) | ||
|
||
// NewCommand returns a new Command. | ||
func NewCommand( | ||
name string, | ||
builder appext.SubCommandBuilder, | ||
) *appcmd.Command { | ||
flags := newFlags() | ||
return &appcmd.Command{ | ||
Use: name + " <remote/owner/plugin>", | ||
Short: "Create a BSR plugin", | ||
Args: appcmd.ExactArgs(1), | ||
Run: builder.NewRunFunc( | ||
func(ctx context.Context, container appext.Container) error { | ||
return run(ctx, container, flags) | ||
}, | ||
), | ||
BindFlags: flags.Bind, | ||
} | ||
} | ||
|
||
type flags struct { | ||
Format string | ||
Visibility string | ||
DefautlLabel string | ||
Type string | ||
} | ||
|
||
func newFlags() *flags { | ||
return &flags{} | ||
} | ||
|
||
func (f *flags) Bind(flagSet *pflag.FlagSet) { | ||
bufcli.BindVisibility(flagSet, &f.Visibility, visibilityFlagName, false) | ||
flagSet.StringVar( | ||
&f.Format, | ||
formatFlagName, | ||
bufprint.FormatText.String(), | ||
fmt.Sprintf(`The output format to use. Must be one of %s`, bufprint.AllFormatsString), | ||
) | ||
flagSet.StringVar( | ||
&f.DefautlLabel, | ||
defaultLabeFlagName, | ||
defaultDefaultLabel, | ||
"The default label name of the module", | ||
) | ||
flagSet.StringVar( | ||
&f.Type, | ||
typeFlagName, | ||
"", | ||
fmt.Sprintf( | ||
"The type of the plugin. Must be one of %s", | ||
stringutil.SliceToString(allPluginTypeStrings), | ||
), | ||
) | ||
_ = appcmd.MarkFlagRequired(flagSet, typeFlagName) | ||
} | ||
|
||
func run( | ||
ctx context.Context, | ||
container appext.Container, | ||
flags *flags, | ||
) error { | ||
pluginFullName, err := bufparse.ParseFullName(container.Arg(0)) | ||
if err != nil { | ||
return appcmd.WrapInvalidArgumentError(err) | ||
} | ||
visibility, err := bufcli.VisibilityFlagToPluginVisibilityAllowUnspecified(flags.Visibility) | ||
if err != nil { | ||
return appcmd.WrapInvalidArgumentError(err) | ||
} | ||
format, err := bufprint.ParseFormat(flags.Format) | ||
if err != nil { | ||
return appcmd.WrapInvalidArgumentError(err) | ||
} | ||
pluginType, err := typeFlagToPluginType(flags.Type) | ||
if err != nil { | ||
return appcmd.WrapInvalidArgumentError(err) | ||
} | ||
|
||
clientConfig, err := bufcli.NewConnectClientConfig(container) | ||
if err != nil { | ||
return err | ||
} | ||
pluginServiceClient := bufregistryapiplugin.NewClientProvider(clientConfig). | ||
V1Beta1PluginServiceClient(pluginFullName.Registry()) | ||
|
||
pluginResponse, err := pluginServiceClient.CreatePlugins(ctx, connect.NewRequest( | ||
&pluginv1beta1.CreatePluginsRequest{ | ||
Values: []*pluginv1beta1.CreatePluginsRequest_Value{ | ||
{ | ||
OwnerRef: &ownerv1.OwnerRef{ | ||
Value: &ownerv1.OwnerRef_Name{ | ||
Name: pluginFullName.Owner(), | ||
}, | ||
}, | ||
Name: pluginFullName.Name(), | ||
Visibility: visibility, | ||
Type: pluginType, | ||
}, | ||
}, | ||
}, | ||
)) | ||
if err != nil { | ||
if connect.CodeOf(err) == connect.CodeAlreadyExists { | ||
return bufcli.NewPluginNameAlreadyExistsError(pluginFullName.String()) | ||
} | ||
return err | ||
} | ||
plugins := pluginResponse.Msg.Plugins | ||
if len(plugins) != 1 { | ||
return syserror.Newf("unexpected number of plugins returned from server: %d", len(plugins)) | ||
} | ||
if format == bufprint.FormatText { | ||
_, err = fmt.Fprintf(container.Stdout(), "Created %s.\n", pluginFullName) | ||
if err != nil { | ||
return syserror.Wrap(err) | ||
} | ||
return nil | ||
} | ||
return bufprint.PrintNames( | ||
container.Stdout(), | ||
format, | ||
bufprint.NewPluginEntity(plugins[0], pluginFullName), | ||
) | ||
} | ||
|
||
// typeFlagToPluginType parses the given string as a pluginv1.PluginType. | ||
func typeFlagToPluginType(pluginType string) (pluginv1beta1.PluginType, error) { | ||
switch pluginType { | ||
case pluginTypeCheck: | ||
return pluginv1beta1.PluginType_PLUGIN_TYPE_CHECK, nil | ||
default: | ||
return 0, fmt.Errorf("invalid plugin type: %s", pluginType) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
private/buf/cmd/buf/command/registry/plugin/plugincreate/usage.gen.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.