Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.8
v2.0.9
10 changes: 10 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package figtree

func (tree *figTree) WithAlias(name, alias string) {
tree.mu.Lock()
defer tree.mu.Unlock()
if _, exists := tree.aliases[alias]; exists {
return
}
tree.aliases[alias] = name
}
91 changes: 91 additions & 0 deletions alias_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package figtree

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestWithAlias(t *testing.T) {
const cmdLong, cmdAliasLong, valueLong, usage = "long", "l", "default", "usage"
const cmdShort, cmdAliasShort, valueShort = "short", "s", "default"

t.Run("basic_usage", func(t *testing.T) {
figs := With(Options{Germinate: true, Tracking: false})
figs.NewString(cmdLong, valueLong, usage)
figs.WithAlias(cmdLong, cmdAliasLong)
assert.NoError(t, figs.Parse())
t.Log(figs.Usage())

assert.Equal(t, valueLong, *figs.String(cmdLong))
assert.Equal(t, valueLong, *figs.String(cmdAliasLong))
figs = nil
})

t.Run("multiple_aliases", func(t *testing.T) {
const k, v, u = "name", "yeshua", "the real name of god"
ka1 := "father"
ka2 := "son"
ka3 := "rauch-hokadesch"
figs := With(Options{Germinate: true, Tracking: false})
figs.NewString(k, v, u)
figs.WithAlias(k, ka1)
figs.WithAlias(k, ka2)
figs.WithAlias(k, ka3)
assert.NoError(t, figs.Parse())
t.Log(figs.Usage())

assert.Equal(t, v, *figs.String(k))
assert.Equal(t, v, *figs.String(ka1))
assert.Equal(t, v, *figs.String(ka2))
assert.Equal(t, v, *figs.String(ka3))
figs = nil
})

t.Run("complex_usage", func(t *testing.T) {

figs := With(Options{Germinate: true, Tracking: false})
// long
figs.NewString(cmdLong, valueLong, usage)
figs.WithAlias(cmdLong, cmdAliasLong)
figs.WithValidator(cmdLong, AssureStringNotEmpty)

// short
figs.NewString(cmdShort, valueShort, usage)
figs.WithAlias(cmdShort, cmdAliasShort)
figs.WithValidator(cmdShort, AssureStringNotEmpty)

assert.NoError(t, figs.Parse())
t.Log(figs.Usage())
// long
assert.Equal(t, valueLong, *figs.String(cmdLong))
assert.Equal(t, valueLong, *figs.String(cmdAliasLong))
// short
assert.Equal(t, valueShort, *figs.String(cmdShort))
assert.Equal(t, valueShort, *figs.String(cmdAliasShort))

figs = nil

})

t.Run("alias_with_int", func(t *testing.T) {
figs := With(Options{Germinate: true})
figs.NewInt("count", 42, "usage")
figs.WithAlias("count", "c")
assert.NoError(t, figs.Parse())
t.Log(figs.Usage())
assert.Equal(t, 42, *figs.Int("count"))
assert.Equal(t, 42, *figs.Int("c"))
})

t.Run("alias_conflict", func(t *testing.T) {
figs := With(Options{Germinate: true})
figs.NewString("one", "value1", "usage")
figs.NewString("two", "value2", "usage")
figs.WithAlias("one", "x")
figs.WithAlias("two", "x") // Should this overwrite or be ignored?
assert.NoError(t, figs.Parse())
assert.Equal(t, "value1", *figs.String("x")) // Clarify expected behavior
t.Log(figs.Usage())
})
}
3 changes: 1 addition & 2 deletions figs.go → figtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ func With(opts Options) Plant {
harvest: opts.Harvest,
angel: &angel,
problems: make([]error, 0),
aliases: make(map[string]string),
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
sources: make(map[string]SourceConfig),
sourceLocker: sync.RWMutex{},
mu: sync.RWMutex{},
mutationsCh: make(chan Mutation),
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
Expand Down
37 changes: 10 additions & 27 deletions figs_test.go → figtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,16 @@ func TestGrow(t *testing.T) {
}

func TestVersion(t *testing.T) {
assert.Empty(t, currentVersion, "currentVersion should return an empty string")
assert.NotEmpty(t, Version(), "Version() should not return an empty string")
assert.Equal(t, currentVersion, Version(), "Version() should return the current version")
t.Run("current_version_default_empty", func(t *testing.T) {
if len(currentVersion) > 0 {
currentVersion = ""
}
assert.Empty(t, currentVersion, "currentVersion should return an empty string")
})
t.Run("current_version", func(t *testing.T) {
assert.NotEmpty(t, Version(), "Version() should not return an empty string")
assert.Equal(t, currentVersion, Version(), "Version() should return the current version")
})
}

func TestIsTracking(t *testing.T) {
Expand Down Expand Up @@ -262,30 +269,6 @@ func TestIsTracking(t *testing.T) {
})
}

/*
func TestTree_PollinateInt(t *testing.T) {

}
func TestTree_PollinateInt64(t *testing.T) {

}
func TestTree_PollinateFloat64(t *testing.T) {

}
func TestTree_PollinateDuration(t *testing.T) {

}
func TestTree_PollinateUnitDuration(t *testing.T) {

}
func TestTree_PollinateList(t *testing.T) {

}
func TestTree_PollinateMap(t *testing.T) {

}
*/

