Skip to content

Commit

Permalink
Improve naming, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaen committed Mar 3, 2024
1 parent de04362 commit 6cb779d
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"
)

var serialiser = app.NewSerialiser(tf.NewStyler(tf.NO_COLOUR), false)
var serialiser = app.NewSerialiser(tf.NewStyler(tf.COLOUR_THEME_NO_COLOUR), false)

func main() {
// Setup
Expand Down
8 changes: 4 additions & 4 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ func main() {
klogFolder := func() app.File {
f, err := determineKlogConfigFolder()
if err != nil {
fail(util.PrettifyAppError(err, false), app.GENERAL_ERROR.ToInt())
fail(util.PrettifyAppError(err, false), app.CONFIG_ERROR.ToInt())
}
return f
}()

configFile := func() string {
c, err := readConfigFile(app.Join(klogFolder, app.CONFIG_FILE_NAME))
if err != nil {
fail(util.PrettifyAppError(err, false), app.GENERAL_ERROR.ToInt())
fail(util.PrettifyAppError(err, false), app.CONFIG_ERROR.ToInt())
}
return c
}()

config := func() app.Config {
c, err := app.NewConfig(
app.FromStaticValues{NumCpus: runtime.NumCPU()},
app.FromDeterminedValues{NumCpus: runtime.NumCPU()},
app.FromEnvVars{GetVar: os.Getenv},
app.FromConfigFile{FileContents: configFile},
)
if err != nil {
fail(util.PrettifyAppError(err, false), app.GENERAL_ERROR.ToInt())
fail(util.PrettifyAppError(err, false), app.CONFIG_ERROR.ToInt())
}
return c
}()
Expand Down
12 changes: 6 additions & 6 deletions klog/app/cli/terminalformat/colour_theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package terminalformat
type ColourTheme string

const (
NO_COLOUR = ColourTheme("no_colour")
DARK = ColourTheme("dark")
LIGHT = ColourTheme("light")
COLOUR_THEME_NO_COLOUR = ColourTheme("no_colour")
COLOUR_THEME_DARK = ColourTheme("dark")
COLOUR_THEME_LIGHT = ColourTheme("light")
)

func NewStyler(c ColourTheme) Styler {
Expand All @@ -21,7 +21,7 @@ func NewStyler(c ColourTheme) Styler {
}

switch c {
case NO_COLOUR:
case COLOUR_THEME_NO_COLOUR:
return Styler{
props: StyleProps{},
colourCodes: make(map[Colour]string),
Expand All @@ -32,7 +32,7 @@ func NewStyler(c ColourTheme) Styler {
underlined: "",
bold: "",
}
case DARK:
case COLOUR_THEME_DARK:
baseColouredStyler.colourCodes = map[Colour]string{
TEXT: "015",
TEXT_INVERSE: "000",
Expand All @@ -45,7 +45,7 @@ func NewStyler(c ColourTheme) Styler {
YELLOW: "221",
}
return baseColouredStyler
case LIGHT:
case COLOUR_THEME_LIGHT:
baseColouredStyler.colourCodes = map[Colour]string{
TEXT: "000",
TEXT_INVERSE: "015",
Expand Down
2 changes: 1 addition & 1 deletion klog/app/cli/terminalformat/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func TestPrintTable(t *testing.T) {
result := ""
styler := NewStyler(DARK)
styler := NewStyler(COLOUR_THEME_DARK)
table := NewTable(3, " ")
table.
Cell("FIRST", Options{align: ALIGN_LEFT}).
Expand Down
4 changes: 2 additions & 2 deletions klog/app/cli/testcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

func NewTestingContext() TestingContext {
bc := app.NewEmptyBookmarksCollection()
config := app.NewDefaultConfig(tf.NO_COLOUR)
styler := tf.NewStyler(tf.NO_COLOUR)
config := app.NewDefaultConfig(tf.COLOUR_THEME_NO_COLOUR)
styler := tf.NewStyler(tf.COLOUR_THEME_NO_COLOUR)
return TestingContext{
State: State{
printBuffer: "",
Expand Down
2 changes: 1 addition & 1 deletion klog/app/cli/util/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ type NoStyleArgs struct {
func (args *NoStyleArgs) Apply(ctx *app.Context) {
if args.NoStyle {
(*ctx).ConfigureSerialisation(func(styler tf.Styler, decimalDuration bool) (tf.Styler, bool) {
return tf.NewStyler(tf.NO_COLOUR), decimalDuration
return tf.NewStyler(tf.COLOUR_THEME_NO_COLOUR), decimalDuration
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion klog/app/cli/util/prettifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func PrettifyAppError(err app.Error, isDebug bool) error {
}

// PrettifyParsingError turns a parsing error into a coloured and well-structured form.
func PrettifyParsingError(err app.ParserErrors, isDebug bool, styler tf.Styler) error {
func PrettifyParsingError(err app.ParserErrors, styler tf.Styler) error {
message := ""
INDENT := " "
for _, e := range err.All() {
Expand Down
8 changes: 4 additions & 4 deletions klog/app/cli/util/prettifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
)

var styler = tf.NewStyler(tf.NO_COLOUR)
var styler = tf.NewStyler(tf.COLOUR_THEME_NO_COLOUR)

func TestFormatParserError(t *testing.T) {
block1, _ := txt.ParseBlock("Good text\nSome malformed text", 37)
Expand All @@ -18,7 +18,7 @@ func TestFormatParserError(t *testing.T) {
txt.NewError(block1, 1, 0, 4, "CODE", "Error", "Short explanation."),
txt.NewError(block2, 0, 8, 5, "CODE", "Problem", "More info.").SetOrigin("some-file.klg"),
})
text := PrettifyParsingError(err, false, styler).Error()
text := PrettifyParsingError(err, styler).Error()
assert.Equal(t, `
[SYNTAX ERROR] in line 39
Some malformed text
Expand All @@ -37,7 +37,7 @@ func TestReflowsLongMessages(t *testing.T) {
err := app.NewParserErrors([]txt.Error{
txt.NewError(block, 0, 4, 3, "CODE", "Some Title", "A verbose description with details, potentially spanning multiple lines with a comprehensive text and tremendously helpful information.\nBut\nit\nrespects\nnewlines."),
})
text := PrettifyParsingError(err, false, styler).Error()
text := PrettifyParsingError(err, styler).Error()
assert.Equal(t, `
[SYNTAX ERROR] in line 2
Foo bar
Expand All @@ -56,7 +56,7 @@ func TestConvertsTabToSpaces(t *testing.T) {
err := app.NewParserErrors([]txt.Error{
txt.NewError(block, 0, 0, 8, "CODE", "Error title", "Error details"),
})
text := PrettifyParsingError(err, false, styler).Error()
text := PrettifyParsingError(err, styler).Error()
assert.Equal(t, `
[SYNTAX ERROR] in line 14
Foo bar
Expand Down
33 changes: 18 additions & 15 deletions klog/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ type Reader interface {
Apply(*Config) Error
}

func NewConfig(c1 FromStaticValues, c2 FromEnvVars, c3 FromConfigFile) (Config, Error) {
config := NewDefaultConfig(tf.DARK)
for _, c := range []Reader{c1, c2, c3} {
// NewConfig creates a new application configuration by merging the config
// based on the following precedence: (1) env variables, (2) config file,
// (3) determined values.
func NewConfig(determined FromDeterminedValues, env FromEnvVars, file FromConfigFile) (Config, Error) {
config := NewDefaultConfig(tf.COLOUR_THEME_DARK)
for _, c := range []Reader{determined, file, env} {
err := c.Apply(&config)
if err != nil {
return Config{}, err
Expand Down Expand Up @@ -132,13 +135,13 @@ func (p *OptionalParam[T]) set(value T, o configOrigin) {
p.origin = o
}

// FromStaticValues is the part of the configuration that is automatically
// FromDeterminedValues is the part of the configuration that is automatically
// determined, e.g. by constraints of the runtime environment.
type FromStaticValues struct {
type FromDeterminedValues struct {
NumCpus int
}

func (e FromStaticValues) Apply(config *Config) Error {
func (e FromDeterminedValues) Apply(config *Config) Error {
config.CpuKernels.override(e.NumCpus, configOriginStaticValues)
return nil
}
Expand All @@ -154,7 +157,7 @@ func (e FromEnvVars) Apply(config *Config) Error {
config.IsDebug.override(true, configOriginEnv)
}
if e.GetVar("NO_COLOR") != "" {
config.ColourScheme.override(tf.NO_COLOUR, configOriginEnv)
config.ColourScheme.override(tf.COLOUR_THEME_NO_COLOUR, configOriginEnv)
}
if e.GetVar("EDITOR") != "" {
config.Editor.set(e.GetVar("EDITOR"), configOriginEnv)
Expand Down Expand Up @@ -183,18 +186,18 @@ var CONFIG_FILE_ENTRIES = []ConfigFileEntries[any]{
Help: Help{
Summary: "The CLI command that shall be invoked when running `klog edit`.",
Value: "The config property can be any valid CLI command, as you would type it on the terminal. klog will append the target file path as last input argument to that command. Note: you can use quotes in order to prevent undesired shell word-splitting, e.g. if the command name/path contains spaces.",
Default: "If absent/empty, `klog edit` tries to fall back to the $EDITOR environment variable (which you’d see below in that case).",
Default: "If absent/empty, `klog edit` tries to fall back to the $EDITOR environment variable (which, by the way, takes precedence, if set).",
},
}, {
Name: "colour_scheme",
reader: func(value string, config *Config) error {
switch value {
case string(tf.DARK):
config.ColourScheme.override(tf.DARK, configOriginFile)
case string(tf.NO_COLOUR):
config.ColourScheme.override(tf.NO_COLOUR, configOriginFile)
case string(tf.LIGHT):
config.ColourScheme.override(tf.LIGHT, configOriginFile)
case string(tf.COLOUR_THEME_DARK):
config.ColourScheme.override(tf.COLOUR_THEME_DARK, configOriginFile)
case string(tf.COLOUR_THEME_NO_COLOUR):
config.ColourScheme.override(tf.COLOUR_THEME_NO_COLOUR, configOriginFile)
case string(tf.COLOUR_THEME_LIGHT):
config.ColourScheme.override(tf.COLOUR_THEME_LIGHT, configOriginFile)
default:
return errors.New("The value must be `dark`, `light` or `no_colour`")
}
Expand Down Expand Up @@ -356,7 +359,7 @@ func (e FromConfigFile) Apply(config *Config) Error {
if rErr != nil {
return NewError(
"Invalid config file",
"The value for `"+key+"` is not valid: "+entry.Help.Value,
"The value for the `"+key+"` setting is not valid: "+entry.Help.Value,
rErr,
)
}
Expand Down
Loading

0 comments on commit 6cb779d

Please sign in to comment.