From 28f9ad9e4312f5d7dc53f6c5fbfe9d88deb9765c Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Tue, 12 Sep 2023 09:29:20 -0700 Subject: [PATCH] Add tags filtering logic --- internal/mold/mold.go | 21 ++++++++++++++++++++- internal/mold/mold_test.go | 12 ++++++------ main.go | 13 ++++++++++++- testdata/mold.simple.yaml | 5 +++++ 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/internal/mold/mold.go b/internal/mold/mold.go index 574c7bb..5002166 100644 --- a/internal/mold/mold.go +++ b/internal/mold/mold.go @@ -51,6 +51,10 @@ func (m *MoldTemplateVariable) HasTag(tag string) bool { return false } +func (m *MoldTemplateVariable) HasTags() bool { + return len(m.Tags) > 0 +} + // MoldTemplate data representation for the MoldTemplate. type MoldTemplate struct { variables []MoldTemplateVariable @@ -59,7 +63,7 @@ type MoldTemplate struct { } // New creates a new MoldTemplate from an io.Reader. Use the helper functions to read from the respective input. -func New(r io.Reader) (*MoldTemplate, error) { +func New(r io.Reader, tags *[]string) (*MoldTemplate, error) { b, err := io.ReadAll(r) if err != nil { return nil, err @@ -76,6 +80,21 @@ func New(r io.Reader) (*MoldTemplate, error) { return nil, ErrMissingVariableName } + var skipEnvVar bool + if tags != nil && moldTemplateVariable.HasTags() { + skipEnvVar = true + for _, tag := range *tags { + if moldTemplateVariable.HasTag(tag) { + skipEnvVar = false + break + } + } + } + + if skipEnvVar { + continue + } + // Check for type constraint on the value field switch moldTemplateVariable.Value.(type) { case int, float32, float64: diff --git a/internal/mold/mold_test.go b/internal/mold/mold_test.go index 51f7e7c..99030fb 100644 --- a/internal/mold/mold_test.go +++ b/internal/mold/mold_test.go @@ -18,7 +18,7 @@ func TestMoldNew(t *testing.T) { required: false ` - if _, err := New(strings.NewReader(moldTemplate)); err != nil { + if _, err := New(strings.NewReader(moldTemplate), nil); err != nil { t.Errorf("Failed to create new mold: %v", err) return } @@ -37,7 +37,7 @@ func TestCheckKeysInMold(t *testing.T) { required: false ` - mold, err := New(strings.NewReader(moldTemplate)) + mold, err := New(strings.NewReader(moldTemplate), nil) if err != nil { t.Errorf("Failed to create new mold: %v", err) return @@ -75,7 +75,7 @@ func TestVerifyTypeConstraint(t *testing.T) { required: false ` - if _, err := New(strings.NewReader(moldTemplate)); err != ErrInvalidDataType { + if _, err := New(strings.NewReader(moldTemplate), nil); err != ErrInvalidDataType { t.Errorf("Failed to create new mold: %v", err) return } @@ -94,7 +94,7 @@ func TestMoldPromptReader(t *testing.T) { required: false ` - m, err := New(strings.NewReader(moldTemplate)) + m, err := New(strings.NewReader(moldTemplate), nil) if err != nil { t.Errorf("Failed to create new mold: %v", err) return @@ -145,7 +145,7 @@ func TestHasTag(t *testing.T) { required: false ` - m, err := New(strings.NewReader(moldTemplate)) + m, err := New(strings.NewReader(moldTemplate), nil) if err != nil { t.Errorf("Failed to create new mold: %v", err) return @@ -187,7 +187,7 @@ func TestTagsAll(t *testing.T) { required: false ` - m, err := New(strings.NewReader(moldTemplate)) + m, err := New(strings.NewReader(moldTemplate), nil) if err != nil { t.Errorf("Failed to create new mold: %v", err) return diff --git a/main.go b/main.go index e89e8f2..2e1446f 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "io" "log" "os" + "strings" "github.com/0verbyte/envmold/internal/mold" ) @@ -25,12 +26,14 @@ var ( moldTemplate string outputWriter string debug bool + tags string ) func init() { flag.StringVar(&moldTemplate, "template", "mold.yaml", "Path to the mold environment template file") flag.StringVar(&outputWriter, "output", "stdout", "Where environment variables will be written. File path or stdout") flag.BoolVar(&debug, "debug", false, "Enables debug logging") + flag.StringVar(&tags, "tags", "", "Filter environment variables matching tags") flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %s (%s):\n", appName, appVersionString()) flag.PrintDefaults() @@ -47,7 +50,7 @@ func appVersionString() string { } func createMold(r io.Reader) (*mold.MoldTemplate, error) { - m, err := mold.New(r) + m, err := mold.New(r, getTags()) if err != nil { return nil, err } @@ -66,6 +69,14 @@ func getMoldEnvironmentWriter(writerType string) mold.Writer { } } +func getTags() *[]string { + if tags == "" { + return nil + } + s := strings.Split(tags, ",") + return &s +} + func main() { log.Printf("Running %s (%s)\n", appName, appVersionString()) diff --git a/testdata/mold.simple.yaml b/testdata/mold.simple.yaml index 5140eb1..ad7d408 100644 --- a/testdata/mold.simple.yaml +++ b/testdata/mold.simple.yaml @@ -2,6 +2,7 @@ value: "bar" type: string required: false + tags: [production] - name: bar value: "bar foo bar" @@ -12,6 +13,9 @@ value: true type: boolean required: false + tags: + - local + - dev - name: baseNumber value: 10 @@ -22,3 +26,4 @@ value: 1.10 type: number required: false + tags: ["local", "test"]