Skip to content

Commit

Permalink
Add tags filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
0verbyte committed Sep 12, 2023
1 parent b937c0d commit 28f9ad9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
21 changes: 20 additions & 1 deletion internal/mold/mold.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions internal/mold/mold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"os"
"strings"

"github.com/0verbyte/envmold/internal/mold"
)
Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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())

Expand Down
5 changes: 5 additions & 0 deletions testdata/mold.simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
value: "bar"
type: string
required: false
tags: [production]

- name: bar
value: "bar foo bar"
Expand All @@ -12,6 +13,9 @@
value: true
type: boolean
required: false
tags:
- local
- dev

- name: baseNumber
value: 10
Expand All @@ -22,3 +26,4 @@
value: 1.10
type: number
required: false
tags: ["local", "test"]

0 comments on commit 28f9ad9

Please sign in to comment.