Skip to content

Initialize klog with (go)flags and add these flags to pflag as well #71

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

Merged
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
29 changes: 27 additions & 2 deletions cmd/shp/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
goflag "flag"
"fmt"
"os"

"github.com/spf13/pflag"

"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2"

"github.com/shipwright-io/cli/pkg/shp/cmd"
)
Expand All @@ -15,8 +17,8 @@ import (
const ApplicationName = "shp"

func main() {
flags := pflag.NewFlagSet(ApplicationName, pflag.ExitOnError)
pflag.CommandLine = flags
initGoFlags()
initPFlags()

streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
rootCmd := cmd.NewCmdSHP(&streams)
Expand All @@ -25,3 +27,26 @@ func main() {
os.Exit(1)
}
}

// initGoFlags initializes the flag sets for klog.
// Any flags for "-h" or "--help" are ignored because pflag will show the usage later with all subcommands.
func initGoFlags() {
flagset := goflag.NewFlagSet(ApplicationName, goflag.ContinueOnError)
goflag.CommandLine = flagset
klog.InitFlags(flagset)

args := []string{}
for _, arg := range os.Args[1:] {
if arg != "-h" && arg != "--help" {
args = append(args, arg)
}
}
flagset.Parse(args)
}

// initPFlags initializes the pflags used by Cobra subcommands.
func initPFlags() {
flags := pflag.NewFlagSet(ApplicationName, pflag.ExitOnError)
flags.AddGoFlagSet(goflag.CommandLine)
pflag.CommandLine = flags
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
k8s.io/apimachinery v0.20.6
k8s.io/cli-runtime v0.20.2
k8s.io/client-go v0.20.6
k8s.io/klog/v2 v2.5.0
k8s.io/utils v0.0.0-20210629042839-4a2b36d8d73f
)

Expand Down
52 changes: 47 additions & 5 deletions test/e2e/e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,58 @@ teardown() {
assert_success
}

@test "shp --help lists all available commands" {
run shp --help
assert_success
assert_line "Available Commands:"
assert_line " build Manage Builds"
assert_line " buildrun Manage BuildRuns"
assert_line " completion generate the autocompletion script for the specified shell"
assert_line " help Help about any command"
}

@test "shp --help lists some common flags" {
run shp --help
assert_success
assert_line --regexp "-s, --server string [ ]+The address and port of the Kubernetes API server"
assert_line --regexp "--user string [ ]+The name of the kubeconfig user to use"
assert_line --regexp "--token string [ ]+Bearer token for authentication to the API server"
assert_line --regexp "-n, --namespace string [ ]+If present, the namespace scope for this CLI request"
}

@test "shp --help lists also logging flags" {
run shp --help
assert_success
assert_line --regexp "-v, --v Level [ ]+number for the log level verbosity"
assert_line --regexp "--log_file string [ ]+If non-empty, use this log file"
}

@test "shp [build/buildrun] create should not error when a name is specified" {
# generate random names for our build and buildrun
build_name=$(random_name)
build_name=$(random_name)
buildrun_name=$(random_name)

# ensure that shp build create does not give an error when a build_name is specified
run shp build create ${build_name} --source-url=url --output-image=image
assert_success
run shp build create ${build_name} --source-url=url --output-image=image
assert_success

# ensure that shp buildrun create does not give an error when a buildrun_name is specified
run shp buildrun create ${buildrun_name} --buildref-name=${build_name}
run shp buildrun create ${buildrun_name} --buildref-name=${build_name}
assert_success
}
}

@test "shp -v=10 build list can log the kubernetes api communication" {
# ensure that shp command doesn't log the api calls by default
run shp build list
assert_success
refute_line --regexp "GET .*/apis/shipwright.io/v1alpha1/namespaces/default/builds"
refute_line --partial "Response Headers"
refute_line --partial "Response Body"

# ensure that shp command supports -v loglevel flag.
run shp -v=10 build list
assert_success
assert_line --regexp "GET .*/apis/shipwright.io/v1alpha1/namespaces/default/builds"
assert_line --partial "Response Headers"
assert_line --partial "Response Body"
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ k8s.io/client-go/util/jsonpath
k8s.io/client-go/util/keyutil
k8s.io/client-go/util/workqueue
# k8s.io/klog/v2 v2.5.0
## explicit
k8s.io/klog/v2
# k8s.io/kube-openapi v0.0.0-20210113233702-8566a335510f
k8s.io/kube-openapi/pkg/common
Expand Down