Skip to content

Commit

Permalink
add syntax highlighting for output
Browse files Browse the repository at this point in the history
fixes itaysk#39
  • Loading branch information
karuppiah7890 committed Dec 30, 2020
1 parent cb31812 commit 88041ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import (
"os/exec"
"unicode"

"github.com/andreazorzetto/yh/highlight"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
)

var outputFormat *string
var inputFile *string
var color *bool

func init() {
outputFormat = rootCmd.PersistentFlags().StringP("output", "o", "yaml", "output format: yaml or json")
inputFile = rootCmd.Flags().StringP("file", "f", "-", "file path to neat, or - to read from stdin")
color = rootCmd.Flags().BoolP("color", "c", false, "colored output with syntax highlighting")
rootCmd.SetOut(os.Stdout)
rootCmd.SetErr(os.Stderr)
rootCmd.MarkFlagFilename("file")
Expand Down Expand Up @@ -74,7 +77,16 @@ kubectl neat -f ./my-pod.json --output yaml`,
if err != nil {
return err
}
cmd.Println(string(out))
if *color {
outreader := bytes.NewReader(out)
highlightedOutput, err := highlight.Highlight(outreader)
if err != nil {
return err
}
cmd.Println(highlightedOutput)
} else {
cmd.Println(string(out))
}
return nil
},
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
k8s.io/apimachinery v0.17.0
k8s.io/client-go v0.17.0
k8s.io/kubernetes v1.17.0
github.com/andreazorzetto/yh v0.4.0
)

replace (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrU
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andreazorzetto/yh v0.4.0 h1:kXQJxhT7v9/JqUNifD+yIBRLKXDWeBgO2/UjxwRQsto=
github.com/andreazorzetto/yh v0.4.0/go.mod h1:c7MhXod3cApIEJDb9VSXA9cZVVaEZbvW+GhMPiVg2tM=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -297,6 +299,8 @@ github.com/libopenstorage/openstorage v1.0.0/go.mod h1:Sp1sIObHjat1BeXhfMqLZ14wn
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/lpabon/godbc v0.1.1/go.mod h1:Jo9QV0cf3U6jZABgiJ2skINAXb9j8m51r07g4KI92ZA=
github.com/lucas-clemente/aes12 v0.0.0-20171027163421-cd47fb39b79f/go.mod h1:JpH9J1c9oX6otFSgdUHwUBUizmKlrMjxWnIAjff4m04=
github.com/lucas-clemente/quic-clients v0.1.0/go.mod h1:y5xVIEoObKqULIKivu+gD/LU90pL73bTdtQjPBvtCBk=
Expand Down

0 comments on commit 88041ed

Please sign in to comment.