Skip to content

Commit

Permalink
Move flag importation to the flags package.
Browse files Browse the repository at this point in the history
  • Loading branch information
bormanp committed Jul 26, 2023
1 parent 5e62e6b commit 045de3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
10 changes: 9 additions & 1 deletion flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ import (
"flag"

"github.com/spf13/pflag"
"k8s.io/klog/v2"
)

// Import imports the command line flags from the standard flag package into
// pflag's command line flags.
func Import() {
func Import(defmap map[string]string) {
klog.InitFlags(nil)
for k, v := range defmap {
if f := flag.Lookup(k); f != nil {
f.Value.Set(v)
f.DefValue = v
}
}
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
}
19 changes: 5 additions & 14 deletions kne_cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
Expand All @@ -26,25 +25,17 @@ import (

"github.com/openconfig/kne/cmd"
"github.com/openconfig/kne/flags"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
)

func main() {
// By default, send logs to files and the screen.
// TODO(borman): rework what goes to the screen
// in a nicer format.
klog.InitFlags(nil)
for k, v := range map[string]string{
// Import flags into pflags that are set in other flag packages.
flags.Import(map[string]string{
"logtostderr": "false",
"alsologtostderr": "false",
"stderrthreshold": "info",
} {
if f := flag.Lookup(k); f != nil {
f.Value.Set(v)
f.DefValue = v
}
}
flags.Import()
})
err := cmd.New().ExecuteContext(context.Background())
flushLogs()
if err != nil {
Expand All @@ -61,7 +52,7 @@ func flushLogs() {
// code mimics the decsion that klog makes to determine the logging
// directory.

f := flag.Lookup("log_dir")
f := pflag.Lookup("log_dir")
var logdir string
if f != nil {
logdir = f.Value.String()
Expand Down

0 comments on commit 045de3a

Please sign in to comment.