cli/command/plugin: minor cleanups: use Println, rename vars

- use Println to print newline instead of custom format
- use dockerCLI with Go's standard camelCase casing.
- suppress some errors to make my IDE and linters happier

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-01 22:48:33 +01:00
parent 35e74d58e3
commit 53aed6119b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 17 additions and 17 deletions

View File

@ -104,7 +104,7 @@ func buildPullConfig(ctx context.Context, dockerCli command.Cli, opts pluginOpti
return options, nil
}
func runInstall(ctx context.Context, dockerCli command.Cli, opts pluginOptions) error {
func runInstall(ctx context.Context, dockerCLI command.Cli, opts pluginOptions) error {
var localName string
if opts.localName != "" {
aref, err := reference.ParseNormalizedNamed(opts.localName)
@ -117,11 +117,11 @@ func runInstall(ctx context.Context, dockerCli command.Cli, opts pluginOptions)
localName = reference.FamiliarString(reference.TagNameOnly(aref))
}
options, err := buildPullConfig(ctx, dockerCli, opts, "plugin install")
options, err := buildPullConfig(ctx, dockerCLI, opts, "plugin install")
if err != nil {
return err
}
responseBody, err := dockerCli.Client().PluginInstall(ctx, localName, options)
responseBody, err := dockerCLI.Client().PluginInstall(ctx, localName, options)
if err != nil {
if strings.Contains(err.Error(), "(image) when fetching") {
return errors.New(err.Error() + " - Use \"docker image pull\"")
@ -129,19 +129,19 @@ func runInstall(ctx context.Context, dockerCli command.Cli, opts pluginOptions)
return err
}
defer responseBody.Close()
if err := jsonstream.Display(ctx, responseBody, dockerCli.Out()); err != nil {
if err := jsonstream.Display(ctx, responseBody, dockerCLI.Out()); err != nil {
return err
}
fmt.Fprintf(dockerCli.Out(), "Installed plugin %s\n", opts.remote) // todo: return proper values from the API for this result
_, _ = fmt.Fprintln(dockerCLI.Out(), "Installed plugin", opts.remote) // todo: return proper values from the API for this result
return nil
}
func acceptPrivileges(dockerCli command.Cli, name string) func(ctx context.Context, privileges types.PluginPrivileges) (bool, error) {
func acceptPrivileges(dockerCLI command.Cli, name string) func(ctx context.Context, privileges types.PluginPrivileges) (bool, error) {
return func(ctx context.Context, privileges types.PluginPrivileges) (bool, error) {
fmt.Fprintf(dockerCli.Out(), "Plugin %q is requesting the following privileges:\n", name)
_, _ = fmt.Fprintf(dockerCLI.Out(), "Plugin %q is requesting the following privileges:\n", name)
for _, privilege := range privileges {
fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
_, _ = fmt.Fprintf(dockerCLI.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
}
return command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), "Do you grant the above permissions?")
return command.PromptForConfirmation(ctx, dockerCLI.In(), dockerCLI.Out(), "Do you grant the above permissions?")
}
}

View File

@ -36,8 +36,8 @@ func newUpgradeCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func runUpgrade(ctx context.Context, dockerCli command.Cli, opts pluginOptions) error {
p, _, err := dockerCli.Client().PluginInspectWithRaw(ctx, opts.localName)
func runUpgrade(ctx context.Context, dockerCLI command.Cli, opts pluginOptions) error {
p, _, err := dockerCLI.Client().PluginInspectWithRaw(ctx, opts.localName)
if err != nil {
return errors.Errorf("error reading plugin data: %v", err)
}
@ -62,9 +62,9 @@ func runUpgrade(ctx context.Context, dockerCli command.Cli, opts pluginOptions)
}
old = reference.TagNameOnly(old)
fmt.Fprintf(dockerCli.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, reference.FamiliarString(old), reference.FamiliarString(remote))
_, _ = fmt.Fprintf(dockerCLI.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, reference.FamiliarString(old), reference.FamiliarString(remote))
if !opts.skipRemoteCheck && remote.String() != old.String() {
r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), "Plugin images do not match, are you sure?")
r, err := command.PromptForConfirmation(ctx, dockerCLI.In(), dockerCLI.Out(), "Plugin images do not match, are you sure?")
if err != nil {
return err
}
@ -73,12 +73,12 @@ func runUpgrade(ctx context.Context, dockerCli command.Cli, opts pluginOptions)
}
}
options, err := buildPullConfig(ctx, dockerCli, opts, "plugin upgrade")
options, err := buildPullConfig(ctx, dockerCLI, opts, "plugin upgrade")
if err != nil {
return err
}
responseBody, err := dockerCli.Client().PluginUpgrade(ctx, opts.localName, options)
responseBody, err := dockerCLI.Client().PluginUpgrade(ctx, opts.localName, options)
if err != nil {
if strings.Contains(err.Error(), "target is image") {
return errors.New(err.Error() + " - Use `docker image pull`")
@ -86,9 +86,9 @@ func runUpgrade(ctx context.Context, dockerCli command.Cli, opts pluginOptions)
return err
}
defer responseBody.Close()
if err := jsonstream.Display(ctx, responseBody, dockerCli.Out()); err != nil {
if err := jsonstream.Display(ctx, responseBody, dockerCLI.Out()); err != nil {
return err
}
fmt.Fprintf(dockerCli.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result
_, _ = fmt.Fprintf(dockerCLI.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result
return nil
}