Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to AddFlags pattern. #791

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/cosign/cli/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ func addAttest(topLevel *cobra.Command) {
return nil
},
}
options.AddAttestOptions(cmd, o)
o.AddFlags(cmd)
topLevel.AddCommand(cmd)
}
2 changes: 1 addition & 1 deletion cmd/cosign/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func New() *cobra.Command {
return nil // TODO: use cobra to output help.
},
}
options.AddRootOptions(cmd, ro)
ro.AddFlags(cmd)

// Add sub-commands.
addPublicKey(cmd)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ to sign payloads with your own tooling or algorithms.`,
},
}

options.AddGenerateOptions(cmd, o)
o.AddFlags(cmd)
topLevel.AddCommand(cmd)
}
2 changes: 1 addition & 1 deletion cmd/cosign/cli/generate_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ CAVEATS:
},
}

options.AddGenerateKeyPairOptions(cmd, o)
o.AddFlags(cmd)
topLevel.AddCommand(cmd)
}
10 changes: 6 additions & 4 deletions cmd/cosign/cli/options/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ type AnnotationOptions struct {
Annotations []string
}

func (s *AnnotationOptions) AnnotationsMap() (sigs.AnnotationsMap, error) {
var _ Interface = (*AnnotationOptions)(nil)

func (o *AnnotationOptions) AnnotationsMap() (sigs.AnnotationsMap, error) {
ann := sigs.AnnotationsMap{}
for _, a := range s.Annotations {
for _, a := range o.Annotations {
kv := strings.Split(a, "=")
if len(kv) != 2 {
return ann, fmt.Errorf("unable to parse annotation: %s", a)
Expand All @@ -44,8 +46,8 @@ func (s *AnnotationOptions) AnnotationsMap() (sigs.AnnotationsMap, error) {
return ann, nil
}

// AddAnnotationOptions adds annotation options to cmd.
func AddAnnotationOptions(cmd *cobra.Command, o *AnnotationOptions) {
// AddFlags implements Inteface
func (o *AnnotationOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringSliceVarP(&o.Annotations, "annotations", "a", nil,
"extra key=value pairs to sign")
}
17 changes: 9 additions & 8 deletions cmd/cosign/cli/options/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ type AttestOptions struct {
RegistryOpts RegistryOpts
}

// AddAttestOptions adds the sign command options to cmd.
func AddAttestOptions(cmd *cobra.Command, o *AttestOptions) {
var _ Interface = (*AttestOptions)(nil)

// AddFlags implements Interface
func (o *AttestOptions) AddFlags(cmd *cobra.Command) {
o.SecurityKey.AddFlags(cmd)
o.Predicate.AddFlags(cmd)
o.Fulcio.AddFlags(cmd)
// TODO(n3wscott): We need o.RegistryOpts.AddFlags(cmd)

cmd.Flags().StringVar(&o.Key, "key", "",
"path to the private key file, KMS URI or Kubernetes Secret")

Expand All @@ -52,10 +59,4 @@ func AddAttestOptions(cmd *cobra.Command, o *AttestOptions) {

cmd.Flags().BoolVar(&o.RegistryOpts.AllowInsecure, "allow-insecure-registry", false,
"whether to allow insecure connections to registries. Don't use this for anything but testing")

AddSecurityKeyOptions(cmd, &o.SecurityKey)

AddPredicateOptions(cmd, &o.Predicate)

AddFulcioOptions(cmd, &o.Fulcio)
}
6 changes: 4 additions & 2 deletions cmd/cosign/cli/options/fulcio.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ type FulcioOptions struct {
IdentityToken string
}

// AddFulcioOptions adds the Fulcio related options to cmd.
func AddFulcioOptions(cmd *cobra.Command, o *FulcioOptions) {
var _ Interface = (*FulcioOptions)(nil)

// AddFlags implements Interface
func (o *FulcioOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.URL, "fulcio-url", fulcioclient.SigstorePublicServerURL,
"[EXPERIMENTAL] address of sigstore PKI server")

Expand Down
8 changes: 5 additions & 3 deletions cmd/cosign/cli/options/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ type GenerateOptions struct {
RegistryOpts RegistryOpts
}

// AddGenerateOptions adds the generate command options to cmd.
func AddGenerateOptions(cmd *cobra.Command, o *GenerateOptions) {
AddAnnotationOptions(cmd, &o.AnnotationOptions)
var _ Interface = (*GenerateOptions)(nil)

// AddFlags implements Interface
func (o *GenerateOptions) AddFlags(cmd *cobra.Command) {
o.AnnotationOptions.AddFlags(cmd)

cmd.Flags().BoolVar(&o.RegistryOpts.AllowInsecure, "allow-insecure-registry", false,
"whether to allow insecure connections to registries. Don't use this for anything but testing")
Expand Down
6 changes: 4 additions & 2 deletions cmd/cosign/cli/options/generate_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type GenerateKeyPairOptions struct {
KMS string
}

// AddGenerateKeyPairOptions adds the generate-key-pair command options to cmd.
func AddGenerateKeyPairOptions(cmd *cobra.Command, o *GenerateKeyPairOptions) {
var _ Interface = (*GenerateKeyPairOptions)(nil)

// AddFlags implements Interface
func (o *GenerateKeyPairOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.KMS, "kms", "",
"create key pair in KMS service to use for signing")
}
6 changes: 4 additions & 2 deletions cmd/cosign/cli/options/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ type OIDCOptions struct {
ClientSecret string
}

// AddOIDCOptions adds the OIDC related options to cmd.
func AddOIDCOptions(cmd *cobra.Command, o *OIDCOptions) {
var _ Interface = (*OIDCOptions)(nil)

// AddFlags implements Interface
func (o *OIDCOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.Issuer, "oidc-issuer", "https://oauth2.sigstore.dev/auth",
"[EXPERIMENTAL] OIDC provider to be used to issue ID token")

Expand Down
23 changes: 23 additions & 0 deletions cmd/cosign/cli/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Copyright 2021 The Sigstore Authors.
//
// 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 options

import "github.com/spf13/cobra"

type Interface interface {
// AddFlags adds this options' flags to the cobra command.
AddFlags(cmd *cobra.Command)
}
6 changes: 4 additions & 2 deletions cmd/cosign/cli/options/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type PredicateOptions struct {
Type string
}

// AddPredicateOptions adds the predicate related options to cmd.
func AddPredicateOptions(cmd *cobra.Command, o *PredicateOptions) {
var _ Interface = (*PredicateOptions)(nil)

// AddFlags implements Interface
func (o *PredicateOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.Path, "predicate", "",
"path to the predicate file.")

Expand Down
10 changes: 6 additions & 4 deletions cmd/cosign/cli/options/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ type PublicKeyOptions struct {
OutFile string
}

// AddPublicKeyOptions adds the public-key command options to cmd.
func AddPublicKeyOptions(cmd *cobra.Command, o *PublicKeyOptions) {
var _ Interface = (*PublicKeyOptions)(nil)

// AddFlags implements Interface
func (o *PublicKeyOptions) AddFlags(cmd *cobra.Command) {
o.SecurityKey.AddFlags(cmd)

cmd.Flags().StringVar(&o.Key, "key", "",
"path to the private key file, KMS URI or Kubernetes Secret")

AddSecurityKeyOptions(cmd, &o.SecurityKey)

cmd.Flags().StringVar(&o.OutFile, "outfile", "",
"path to a payload file to use rather than generating one")
}
6 changes: 4 additions & 2 deletions cmd/cosign/cli/options/rekor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ type RekorOptions struct {
URL string
}

// AddRekorOptions adds the Rektor related options to cmd.
func AddRekorOptions(cmd *cobra.Command, o *RekorOptions) {
var _ Interface = (*RekorOptions)(nil)

// AddFlags implements Interface
func (o *RekorOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.URL, "rekor-url", "https://rekor.sigstore.dev",
"[EXPERIMENTAL] address of rekor STL server")
}
5 changes: 4 additions & 1 deletion cmd/cosign/cli/options/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ type RootOptions struct {
Verbose bool
}

func AddRootOptions(cmd *cobra.Command, o *RootOptions) {
var _ Interface = (*RootOptions)(nil)

// AddFlags implements Interface
func (o *RootOptions) AddFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&o.OutputFile, "output-file", "",
"log output to a file")

Expand Down
6 changes: 4 additions & 2 deletions cmd/cosign/cli/options/security_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type SecurityKeyOptions struct {
Slot string
}

// AddSecurityKeyOptions adds the security key related options to cmd.
func AddSecurityKeyOptions(cmd *cobra.Command, o *SecurityKeyOptions) {
var _ Interface = (*SecurityKeyOptions)(nil)

// AddFlags implements Interface
func (o *SecurityKeyOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&o.Use, "sk", false,
"whether to use a hardware security key")

Expand Down
22 changes: 10 additions & 12 deletions cmd/cosign/cli/options/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ type SignOptions struct {
RegistryOpts RegistryOpts
}

// AddSignOptions adds the sign command options to cmd.
func AddSignOptions(cmd *cobra.Command, o *SignOptions) {
var _ Interface = (*SignOptions)(nil)

// AddFlags implements Interface
func (o *SignOptions) AddFlags(cmd *cobra.Command) {
o.Rektor.AddFlags(cmd)
n3wscott marked this conversation as resolved.
Show resolved Hide resolved
o.Fulcio.AddFlags(cmd)
o.OIDC.AddFlags(cmd)
o.SecurityKey.AddFlags(cmd)
o.AnnotationOptions.AddFlags(cmd)
mattmoor marked this conversation as resolved.
Show resolved Hide resolved

cmd.Flags().StringVar(&o.Key, "key", "",
"path to the private key file, KMS URI or Kubernetes Secret")

Expand All @@ -50,8 +58,6 @@ func AddSignOptions(cmd *cobra.Command, o *SignOptions) {
cmd.Flags().BoolVar(&o.Upload, "upload", true,
"whether to upload the signature")

AddSecurityKeyOptions(cmd, &o.SecurityKey)

cmd.Flags().StringVar(&o.PayloadPath, "payload", "",
"path to a payload file to use rather than generating one")

Expand All @@ -64,14 +70,6 @@ func AddSignOptions(cmd *cobra.Command, o *SignOptions) {
cmd.Flags().StringVar(&o.Attachment, "attachment", "",
"related image attachment to sign (sbom), default none")

AddAnnotationOptions(cmd, &o.AnnotationOptions)

cmd.Flags().BoolVar(&o.RegistryOpts.AllowInsecure, "allow-insecure-registry", false,
"whether to allow insecure connections to registries. Don't use this for anything but testing")

AddRekorOptions(cmd, &o.Rektor)
mattmoor marked this conversation as resolved.
Show resolved Hide resolved

AddFulcioOptions(cmd, &o.Fulcio)

AddOIDCOptions(cmd, &o.OIDC)
}
19 changes: 9 additions & 10 deletions cmd/cosign/cli/options/signblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ type SignBlobOptions struct {
RegistryOpts RegistryOpts
}

// AddSignBlobOptions adds the sign-blob command options to cmd.
func AddSignBlobOptions(cmd *cobra.Command, o *SignBlobOptions) {
var _ Interface = (*SignBlobOptions)(nil)

// AddFlags implements Interface
func (o *SignBlobOptions) AddFlags(cmd *cobra.Command) {
o.SecurityKey.AddFlags(cmd)
o.Fulcio.AddFlags(cmd)
o.Rektor.AddFlags(cmd)
o.OIDC.AddFlags(cmd)

cmd.Flags().StringVar(&o.Key, "key", "",
"path to the private key file, KMS URI or Kubernetes Secret")

Expand All @@ -44,12 +51,4 @@ func AddSignBlobOptions(cmd *cobra.Command, o *SignBlobOptions) {

cmd.Flags().BoolVar(&o.RegistryOpts.AllowInsecure, "allow-insecure-registry", false,
"whether to allow insecure connections to registries. Don't use this for anything but testing")

AddSecurityKeyOptions(cmd, &o.SecurityKey)

AddFulcioOptions(cmd, &o.Fulcio)

AddRekorOptions(cmd, &o.Rektor)

AddOIDCOptions(cmd, &o.OIDC)
}
2 changes: 1 addition & 1 deletion cmd/cosign/cli/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ func addPublicKey(topLevel *cobra.Command) {
},
}

options.AddPublicKeyOptions(cmd, o)
o.AddFlags(cmd)
topLevel.AddCommand(cmd)
}
2 changes: 1 addition & 1 deletion cmd/cosign/cli/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ func addSign(topLevel *cobra.Command) {
},
}

options.AddSignOptions(cmd, o)
o.AddFlags(cmd)
topLevel.AddCommand(cmd)
}
2 changes: 1 addition & 1 deletion cmd/cosign/cli/signblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ func addSignBlob(topLevel *cobra.Command) {
},
}

options.AddSignBlobOptions(cmd, o)
o.AddFlags(cmd)
topLevel.AddCommand(cmd)
}
2 changes: 1 addition & 1 deletion cmd/sget/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func New() *cobra.Command {
return sget.New(ro.ImageRef, ro.PublicKey, wc).Do(context.Background())
},
}
options.AddRootArgs(cmd, ro)
ro.AddFlags(cmd)
return cmd
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/sget/cli/options/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package options

import (
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/spf13/cobra"
)

Expand All @@ -26,7 +27,10 @@ type RootOptions struct {
ImageRef string
}

func AddRootArgs(cmd *cobra.Command, o *RootOptions) {
var _ options.Interface = (*RootOptions)(nil)

// AddFlags implements options.Interface
func (o *RootOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.OutputFile, "output", "o", "",
"output file")

Expand Down