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

Use cobra cli for docker-generate command #250

Merged
merged 7 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 7 additions & 6 deletions cmd/docker-generate/generate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)

var generateCommand = &cli.Command{
Name: "generate",
Subcommands: []*cli.Command{
manifestCommand,
},
func generateCommand() *cobra.Command {
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
cmd := &cobra.Command{
Use: "generate",
}
cmd.AddCommand(generateManifestCommand())
return cmd
}
14 changes: 5 additions & 9 deletions cmd/docker-generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package main

import (
"log"
"os"

"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)

func main() {
app := &cli.App{
Name: "docker",
Commands: []*cli.Command{
generateCommand,
metadataCommand,
},
cmd := &cobra.Command{
Use: "docker",
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
}
if err := app.Run(os.Args); err != nil {
cmd.AddCommand(generateCommand(), metadataCommand())
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}
28 changes: 13 additions & 15 deletions cmd/docker-generate/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@ import (
"os/exec"

"github.com/notaryproject/notation/pkg/docker"
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)

var manifestCommand = &cli.Command{
Name: "manifest",
Usage: "generates the manifest of a docker image",
ArgsUsage: "[<reference>]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "write to a file instead of stdout",
func generateManifestCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "manifest [reference]",
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
Short: "generates the manifest of a docker image",
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
RunE: func(cmd *cobra.Command, args []string) error {
return generateManifest(cmd)
},
},
Action: generateManifest,
}
cmd.Flags().StringP("output", "o", "", "write to a file instead of stdout")
return cmd
}

func generateManifest(ctx *cli.Context) error {
func generateManifest(cmd *cobra.Command) error {
var reader io.Reader
if reference := ctx.Args().First(); reference != "" {
if reference := cmd.Flags().Arg(0); reference != "" {
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
cmd := exec.Command("docker", "save", reference)
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
Expand All @@ -41,7 +39,7 @@ func generateManifest(ctx *cli.Context) error {
}

var writer io.Writer
if output := ctx.String("output"); output != "" {
if output, _ := cmd.Flags().GetString("output"); output != "" {
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
file, err := os.Create(output)
if err != nil {
return err
Expand Down
69 changes: 69 additions & 0 deletions cmd/docker-generate/manifest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main
chloeyin marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"
)

func TestGenerateManifestCmd(t *testing.T) {
tests := []struct {
expectedOutput string
expectedReference string
args []string
expectedErr bool
}{
{
expectedOutput: "abc",
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
expectedReference: "def",
args: []string{"-o", "abc", "def"},
expectedErr: false,
},
{
expectedOutput: "abc",
expectedReference: "",
args: []string{"-o", "abc"},
expectedErr: false,
},
{
expectedOutput: "",
expectedReference: "def",
args: []string{"def"},
expectedErr: false,
},
{
expectedOutput: "",
expectedReference: "",
args: []string{},
expectedErr: false,
},
{
expectedOutput: "abc",
expectedReference: "def",
args: []string{"def", "--output", "abc"},
expectedErr: false,
},
{
args: []string{"-o", "b", "-n", "x"},
expectedErr: true,
},
}
for _, test := range tests {
cmd := generateManifestCommand()
err := cmd.ParseFlags(test.args)
if err != nil && !test.expectedErr {
t.Fatalf("Test failed with error: %v", err)
}
if err == nil && test.expectedErr {
t.Fatalf("Expect test to error but it didn't: %v", test.args)
}
if err != nil {
continue
}
if output, _ := cmd.Flags().GetString("output"); output != test.expectedOutput {
t.Fatalf("Expect output: %v, got: %v", test.expectedOutput, output)
}
if arg := cmd.Flags().Arg(0); arg != test.expectedReference {
t.Fatalf("Expect reference: %v, got: %v", test.expectedReference, arg)
}
}

}
23 changes: 13 additions & 10 deletions cmd/docker-generate/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ import (
"os"

"github.com/notaryproject/notation/internal/docker"
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)

func metadataCommand() *cobra.Command {
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
cmd := &cobra.Command{
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
Use: docker.PluginMetadataCommandName,
RunE: func(cmd *cobra.Command, args []string) error {
writer := json.NewEncoder(os.Stdout)
return writer.Encode(pluginMetadata)
},
Hidden: true,
}
return cmd
}

var pluginMetadata = docker.PluginMetadata{
SchemaVersion: "0.1.0",
Vendor: "CNCF Notary Project",
Expand All @@ -16,12 +28,3 @@ var pluginMetadata = docker.PluginMetadata{
URL: "https://github.com/notaryproject/notation",
Experimental: true,
}

var metadataCommand = &cli.Command{
Name: docker.PluginMetadataCommandName,
Action: func(ctx *cli.Context) error {
writer := json.NewEncoder(os.Stdout)
return writer.Encode(pluginMetadata)
},
Hidden: true,
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ require (
github.com/notaryproject/notation-core-go v0.0.0-20220712013708-3c4b3efa03c5
github.com/notaryproject/notation-go v0.9.0-alpha.1.0.20220712175603-962d79cd4090
github.com/opencontainers/go-digest v1.0.0
github.com/urfave/cli/v2 v2.10.3
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/urfave/cli/v2 v2.11.0
oras.land/oras-go/v2 v2.0.0-20220620164807-8b2a54608a94
)

Expand All @@ -17,6 +19,7 @@ require (
github.com/docker/docker v20.10.8+incompatible // indirect
github.com/docker/docker-credential-helpers v0.6.4 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/oras-project/artifacts-spec v1.0.0-rc.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -107,15 +108,19 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/urfave/cli/v2 v2.10.3 h1:oi571Fxz5aHugfBAJd5nkwSk3fzATXtMlpxdLylSCMo=
github.com/urfave/cli/v2 v2.10.3/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
github.com/urfave/cli/v2 v2.11.0 h1:c6bD90aLd2iEsokxhxkY5Er0zA2V9fId2aJfwmrF+do=
github.com/urfave/cli/v2 v2.11.0/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
Expand Down
67 changes: 67 additions & 0 deletions internal/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -53,6 +56,70 @@ var (
}
)

var (
PflagKey = &pflag.Flag{
Name: "key",
Shorthand: "k",
Usage: "signing key name",
}
SetPflagKey = func(cmd *cobra.Command) {
cmd.Flags().StringP(PflagKey.Name, PflagKey.Shorthand, "", PflagKey.Usage)
}

PflagKeyFile = &pflag.Flag{
Name: "key-file",
Usage: "signing key file",
}
SetPflagKeyFile = func(cmd *cobra.Command) {
cmd.Flags().String(PflagKeyFile.Name, "", PflagKeyFile.Usage)
}

PflagCertFile = &pflag.Flag{
Name: "cert-file",
Usage: "signing certificate file",
}
SetPflagCertFile = func(cmd *cobra.Command) {
cmd.Flags().String(PflagCertFile.Name, "", PflagCertFile.Usage)
}

PflagTimestamp = &pflag.Flag{
Name: "timestamp",
Shorthand: "t",
Usage: "timestamp the signed signature via the remote TSA",
}
SetPflagTimestamp = func(cmd *cobra.Command) {
cmd.Flags().StringP(PflagTimestamp.Name, PflagTimestamp.Shorthand, "", PflagTimestamp.Usage)
}

PflagExpiry = &pflag.Flag{
Name: "expiry",
Shorthand: "e",
Usage: "expire duration",
}
SetPflagExpiry = func(cmd *cobra.Command) {
cmd.Flags().DurationP(PflagExpiry.Name, PflagExpiry.Shorthand, time.Duration(0), PflagExpiry.Usage)
}

PflagReference = &pflag.Flag{
Name: "reference",
Shorthand: "r",
Usage: "original reference",
}
SetPflagReference = func(cmd *cobra.Command) {
cmd.Flags().StringP(PflagReference.Name, PflagReference.Shorthand, "", PflagReference.Usage)
}

// TODO: cobra does not support string shorthand
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
PflagPluginConfig = &pflag.Flag{
Name: "pluginConfig",
Shorthand: "c",
chloeyin marked this conversation as resolved.
Show resolved Hide resolved
Usage: "list of comma-separated {key}={value} pairs that are passed as is to the plugin, refer plugin documentation to set appropriate values",
}
SetPflagPluginConfig = func(cmd *cobra.Command) {
cmd.Flags().StringP(PflagPluginConfig.Name, PflagPluginConfig.Shorthand, "", PflagPluginConfig.Usage)
}
)

// KeyValueSlice is a flag with type int
type KeyValueSlice interface {
Set(value string) error
Expand Down