func TestTree_PollinateString(t *testing.T) {
figs := With(Options{Pollinate: true, Tracking: true, Germinate: true})
figs.NewString("test", "initial", "usage")
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module github.com/andreimerlescu/figtree/v2
go 1.23.4

require (
github.com/andreimerlescu/checkfs v1.0.3
github.com/andreimerlescu/checkfs v1.0.4
github.com/go-ini/ini v1.67.0
github.com/stretchr/testify v1.10.0
golang.org/x/term v0.31.0
golang.org/x/term v0.32.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/sys v0.33.0 // indirect
)
18 changes: 6 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/andreimerlescu/checkfs v1.0.1 h1:4w4tPgI20NEkVQbhES8GwgJDBuLpPdbdzYgI6blzI/k=
github.com/andreimerlescu/checkfs v1.0.1/go.mod h1:VBk2qYxPz4l8nbLnT2I9LYHJ8ygxRxAJv14dBYJQCrw=
github.com/andreimerlescu/checkfs v1.0.3 h1:vnYAPI+Yu+4YfuvY8Jjnq26p7WhIvw4XjCJ5w3S0sjI=
github.com/andreimerlescu/checkfs v1.0.3/go.mod h1:ADaqjiRJf3gmyENLS3v9bJIaEH00IOeM48cXxVwy1JY=
github.com/andreimerlescu/checkfs v1.0.4 h1:pRXZGW1sfe+yXyWNUxmPC2IiX5yT3vF1V5O8PXulnFc=
github.com/andreimerlescu/checkfs v1.0.4/go.mod h1:ADaqjiRJf3gmyENLS3v9bJIaEH00IOeM48cXxVwy1JY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
Expand All @@ -10,14 +8,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
66 changes: 30 additions & 36 deletions internals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ func TestTree_checkAndSetFromEnv(t *testing.T) {
// create a new fig tree
var figs *figTree
figs = &figTree{
harvest: 1,
figs: make(map[string]*figFruit),
tracking: false,
withered: make(map[string]witheredFig),
sources: make(map[string]SourceConfig),
sourceLocker: sync.RWMutex{},
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
mu: sync.RWMutex{},
mutationsCh: make(chan Mutation, 1),
filterTests: true,
harvest: 1,
figs: make(map[string]*figFruit),
tracking: false,
withered: make(map[string]witheredFig),
aliases: make(map[string]string),
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
mu: sync.RWMutex{},
mutationsCh: make(chan Mutation, 1),
filterTests: true,
}

// assign an int to k
Expand Down Expand Up @@ -63,8 +62,7 @@ func TestTree_setValue(t *testing.T) {
ConfigFilePath string
figs map[string]*figFruit
withered map[string]witheredFig
sources map[string]SourceConfig
sourceLocker sync.RWMutex
aliases map[string]string
mu sync.RWMutex
tracking bool
mutationsCh chan Mutation
Expand All @@ -83,11 +81,10 @@ func TestTree_setValue(t *testing.T) {
{
name: "Set int value",
fields: fields{
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
mutationsCh: make(chan Mutation, 1),
sources: make(map[string]SourceConfig),
sourceLocker: sync.RWMutex{},
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
aliases: make(map[string]string),
mutationsCh: make(chan Mutation, 1),
},
args: args{
flagVal: new(int),
Expand All @@ -99,11 +96,10 @@ func TestTree_setValue(t *testing.T) {
{
name: "Set string value",
fields: fields{
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
mutationsCh: make(chan Mutation, 1),
sources: make(map[string]SourceConfig),
sourceLocker: sync.RWMutex{},
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
aliases: make(map[string]string),
mutationsCh: make(chan Mutation, 1),
},
args: args{
flagVal: new(string),
Expand All @@ -115,11 +111,10 @@ func TestTree_setValue(t *testing.T) {
{
name: "Invalid type",
fields: fields{
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
mutationsCh: make(chan Mutation, 1),
sources: make(map[string]SourceConfig),
sourceLocker: sync.RWMutex{},
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
aliases: make(map[string]string),
mutationsCh: make(chan Mutation, 1),
},
args: args{
flagVal: new(float32), // Unsupported type
Expand Down Expand Up @@ -160,15 +155,14 @@ func TestTree_setValue(t *testing.T) {

func TestTree_setValuesFromMap(t *testing.T) {
tree := &figTree{
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
sources: make(map[string]SourceConfig),
sourceLocker: sync.RWMutex{},
mu: sync.RWMutex{},
tracking: false,
mutationsCh: make(chan Mutation, 1),
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
filterTests: true,
figs: make(map[string]*figFruit),
withered: make(map[string]witheredFig),
aliases: make(map[string]string),
mu: sync.RWMutex{},
tracking: false,
mutationsCh: make(chan Mutation, 1),
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
filterTests: true,
}
m := map[string]interface{}{
"name": "yahuah",
Expand Down
Loading
Loading