Skip to content

Commit

Permalink
refactor: Clean up log & version flags
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Toh <tohjustin@hotmail.com>
  • Loading branch information
tohjustin committed Oct 3, 2021
1 parent a8cd919 commit 513e4fd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
19 changes: 19 additions & 0 deletions cmd/kube-lineage/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"

"github.com/spf13/cobra"

"github.com/tohjustin/kube-lineage/internal/log"
"github.com/tohjustin/kube-lineage/internal/version"
)

func addLogFlags(cmd *cobra.Command) {
log.AddFlags(cmd.Flags())
}

func addVersionFlags(cmd *cobra.Command) {
cmd.SetVersionTemplate("{{printf \"%s\" .Version}}\n")
cmd.Version = fmt.Sprintf("%#v", version.Get())
}
6 changes: 2 additions & 4 deletions cmd/kube-lineage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2"

"github.com/tohjustin/kube-lineage/pkg/cmd/lineage"
)

func New(streams genericclioptions.IOStreams) *cobra.Command {
cmd := lineage.New(streams)
addVersionFlag(cmd)

addLogFlags(cmd)
addVersionFlags(cmd)
return cmd
}

Expand All @@ -25,7 +24,6 @@ func main() {
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
rootCmd := New(streams)

klog.V(4).Infof("Version: %s", getVersion())
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
18 changes: 0 additions & 18 deletions cmd/kube-lineage/version.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/cmd/lineage/lineage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/tohjustin/kube-lineage/internal/graph"
"github.com/tohjustin/kube-lineage/internal/log"
lineageprinters "github.com/tohjustin/kube-lineage/internal/printers"
)

Expand Down Expand Up @@ -143,6 +142,7 @@ func New(streams genericclioptions.IOStreams) *cobra.Command {
DisableSuggestions: true,
SilenceUsage: true,
Run: func(c *cobra.Command, args []string) {
klog.V(4).Infof("Version: %s", c.Version)
cmdutil.CheckErr(o.Complete(c, args))
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
Expand All @@ -151,7 +151,6 @@ func New(streams genericclioptions.IOStreams) *cobra.Command {

o.ConfigFlags.AddFlags(cmd.Flags())
o.PrintFlags.AddFlags(cmd.Flags())
log.AddFlags(cmd.Flags())

return cmd
}
Expand Down

0 comments on commit 513e4fd

Please sign in to comment.