Skip to content

Commit

Permalink
more docs and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Feb 4, 2024
1 parent 40e7b3d commit e419d78
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 231 deletions.
6 changes: 3 additions & 3 deletions cmd/cmd_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ var printCommand = &cli.Command{
Usage: "Print environment variables",
Before: setup,
Action: func(_ context.Context, _ *cli.Command) error {
settings.Interpolate = true
settings.UseInterpolatedValues = true

var handlers []render.Handler

if settings.ShowPretty {
handlers = append(handlers, render.FormatHandler)
if settings.FormatOutput {
handlers = append(handlers, render.FormatterHandler)
}

fmt.Println(render.NewRenderer(*settings, handlers...).Document(env))
Expand Down
21 changes: 10 additions & 11 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ func setup(_ context.Context, cmd *cli.Command) error {
return err
}

settings = &render.Settings{
FilterKeyPrefix: cmd.Root().String("key-prefix"),
FilterGroup: cmd.Root().String("group"),
IncludeDisabled: cmd.Root().Bool("include-commented"),

ShowPretty: cmd.Root().Bool("pretty"),
ShowBlankLines: cmd.Root().Bool("with-blank-lines"),
ShowComments: cmd.Root().Bool("with-comments"),
ShowGroupBanners: cmd.Root().Bool("with-groups"),
ShowColors: cmd.Root().Bool("colors"),
}
settings = render.NewSettings(
render.WithBlankLines(cmd.Root().Bool("with-blank-lines")),
render.WithComments(cmd.Root().Bool("with-comments")),
render.WithGroupBanners(cmd.Root().Bool("with-groups")),
render.WithFilterGroup(cmd.Root().String("group")),
render.WithFilterKeyPrefix(cmd.Root().String("key-prefix")),
render.WithIncludeDisabled(cmd.Root().Bool("include-commented")),
render.WithFormattedOutput(cmd.Root().Bool("pretty")),
render.WithColors(cmd.Root().Bool("colors")),
)

return nil
}
18 changes: 9 additions & 9 deletions pkg/render/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import (
type Handler func(in *HandlerInput) HandlerSignal

type HandlerInput struct {
Presenter *Renderer
Previous ast.Statement
Settings Settings
Statement any
Value string
Presenter *Renderer
PreviousStatement ast.Statement
Settings Settings
CurrentStatement any
ReturnValue string
}

func (si *HandlerInput) Stop() HandlerSignal {
func (hi *HandlerInput) Stop() HandlerSignal {
return Stop
}

func (si *HandlerInput) Return(val string) HandlerSignal {
si.Value = val
func (hi *HandlerInput) Return(value string) HandlerSignal {
hi.ReturnValue = value

return Return
}

func (si *HandlerInput) Continue() HandlerSignal {
func (hi *HandlerInput) Continue() HandlerSignal {
return Continue
}
68 changes: 34 additions & 34 deletions pkg/render/handler_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,83 @@ import (
"github.com/jippi/dottie/pkg/ast"
)

// FilterKeyPrefix will filter out Statements that do not have the
// FilterByKeyPrefix will filter out Statements that do not have the
// configured (optional) key prefix
func FilterKeyPrefix(in *HandlerInput) HandlerSignal {
func FilterByKeyPrefix(hi *HandlerInput) HandlerSignal {
// Short circuit the filter if there is no KeyPrefix to filter on
if len(in.Settings.FilterKeyPrefix) == 0 {
return in.Continue()
if len(hi.Settings.FilterKeyPrefix) == 0 {
return hi.Continue()
}

switch val := in.Statement.(type) {
switch statement := hi.CurrentStatement.(type) {
case *ast.Assignment:
if !strings.HasPrefix(val.Name, in.Settings.FilterKeyPrefix) {
return in.Stop()
if !strings.HasPrefix(statement.Name, hi.Settings.FilterKeyPrefix) {
return hi.Stop()
}
}

return in.Continue()
return hi.Continue()
}

// FilterComments will filter out Comment statements if they aren't to be included
func FilterComments(in *HandlerInput) HandlerSignal {
func FilterComments(hi *HandlerInput) HandlerSignal {
// Short circuit the filter if we allow comments
if in.Settings.WithComments() {
return in.Continue()
if hi.Settings.ShowComments {
return hi.Continue()
}

switch in.Statement.(type) {
switch hi.CurrentStatement.(type) {
case *ast.Comment:
if !in.Settings.WithComments() {
return in.Stop()
if !hi.Settings.ShowComments {
return hi.Stop()
}
}

return in.Continue()
return hi.Continue()
}

// FilterDisabledStatements will filter out Assignment Statements that are
// disabled
func FilterDisabledStatements(in *HandlerInput) HandlerSignal {
func FilterDisabledStatements(hi *HandlerInput) HandlerSignal {
// Short circuit the filter if we allow disabled statements
if in.Settings.IncludeDisabled {
return in.Continue()
if hi.Settings.IncludeDisabled {
return hi.Continue()
}

switch val := in.Statement.(type) {
switch statement := hi.CurrentStatement.(type) {
case *ast.Assignment:
if !val.Active && !in.Settings.IncludeDisabled {
return in.Stop()
if !statement.Active && !hi.Settings.IncludeDisabled {
return hi.Stop()
}
}

return in.Continue()
return hi.Continue()
}

// FilterGroupName will filter out Statements that do not
// FilterByGroupName will filter out Statements that do not
// belong to the required Group name
func FilterGroupName(in *HandlerInput) HandlerSignal {
func FilterByGroupName(hi *HandlerInput) HandlerSignal {
// Short circuit the filter if there is no Group name to filter on
if len(in.Settings.FilterGroup) == 0 {
return in.Continue()
if len(hi.Settings.FilterGroup) == 0 {
return hi.Continue()
}

switch val := in.Statement.(type) {
switch statement := hi.CurrentStatement.(type) {
case *ast.Assignment:
if !val.BelongsToGroup(in.Settings.FilterGroup) {
return in.Stop()
if !statement.BelongsToGroup(hi.Settings.FilterGroup) {
return hi.Stop()
}

case *ast.Group:
if !val.BelongsToGroup(in.Settings.FilterGroup) {
return in.Stop()
if !statement.BelongsToGroup(hi.Settings.FilterGroup) {
return hi.Stop()
}

case *ast.Comment:
if !val.BelongsToGroup(in.Settings.FilterGroup) {
return in.Stop()
if !statement.BelongsToGroup(hi.Settings.FilterGroup) {
return hi.Stop()
}
}

return in.Continue()
return hi.Continue()
}
83 changes: 37 additions & 46 deletions pkg/render/handler_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,71 @@ import (

func NewFormatter() *Renderer {
settings := Settings{
IncludeDisabled: true,
Interpolate: false,
ShowBlankLines: true,
ShowColors: false,
ShowComments: true,
ShowGroupBanners: true,
IncludeDisabled: true,
UseInterpolatedValues: false,
ShowBlankLines: true,
ShowColors: false,
ShowComments: true,
ShowGroupBanners: true,
}

return NewRenderer(settings, FormatHandler)
return NewRenderer(settings, FormatterHandler)
}

func FormatHandler(in *HandlerInput) HandlerSignal {
switch val := in.Statement.(type) {
// Ignore all existing newlines when doing formatting
// we will be injecting these ourself in other places
// FormatterHandler is responsible for formatting an .env file according
// to our opinionated style.
func FormatterHandler(hi *HandlerInput) HandlerSignal {
switch statement := hi.CurrentStatement.(type) {
case *ast.Newline:
return in.Stop()
// Ignore all existing newlines when doing formatting as
// we will be injecting these ourself in other places.
return hi.Stop()

case *ast.Group:
output := in.Presenter.Group(val)
output := hi.Presenter.Group(statement)
if len(output) == 0 {
return in.Stop()
return hi.Stop()
}

res := NewLineBuffer()
buf := NewLineBuffer()

// If the previous line is a newline, don't add another one.
// If the previous line is a Newline, don't add another one.
// This could happen if a group is the *first* thing in the document
if !(&ast.Newline{}).Is(in.Previous) && in.Previous != nil {
res.AddNewline()
if hi.PreviousStatement != nil && !hi.PreviousStatement.Is(&ast.Newline{}) {
buf.AddNewline()
}

return in.Return(
res.
Add(output).
AddNewline().
Get(),
)
return hi.Return(buf.Add(output).AddNewline().Get())

case *ast.Assignment:
output := in.Presenter.Assignment(val)
output := hi.Presenter.Assignment(statement)
if len(output) == 0 {
return in.Stop()
return hi.Stop()
}

buff := NewLineBuffer()
buf := NewLineBuffer()

// If the assignment belongs to a group, but there are no previous
// then we're the first, so add a newline padding
if val.Group != nil && in.Previous == nil {
buff.AddNewline()
// If the previous Statement was also an Assignment, detect if they should
// be allowed to cuddle (without newline between them) or not.
//
// Statements are only allow cuddle if both have no comments
if statement.Is(hi.PreviousStatement) && (statement.HasComments() || assignmentHasComments(hi.PreviousStatement)) {
buf.AddNewline()
}

// Looks like current and previous Statement is both "Assignment"
// which mean they might be too close in the document, so we will
// attempt to inject some new-lines to give them some space
if val.Is(in.Previous) {
// only allow cuddling of assignments if they both have no comments
if val.HasComments() || assignmentHasComments(in.Previous) {
buff.AddNewline()
}
}

return in.Return(buff.Add(output).Get())
return hi.Return(buf.Add(output).Get())
}

return in.Continue()
return hi.Continue()
}

func assignmentHasComments(stmt ast.Statement) bool {
x, ok := stmt.(*ast.Assignment)
// assignmentHasComments checks if the Statement is an Assignment
// and if it has any comments attached to it
func assignmentHasComments(statement ast.Statement) bool {
assignment, ok := statement.(*ast.Assignment)
if !ok {
return false
}

return x.HasComments()
return assignment.HasComments()
}
2 changes: 1 addition & 1 deletion pkg/render/handler_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var signals = []string{
Return: "RETURN",
}

// String returns the string corresponding to the token.
// String returns the string corresponding to the Handler Signal.
func (hs HandlerSignal) String() string {
s := ""

Expand Down
10 changes: 10 additions & 0 deletions pkg/render/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package render

import "github.com/jippi/dottie/pkg/ast"

type Output interface {
GroupBanner(*ast.Group, Settings) string
Assignment(*ast.Assignment, Settings) string
Comment(*ast.Comment, Settings) string
Newline(*ast.Newline, Settings) string
}
Loading

0 comments on commit e419d78

Please sign in to comment.