Skip to content

Commit

Permalink
refactor: replace fatih/color with muesli/termenv (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
brumhard authored Oct 28, 2022
1 parent ebb9ea6 commit 32514c2
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 60 deletions.
35 changes: 20 additions & 15 deletions cmd/gt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,35 @@ import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/muesli/termenv"
"github.com/schwarzit/go-template/pkg/colors"
"github.com/schwarzit/go-template/pkg/gotemplate"
"github.com/spf13/cobra"
)

var (
//nolint:gochecknoglobals // only a colored string, cannot be put into a const
goTemplateHighlighted = color.CyanString("go/template")
)
const goTemplate = "go/template"

func main() {
cmd := buildRootCommand()
output := termenv.NewOutput(os.Stdout, termenv.WithProfile(termenv.EnvColorProfile()))

cmd := buildRootCommand(output)
if err := cmd.Execute(); err != nil {
printError(err)
printError(output, err)
os.Exit(1)
}
}

func printError(err error) {
headerHighlight := color.New(color.FgRed, color.Bold).SprintFunc()
highlight := color.New(color.FgRed).SprintFunc()
func printError(output *termenv.Output, err error) {
redStyler := output.String().Foreground(output.Color(colors.Red))

_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", headerHighlight("ERROR"), highlight(err.Error()))
_, _ = fmt.Fprintf(
os.Stderr, "%s: %s\n",
redStyler.Bold().Styled("ERROR"),
redStyler.Styled(err.Error()),
)
}

func buildRootCommand() *cobra.Command {
func buildRootCommand(output *termenv.Output) *cobra.Command {
gt := gotemplate.New()

cmd := &cobra.Command{
Expand All @@ -44,7 +47,9 @@ To begin working with %[1]s, run the 'gt new' command:
This will prompt you to create a new Golang project using standard configs.
For more information, please visit the project's Github page: github.com/schwarzit/go-template.`, goTemplateHighlighted),
For more information, please visit the project's Github page: github.com/schwarzit/go-template.`,
output.String(goTemplate).Foreground(output.Color(colors.Cyan)),
),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Enable swapping out stdout/stderr for testing
gt.Out = cmd.OutOrStdout()
Expand All @@ -58,8 +63,8 @@ For more information, please visit the project's Github page: github.com/schwarz
SilenceUsage: true,
}

cmd.AddCommand(buildNewCommand(gt))
cmd.AddCommand(buildVersionCommand(gt))
cmd.AddCommand(buildNewCommand(output, gt))
cmd.AddCommand(buildVersionCommand(output, gt))

return cmd
}
6 changes: 3 additions & 3 deletions cmd/gt/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package main
import (
"fmt"

"github.com/fatih/color"
"github.com/muesli/termenv"
"github.com/schwarzit/go-template/pkg/gotemplate"
"github.com/spf13/cobra"
)

func buildNewCommand(gt *gotemplate.GT) *cobra.Command {
func buildNewCommand(output *termenv.Output, gt *gotemplate.GT) *cobra.Command {
var (
configFile string
opts gotemplate.NewRepositoryOptions
)

underline := color.New(color.Underline).SprintFunc()
underline := output.String().Underline().Styled

cmd := &cobra.Command{
Use: "new",
Expand Down
5 changes: 4 additions & 1 deletion cmd/gt/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"fmt"

"github.com/muesli/termenv"
"github.com/schwarzit/go-template/pkg/colors"
"github.com/schwarzit/go-template/pkg/gotemplate"
"github.com/spf13/cobra"
)

func buildVersionCommand(gt *gotemplate.GT) *cobra.Command {
func buildVersionCommand(output *termenv.Output, gt *gotemplate.GT) *cobra.Command {
goTemplateHighlighted := output.String(goTemplate).Foreground(output.Color(colors.Cyan))
cmd := &cobra.Command{
Use: "version",
Short: fmt.Sprintf("Print the version number of %s", goTemplateHighlighted),
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.19
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/Masterminds/sprig/v3 v3.2.2
github.com/fatih/color v1.13.0
github.com/google/go-github/v39 v39.2.0
github.com/muesli/termenv v0.13.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
Expand All @@ -15,20 +15,23 @@ require (

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8=
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/cpuguy83/go-md2man/v2 v2.0.2/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/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -30,22 +30,26 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
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/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down Expand Up @@ -76,14 +80,10 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
7 changes: 7 additions & 0 deletions pkg/colors/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package colors

const (
Cyan = "#00FFFF"
Yellow = "#FFD500"
Red = "#FF4C4C"
)
22 changes: 22 additions & 0 deletions pkg/gotemplate/gt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"context"
"io"
"net/http"
"sync"
"text/template"
"time"

"github.com/Masterminds/sprig/v3"
"github.com/google/go-github/v39/github"
"github.com/muesli/termenv"
"github.com/schwarzit/go-template/pkg/repos"
)

Expand All @@ -18,6 +20,26 @@ type GT struct {
Options *Options
FuncMap template.FuncMap
GithubTagLister repos.GithubTagLister
once sync.Once
output *termenv.Output
}

func (gt *GT) styler() *termenv.Output {
if gt.output != nil {
return gt.output
}

if gt.Out == nil {
// panic here since it's a package user error
// that it is not set
panic("gt out stream not set")
}

gt.once.Do(func() {
gt.output = termenv.NewOutput(gt.Out, termenv.WithProfile(termenv.EnvColorProfile()))
})

return gt.output
}

type Streams struct {
Expand Down
18 changes: 9 additions & 9 deletions pkg/gotemplate/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestGT_LoadConfigValuesFromFile(t *testing.T) {
},
}

optionValues, err := loadValueFromTestFile(t, gt, fmt.Sprintf(`---
optionValues, err := loadValueFromTestFile(t, &gt, fmt.Sprintf(`---
base:
%s: %s
extensions:
Expand All @@ -101,7 +101,7 @@ extensions:
})

t.Run("validates that base parameters are not empty", func(t *testing.T) {
_, err := loadValueFromTestFile(t, gt, fmt.Sprintf(`---
_, err := loadValueFromTestFile(t, &gt, fmt.Sprintf(`---
base:
%s: ""`, optionName))

Expand All @@ -119,7 +119,7 @@ base:
)),
)

_, err := loadValueFromTestFile(t, gt, fmt.Sprintf(`---
_, err := loadValueFromTestFile(t, &gt, fmt.Sprintf(`---
base:
%s: "NOT_A_VALID_VALUE"`, optionName))

Expand All @@ -142,7 +142,7 @@ base:
},
}

optionValues, err := loadValueFromTestFile(t, gt, "")
optionValues, err := loadValueFromTestFile(t, &gt, "")
require.NoError(t, err)
require.Equal(t, &gotemplate.OptionValues{
Extensions: map[string]gotemplate.OptionNameToValue{
Expand Down Expand Up @@ -174,7 +174,7 @@ base:
},
}

optionValues, err := loadValueFromTestFile(t, gt, `---
optionValues, err := loadValueFromTestFile(t, &gt, `---
base:
int: 2
string: "test"
Expand All @@ -198,7 +198,7 @@ base:
gotemplate.StaticValue(false),
)

_, err := loadValueFromTestFile(t, gt, fmt.Sprintf(`---
_, err := loadValueFromTestFile(t, &gt, fmt.Sprintf(`---
base:
%s: "not a bool"`, optionName))

Expand All @@ -223,7 +223,7 @@ base:
},
}

_, err := loadValueFromTestFile(t, gt, `---
_, err := loadValueFromTestFile(t, &gt, `---
extensions:
test:
option: true`)
Expand All @@ -248,15 +248,15 @@ extensions:
},
}

_, err := loadValueFromTestFile(t, gt, `---
_, err := loadValueFromTestFile(t, &gt, `---
extensions:
test:
option: true`)
require.NoError(t, err)
})
}

func loadValueFromTestFile(t *testing.T, gt gotemplate.GT, contents string) (*gotemplate.OptionValues, error) {
func loadValueFromTestFile(t *testing.T, gt *gotemplate.GT, contents string) (*gotemplate.OptionValues, error) {
dir := t.TempDir()
testFile := path.Join(dir, "test.yml")
err := os.WriteFile(testFile, []byte(contents), os.ModePerm)
Expand Down
Loading

0 comments on commit 32514c2

Please sign in to comment.