From f24105bb8a3c73a62894407b18f356f987d6cb5e Mon Sep 17 00:00:00 2001 From: Jan Steffen Date: Mon, 10 Aug 2020 15:57:14 +0200 Subject: [PATCH] Add option to ignore case --- README.md | 15 +++++++++------ cmd/jp.go | 11 +++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d9752f7..35a2a76 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,11 @@ Usage: jp [flags] Flags: - -h, --help help for jp - -k, --key string Key to search for - -t, --show-type If enabled, will show the type of the value for found path - -v, --show-value If enabled, will show the value for found path + -h, --help help for jp + -i, --ignore-case If enabled, will ignores case when matching key + -k, --key string Key to search for + -t, --show-type If enabled, will show the type of the value for found path + -v, --show-value If enabled, will show the value for found path ``` ## Example @@ -21,11 +22,13 @@ Flags: $ kubectl get node node01 -ojson | jp -k osImage .status.nodeInfo.osImage $ kubectl get node node01 -ojson | jp -k osImage -v -.status.nodeInfo.osImage:Flatcar Container Linux by Kinvolk 2512.2.0 (Oklo) +.status.nodeInfo.osImage:Flatcar Container Linux $ kubectl get node node01 -ojson | jp -k osImage -v -t -.status.nodeInfo.osImage:Flatcar Container Linux by Kinvolk 2512.2.0 (Oklo):string +.status.nodeInfo.osImage:Flatcar Container Linux:string $ kubectl get node node01 -ojson | jp -k osImage -t .status.nodeInfo.osImage:string +kubectl get node node01 -ojson | ./jp.linux -k containerruntimeversion -v -i +.status.nodeInfo.containerRuntimeVersion:docker://47.1.1 ``` ## Releases diff --git a/cmd/jp.go b/cmd/jp.go index 0afb216..2584cc3 100644 --- a/cmd/jp.go +++ b/cmd/jp.go @@ -6,14 +6,16 @@ import ( "flag" "fmt" "os" + "strings" "gopkg.in/spf13/cobra.v0" ) var ( - key string - showValue bool - showType bool + key string + showValue bool + showType bool + ignoreCase bool ) var jpCmd = &cobra.Command{ @@ -55,6 +57,7 @@ func init() { flags.StringVarP(&key, "key", "k", "", "Key to search for") flags.BoolVarP(&showValue, "show-value", "v", false, "If enabled, will show the value for found path") flags.BoolVarP(&showType, "show-type", "t", false, "If enabled, will show the type of the value for found path") + flags.BoolVarP(&ignoreCase, "ignore-case", "i", false, "If enabled, will ignores case when matching key") } func printPaths(v interface{}, key, path string) { @@ -62,7 +65,7 @@ func printPaths(v interface{}, key, path string) { case map[string]interface{}: for mk, mv := range v { p := path + "." + mk - if mk == key { + if (ignoreCase && strings.EqualFold(mk, key)) || mk == key { fmt.Print(p) if showValue { fmt.Printf(":%v", mv)