Skip to content

Commit

Permalink
feat: remove urfave cli framework
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Mar 1, 2023
1 parent 47573c5 commit cdd142c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 99 deletions.
33 changes: 4 additions & 29 deletions cmd/notation-azure-kv/key.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"

"github.com/Azure/notation-azure-kv/internal/signature"
"github.com/notaryproject/notation-go/plugin/proto"

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

var describeKeyCommand = &cli.Command{
Name: string(proto.CommandDescribeKey),
Usage: "Azure key description",
Action: runDescribeKey,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Usage: "request json file",
TakesFile: true,
Hidden: true,
},
},
}

func runDescribeKey(ctx *cli.Context) error {
var r io.Reader
if f := ctx.String("file"); f != "" {
var err error
r, err = os.Open(f)
if err != nil {
return err
}
} else {
r = os.Stdin
}
func runDescribeKey(ctx context.Context) error {
r := os.Stdin
var req proto.DescribeKeyRequest
err := json.NewDecoder(r).Decode(&req)
if err != nil {
Expand All @@ -47,7 +22,7 @@ func runDescribeKey(ctx *cli.Context) error {
}
}

resp, err := signature.Key(ctx.Context, &req)
resp, err := signature.Key(ctx, &req)
if err != nil {
var rerr proto.RequestError
if errors.As(err, &rerr) {
Expand Down
47 changes: 36 additions & 11 deletions cmd/notation-azure-kv/main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"

"github.com/Azure/notation-azure-kv/internal/version"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/urfave/cli/v2"
)

func main() {
app := &cli.App{
Name: "notation-azure-kv",
Usage: "Notation - Notary V2 Azure KV plugin",
Version: version.GetVersion(),
Commands: []*cli.Command{
metadataCommand,
signCommand,
describeKeyCommand,
},
if len(os.Args) < 2 {
help()
return
}
if err := app.Run(os.Args); err != nil {
ctx := context.Background()
var err error
switch os.Args[1] {
case string(proto.CommandGetMetadata):
runGetMetadata()
case string(proto.CommandDescribeKey):
err = runDescribeKey(ctx)
case string(proto.CommandGenerateSignature):
err = runSign(ctx)
default:
err = fmt.Errorf("invalid command: %s", os.Args[1])
}

if err != nil {
var reer proto.RequestError
if !errors.As(err, &reer) {
err = proto.RequestError{
Expand All @@ -34,3 +42,20 @@ func main() {
os.Exit(1)
}
}

func help() {
fmt.Printf(`NAME:
notation-azure-kv - Notation - Notary V2 Azure KV plugin
USAGE:
notation-azure-kv [global options] command [command options] [arguments...]
VERSION:
%s
COMMANDS:
get-plugin-metadata Get plugin metadata
generate-signature Sign artifacts with keys in Azure Key Vault
describe-key Azure key description
`, version.GetVersion())
}
19 changes: 1 addition & 18 deletions cmd/notation-azure-kv/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,8 @@ import (

"github.com/Azure/notation-azure-kv/internal/version"
"github.com/notaryproject/notation-go/plugin/proto"

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

var metadataCommand = &cli.Command{
Name: string(proto.CommandGetMetadata),
Usage: "Get plugin metadata",
Action: runGetMetadata,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Usage: "request json file",
TakesFile: true,
Hidden: true,
},
},
}

var metadata []byte

func init() {
Expand All @@ -41,8 +25,7 @@ func init() {
}
}

func runGetMetadata(ctx *cli.Context) error {
func runGetMetadata() {
// write response
os.Stdout.Write(metadata)
return nil
}
33 changes: 4 additions & 29 deletions cmd/notation-azure-kv/sign.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"

"github.com/Azure/notation-azure-kv/internal/signature"
"github.com/notaryproject/notation-go/plugin/proto"

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

var signCommand = &cli.Command{
Name: string(proto.CommandGenerateSignature),
Usage: "Sign artifacts with keys in Azure Key Vault",
Action: runSign,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Usage: "request json file",
TakesFile: true,
Hidden: true,
},
},
}

func runSign(ctx *cli.Context) error {
var r io.Reader
if f := ctx.String("file"); f != "" {
var err error
r, err = os.Open(f)
if err != nil {
return err
}
} else {
r = os.Stdin
}
func runSign(ctx context.Context) error {
r := os.Stdin
var req proto.GenerateSignatureRequest
err := json.NewDecoder(r).Decode(&req)
if err != nil {
Expand All @@ -47,7 +22,7 @@ func runSign(ctx *cli.Context) error {
}
}

resp, err := signature.Sign(ctx.Context, &req)
resp, err := signature.Sign(ctx, &req)
if err != nil {
var rerr proto.RequestError
if errors.As(err, &rerr) {
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/Azure/go-autorest/autorest v0.11.28
github.com/notaryproject/notation-core-go v1.0.0-rc.2
github.com/notaryproject/notation-go v1.0.0-rc.3
github.com/urfave/cli/v2 v2.23.6
golang.org/x/crypto v0.5.0
)

Expand All @@ -21,12 +20,9 @@ require (
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.7.2 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+Z
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -43,16 +41,10 @@ github.com/notaryproject/notation-go v1.0.0-rc.3 h1:J93pnI42xw6UzeeCn8a5r3j1n8n5
github.com/notaryproject/notation-go v1.0.0-rc.3/go.mod h1:IlP9GVzPUavxljgJIWoHY0GY1unlqfee7tIiCbSem1w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/urfave/cli/v2 v2.23.6 h1:iWmtKD+prGo1nKUtLO0Wg4z9esfBM4rAV4QRLQiEmJ4=
github.com/urfave/cli/v2 v2.23.6/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
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=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
Expand Down

0 comments on commit cdd142c

Please sign in to comment.