Skip to content

Commit

Permalink
update: updated plugin list command (notaryproject#461)
Browse files Browse the repository at this point in the history
This PR mainly fixed the plugin list command.
Previously, if there's an error on getting a plugin, only the error will
be printed out.
In this PR, on error, the pluginName will also be printed out along with
the error.
(This PR also includes some of the minor cleanups of notation CLI.)

Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts authored Dec 2, 2022
1 parent 86287b5 commit a6dbab8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 39 deletions.
7 changes: 4 additions & 3 deletions cmd/notation/cert/generateTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ func generateTestCert(opts *certGenerateTestOpts) error {

// write private key
relativeKeyPath, relativeCertPath := dir.LocalKeyPath(name)
keyPath, err := dir.ConfigFS().SysPath(relativeKeyPath)
configFS := dir.ConfigFS()
keyPath, err := configFS.SysPath(relativeKeyPath)
if err != nil {
return err
}
certPath, err := dir.ConfigFS().SysPath(relativeCertPath)
certPath, err := configFS.SysPath(relativeCertPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -164,7 +165,7 @@ func generateSelfSignedCert(privateKey *rsa.PrivateKey, name string) (testhelper

func addKeyToSigningKeys(signingKeys *config.SigningKeys, key config.KeySuite, markDefault bool) error {
if slices.Contains(signingKeys.Keys, key.Name) {
return errors.New(key.Name + ": already exists")
return fmt.Errorf("signing key with name %q already exists", key.Name)
}
signingKeys.Keys = append(signingKeys.Keys, key)
if markDefault {
Expand Down
9 changes: 5 additions & 4 deletions cmd/notation/cert/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func certListCommand(opts *certListOpts) *cobra.Command {
func listCerts(opts *certListOpts) error {
namedStore := opts.namedStore
storeType := opts.storeType
configFS := dir.ConfigFS()

// List all certificates under truststore/x509, display empty if there's
// no certificate yet
if namedStore == "" && storeType == "" {
path, err := dir.ConfigFS().SysPath(dir.TrustStoreDir, "x509")
path, err := configFS.SysPath(dir.TrustStoreDir, "x509")
if err := truststore.CheckNonErrNotExistError(err); err != nil {
return err
}
Expand All @@ -52,7 +53,7 @@ func listCerts(opts *certListOpts) error {
// List all certificates under truststore/x509/storeType/namedStore,
// display empty if there's no such certificate
if namedStore != "" && storeType != "" {
path, err := dir.ConfigFS().SysPath(dir.TrustStoreDir, "x509", storeType, namedStore)
path, err := configFS.SysPath(dir.TrustStoreDir, "x509", storeType, namedStore)
if err := truststore.CheckNonErrNotExistError(err); err != nil {
return err
}
Expand All @@ -66,7 +67,7 @@ func listCerts(opts *certListOpts) error {
// List all certificates under x509/storeType, display empty if
// there's no certificate yet
if storeType != "" {
path, err := dir.ConfigFS().SysPath(dir.TrustStoreDir, "x509", storeType)
path, err := configFS.SysPath(dir.TrustStoreDir, "x509", storeType)
if err := truststore.CheckNonErrNotExistError(err); err != nil {
return err
}
Expand All @@ -77,7 +78,7 @@ func listCerts(opts *certListOpts) error {
// List all certificates under named store namedStore, display empty if
// there's no such certificate
for _, t := range notationgoTruststore.Types {
path, err := dir.ConfigFS().SysPath(dir.TrustStoreDir, "x509", string(t), namedStore)
path, err := configFS.SysPath(dir.TrustStoreDir, "x509", string(t), namedStore)
if err := truststore.CheckNonErrNotExistError(err); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func addExternalKey(ctx context.Context, opts *keyAddOpts, pluginName, keyName s

func addKeyCore(signingKeys *config.SigningKeys, key config.KeySuite, markDefault bool) error {
if slices.Contains(signingKeys.Keys, key.Name) {
return errors.New(key.Name + ": already exists")
return fmt.Errorf("signing key with name %q already exists", key.Name)
}
signingKeys.Keys = append(signingKeys.Keys, key)
if markDefault {
Expand Down
27 changes: 20 additions & 7 deletions cmd/notation/plugin.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"fmt"
"os"
"text/tabwriter"

"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/plugin"
"github.com/notaryproject/notation/internal/ioutil"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -35,12 +37,23 @@ func listPlugins(command *cobra.Command) error {
if err != nil {
return err
}
var plugins []plugin.Plugin
var errors []error

tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(tw, "NAME\tDESCRIPTION\tVERSION\tCAPABILITIES\tERROR\t")

var pl plugin.Plugin
var resp *proto.GetMetadataResponse
for _, n := range pluginNames {
pl, err := mgr.Get(command.Context(), n)
errors = append(errors, err)
plugins = append(plugins, pl)
pl, err = mgr.Get(command.Context(), n)
metaData := &proto.GetMetadataResponse{}
if err == nil {
resp, err = pl.GetMetadata(command.Context(), &proto.GetMetadataRequest{})
if err == nil {
metaData = resp
}
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%v\t%v\t\n",
n, metaData.Description, metaData.Version, metaData.Capabilities, err)
}
return ioutil.PrintPlugins(command.Context(), os.Stdout, plugins, errors)
return tw.Flush()
}
4 changes: 2 additions & 2 deletions cmd/notation/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func runVerify(command *cobra.Command, opts *verifyOpts) error {
return err
}
authClient, plainHTTP, _ := getAuthClient(&opts.SecureFlagOpts, ref)
remote_repo := remote.Repository{
remoteRepo := remote.Repository{
Client: authClient,
Reference: ref,
PlainHTTP: plainHTTP,
}
repo := notationregistry.NewRepository(&remote_repo)
repo := notationregistry.NewRepository(&remoteRepo)

// set up verification plugin config.
configs, err := cmd.ParseFlagPluginConfig(opts.pluginConfig)
Expand Down
22 changes: 0 additions & 22 deletions internal/ioutil/print.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
package ioutil

import (
"context"
"fmt"
"io"
"text/tabwriter"

"github.com/notaryproject/notation-go"
"github.com/notaryproject/notation-go/config"
"github.com/notaryproject/notation-go/plugin"
"github.com/notaryproject/notation-go/plugin/proto"
)

func newTabWriter(w io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
}

func PrintPlugins(ctx context.Context, w io.Writer, v []plugin.Plugin, errors []error) error {
tw := newTabWriter(w)
fmt.Fprintln(tw, "NAME\tDESCRIPTION\tVERSION\tCAPABILITIES\tERROR\t")
for ind, p := range v {
metaData := proto.GetMetadataResponse{}
if p != nil {
req := &proto.GetMetadataRequest{}
metadata, err := p.GetMetadata(ctx, req)
if err == nil {
metaData = *metadata
}
errors[ind] = err
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%v\t%v\t\n",
metaData.Name, metaData.Description, metaData.Version, metaData.Capabilities, errors[ind])
}
return tw.Flush()
}

func PrintKeyMap(w io.Writer, target string, v []config.KeySuite) error {
tw := newTabWriter(w)
fmt.Fprintln(tw, "NAME\tKEY PATH\tCERTIFICATE PATH\tID\tPLUGIN NAME\t")
Expand Down

0 comments on commit a6dbab8

Please sign in to comment.