Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorize config output #77

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/spf13/cobra"
)

var output string
var (
output string
noColor bool
)

// configCmd is the entry point for printing the applications configuration in the terminal
var configCmd = &cobra.Command{
Expand All @@ -20,9 +23,9 @@ var configCmd = &cobra.Command{
RunE: func(command *cobra.Command, args []string) error {
switch output {
case "json":
return configuration.Config.PrintJSON(os.Stdout)
return configuration.Config.PrintJSON(noColor, os.Stdout)
case "yaml":
return configuration.Config.PrintYAML(os.Stdout)
return configuration.Config.PrintYAML(noColor, os.Stdout)
default:
return errors.New("invalid output format. Valid values are 'json' and 'yaml'")
}
Expand All @@ -31,4 +34,5 @@ var configCmd = &cobra.Command{

func init() {
configCmd.Flags().StringVarP(&output, "output", "o", "yaml", "The output format. Valid values are 'json' and 'yaml'. Defaults to 'yaml'.")
configCmd.Flags().BoolVarP(&noColor, "no-color", "n", false, "Disable color output")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/alecthomas/chroma v0.10.0
github.com/briandowns/spinner v1.18.1
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
Expand All @@ -21,7 +22,6 @@ require (
)

require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.1 // indirect
Expand Down
65 changes: 58 additions & 7 deletions internal/pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"os"
"path/filepath"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
Expand All @@ -28,28 +32,73 @@ type configuration struct {
CheckForUpdates bool `mapstructure:"check_for_updates" yaml:"check_for_updates" json:"checkForUpdates"`
}

func write(data []byte, writer io.Writer) error {
_, err := fmt.Fprint(writer, string(data))
return err
type writeOptions struct {
data string
lexerName string
noColor bool
writer io.Writer
}

func (c *configuration) PrintJSON(writer io.Writer) error {
func prettyWrite(opts writeOptions) error {
lexer := lexers.Get(opts.lexerName)
if lexer == nil {
lexer = lexers.Fallback
}

lexer = chroma.Coalesce(lexer)

style := styles.Get("native")
if style == nil {
style = styles.Fallback
}

formatter := formatters.Get("terminal16m")

if opts.noColor {
formatter = formatters.Get("noop")
}

iterator, err := lexer.Tokenise(nil, opts.data)
if err != nil {
return err
}

return formatter.Format(opts.writer, style, iterator)
}

func (c *configuration) PrintJSON(noColor bool, writer io.Writer) error {
b, err := json.MarshalIndent(c, "", " ")
b = append(b, '\n')
if err != nil {
return err
}

return write(b, writer)
opts := writeOptions{
data: string(b),
lexerName: "json",
noColor: noColor,
writer: writer,
}

return prettyWrite(opts)
}

func (c *configuration) PrintYAML(writer io.Writer) error {
func (c *configuration) PrintYAML(noColor bool, writer io.Writer) error {
b, err := yaml.Marshal(c)
y := []byte("---\n")
y = append(y, b...)
if err != nil {
return err
}

return write(b, writer)
opts := writeOptions{
data: string(y),
lexerName: "json",
noColor: noColor,
writer: writer,
}

return prettyWrite(opts)
}

func InitConfig() error {
Expand Down Expand Up @@ -103,4 +152,6 @@ func setDefaults() {
viper.SetDefault("show_unreleased", true)

viper.SetDefault("check_for_updates", true)

viper.SetDefault("no_color", false)
}
7 changes: 4 additions & 3 deletions internal/pkg/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestPrintJSON(t *testing.T) {
config := configuration.Config

var buf bytes.Buffer
err = config.PrintJSON(&buf)
err = config.PrintJSON(true, &buf)
assert.NoError(t, err)

cfg := `{
Expand Down Expand Up @@ -73,10 +73,11 @@ func TestPrintYAML(t *testing.T) {
config := configuration.Config

var buf bytes.Buffer
err = config.PrintYAML(&buf)
err = config.PrintYAML(true, &buf)
assert.NoError(t, err)

cfg := `file_name: CHANGELOG.md
cfg := `---
file_name: CHANGELOG.md
excluded_labels:
- maintenance
sections:
Expand Down