mirror of
https://github.com/jimeh/evm.git
synced 2026-02-18 23:16:39 +00:00
chore(commands): switch to jimeh/go-render for CLI output rendering
This commit is contained in:
@@ -2,6 +2,7 @@ package commands
|
||||
|
||||
import (
|
||||
"github.com/jimeh/evm/manager"
|
||||
"github.com/jimeh/go-render"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,9 @@ func NewConfig(mgr *manager.Manager) (*cobra.Command, error) {
|
||||
RunE: configRunE(mgr),
|
||||
}
|
||||
|
||||
cmd.Flags().StringP("format", "f", "", "output format (yaml or json)")
|
||||
cmd.Flags().StringP(
|
||||
"format", "f", "yaml", "output format, \"yaml\" or \"json\"",
|
||||
)
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
@@ -23,6 +26,6 @@ func configRunE(mgr *manager.Manager) runEFunc {
|
||||
return func(cmd *cobra.Command, _ []string) error {
|
||||
format := flagString(cmd, "format")
|
||||
|
||||
return render(cmd.OutOrStdout(), format, mgr.Config)
|
||||
return render.Pretty(cmd.OutOrStdout(), format, mgr.Config)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/jimeh/evm/manager"
|
||||
"github.com/jimeh/go-render"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -18,7 +19,9 @@ func NewList(mgr *manager.Manager) (*cobra.Command, error) {
|
||||
RunE: listRunE(mgr),
|
||||
}
|
||||
|
||||
cmd.Flags().StringP("format", "f", "", "output format (yaml or json)")
|
||||
cmd.Flags().StringP(
|
||||
"format", "f", "text", "output format ,\"text\", \"yaml\", or \"json\"",
|
||||
)
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
@@ -33,40 +36,44 @@ func listRunE(mgr *manager.Manager) runEFunc {
|
||||
}
|
||||
|
||||
output := &listOutput{
|
||||
Current: mgr.CurrentVersion(),
|
||||
Current: listOutputCurrent{
|
||||
Version: mgr.CurrentVersion(),
|
||||
SetBy: mgr.CurrentSetBy(),
|
||||
},
|
||||
Versions: versions,
|
||||
}
|
||||
|
||||
return render(cmd.OutOrStdout(), format, output)
|
||||
return render.Pretty(cmd.OutOrStdout(), format, output)
|
||||
}
|
||||
}
|
||||
|
||||
type listOutput struct {
|
||||
Current string `yaml:"current" json:"current"`
|
||||
SetBy string `yaml:"current_set_by,omitempty" json:"current_set_by,omitempty"`
|
||||
Current listOutputCurrent `yaml:"current" json:"current"`
|
||||
Versions []*manager.Version `yaml:"versions" json:"versions"`
|
||||
}
|
||||
|
||||
func (lr *listOutput) WriteTo(w io.Writer) (int64, error) {
|
||||
var b []byte
|
||||
|
||||
for _, ver := range lr.Versions {
|
||||
if lr.Current == ver.Version {
|
||||
b = append(b, []byte("* ")...)
|
||||
} else {
|
||||
b = append(b, []byte(" ")...)
|
||||
}
|
||||
|
||||
b = append(b, []byte(ver.Version)...)
|
||||
if lr.Current == ver.Version && lr.SetBy != "" {
|
||||
b = append(b, []byte(" (set by "+lr.SetBy+")")...)
|
||||
}
|
||||
|
||||
b = append(b, byte('\n'))
|
||||
}
|
||||
|
||||
n, err := w.Write(b)
|
||||
|
||||
return int64(n), err
|
||||
type listOutputCurrent struct {
|
||||
Version string `yaml:"version" json:"version"`
|
||||
SetBy string `yaml:"set_by,omitempty" json:"set_by,omitempty"`
|
||||
}
|
||||
|
||||
func (lo *listOutput) String() string {
|
||||
buf := &strings.Builder{}
|
||||
|
||||
for _, ver := range lo.Versions {
|
||||
if lo.Current.Version == ver.Version {
|
||||
buf.WriteString("* ")
|
||||
} else {
|
||||
buf.WriteString(" ")
|
||||
}
|
||||
|
||||
buf.WriteString(ver.Version)
|
||||
if lo.Current.Version == ver.Version && lo.Current.SetBy != "" {
|
||||
buf.WriteString(" (set by " + lo.Current.SetBy + ")")
|
||||
}
|
||||
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func render(w io.Writer, format string, v interface{}) error {
|
||||
if format == "yaml" || format == "yml" {
|
||||
return renderYAML(w, v)
|
||||
}
|
||||
|
||||
if format == "json" {
|
||||
return renderJSON(w, v)
|
||||
}
|
||||
|
||||
if wt, ok := v.(io.WriterTo); ok {
|
||||
_, err := wt.WriteTo(w)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
if tm, ok := v.(encoding.TextMarshaler); ok {
|
||||
b, err := tm.MarshalText()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(b)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return renderYAML(w, v)
|
||||
}
|
||||
|
||||
func renderYAML(w io.Writer, v interface{}) error {
|
||||
enc := yaml.NewEncoder(w)
|
||||
enc.SetIndent(2)
|
||||
|
||||
return enc.Encode(v)
|
||||
}
|
||||
|
||||
func renderJSON(w io.Writer, v interface{}) error {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
return enc.Encode(v)
|
||||
}
|
||||
3
go.mod
3
go.mod
@@ -3,10 +3,11 @@ module github.com/jimeh/evm
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/jimeh/go-render v0.0.1
|
||||
github.com/rs/zerolog v1.26.1
|
||||
github.com/sethvargo/go-envconfig v0.5.0
|
||||
github.com/spf13/cobra v1.4.0
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
21
go.sum
21
go.sum
@@ -1,12 +1,19 @@
|
||||
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/jimeh/go-render v0.0.1 h1:g5ZHbw7JRnF5zZj5s/sqko8OTtQ/hqVnoQSazEVkSbY=
|
||||
github.com/jimeh/go-render v0.0.1/go.mod h1:/kSTeas7AxvjG609H5idpl02SqKf6+J7wM5aZ9tGTEk=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
|
||||
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
|
||||
@@ -17,6 +24,15 @@ github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
|
||||
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
@@ -47,5 +63,6 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
Reference in New Issue
Block a user