Skip to content

Commit

Permalink
Merge pull request #27 from g4s8/i26
Browse files Browse the repository at this point in the history
fix: un-escape glob patterns
  • Loading branch information
g4s8 authored Aug 19, 2024
2 parents 14b5c52 + 5dc201f commit 69067de
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
2 changes: 1 addition & 1 deletion _examples/all_files.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//go:generate go run ../ -output all.md -dir . -files * -types *
//go:generate go run ../ -debug -output all.md -dir . -files * -types='*'
package main
11 changes: 11 additions & 0 deletions _examples/complex-fields.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
<article>
<h1>Environment Variables</h1>

<h2>FieldNames</h2>
<p>FieldNames uses field names as env names.</p>
<ul>
<li><code>FOO</code> - Foo is a single field.</li>
<li><code>BAR</code> - Bar and Baz are two fields.</li>
<li><code>BAZ</code> - Bar and Baz are two fields.</li>
<li><code>QUUX</code> - Quux is a field with a tag.</li>
<li><code>FOO_BAR</code> (default: <code>quuux</code>) - FooBar is a field with a default value.</li>
<li>Required is a required field.</li>
</ul>

</article>
</section>
</body>
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func (c *Config) parseEnv() error {
return nil
}

func (c *Config) normalize() {
c.TypeGlob = unescapeGlob(c.TypeGlob)
c.FileGlob = unescapeGlob(c.FileGlob)
}

func (c *Config) setDefaults() {
if c.FileGlob == "" {
c.FileGlob = c.ExecFile
Expand Down Expand Up @@ -172,5 +177,6 @@ func (c *Config) Load() error {
return fmt.Errorf("parse env: %w", err)
}
c.setDefaults()
c.normalize()
return nil
}
64 changes: 64 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"flag"
"os"
"testing"
)

func TestConfig(t *testing.T) {
t.Run("parse flags", func(t *testing.T) {
var c Config
fs := flag.NewFlagSet("test", flag.ContinueOnError)
os.Args = []string{
"test",
"-types", "foo,bar",
"-files", "*",
"-output", "out.txt",
"-format", "plaintext",
"-env-prefix", "FOO",
"-no-styles",
"-field-names",
"-debug",
}
if err := c.parseFlags(fs); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if c.TypeGlob != "foo,bar" {
t.Errorf("unexpected TypeGlob: %q", c.TypeGlob)
}
if c.FileGlob != "*" {
t.Errorf("unexpected FileGlob: %q", c.FileGlob)
}
if c.OutFile != "out.txt" {
t.Errorf("unexpected OutFile: %q", c.OutFile)
}
if c.OutFormat != "plaintext" {
t.Errorf("unexpected OutFormat: %q", c.OutFormat)
}
if c.EnvPrefix != "FOO" {
t.Errorf("unexpected EnvPrefix: %q", c.EnvPrefix)
}
if !c.NoStyles {
t.Error("unexpected NoStyles: false")
}
if !c.FieldNames {
t.Error("unexpected FieldNames: false")
}
if !c.Debug {
t.Error("unexpected Debug: false")
}
})
t.Run("normalize", func(t *testing.T) {
var c Config
c.TypeGlob = `"foo"`
c.FileGlob = `"*"`
c.normalize()
if c.TypeGlob != "foo" {
t.Errorf("unexpected TypeGlob: %q", c.TypeGlob)
}
if c.FileGlob != "*" {
t.Errorf("unexpected FileGlob: %q", c.FileGlob)
}
})
}
9 changes: 9 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ func camelToSnake(s string) string {

return result.String()
}

// un-escape -types and -files globs: '*' -> *, "foo" -> foo
// if first and last characters are quotes, remove them.
func unescapeGlob(s string) string {
if len(s) >= 2 && ((s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'')) {
return s[1 : len(s)-1]
}
return s
}
22 changes: 22 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,25 @@ func TestCamelToSnake(t *testing.T) {
}
}
}

func TestUnescapeGlob(t *testing.T) {
tests := map[string]string{
`"foo"`: `foo`,
`"foo`: `"foo`,
`foo"`: `foo"`,
`foo`: `foo`,
`'foo'`: `foo`,
`'foo`: `'foo`,
`foo'`: `foo'`,
`*`: `*`,
`*foo*`: `*foo*`,
`'*'`: `*`,
``: ``,
}

for input, expected := range tests {
if got := unescapeGlob(input); got != expected {
t.Errorf("unexpected result for %q: got %q, want %q", input, got, expected)
}
}
}

0 comments on commit 69067de

Please sign in to comment.