forked from Lucretius/vault_raft_snapshot_agent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve path with tag resolver (#11)
* resolve path using go tags instead of viper-decode-hook
- Loading branch information
1 parent
5ddc359
commit 284513f
Showing
7 changed files
with
162 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
internal/app/vault_raft_snapshot_agent/config/resolve-path-tag.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
const ( | ||
tagFieldName = "resolve-path" | ||
) | ||
|
||
var ( | ||
errorInvalidType error = errors.New("subject must be a struct passed by pointer") | ||
) | ||
|
||
type pathResolver struct { | ||
baseDir string | ||
} | ||
|
||
func newPathResolver(baseDir string) pathResolver { | ||
return pathResolver{baseDir} | ||
} | ||
|
||
func (r pathResolver) Resolve(subject interface{}) error { | ||
if reflect.TypeOf(subject).Kind() != reflect.Ptr { | ||
return errorInvalidType | ||
} | ||
|
||
s := reflect.ValueOf(subject).Elem() | ||
|
||
return r.resolve(s) | ||
} | ||
|
||
func (r pathResolver) resolve(value reflect.Value) error { | ||
t := value.Type() | ||
|
||
if t.Kind() != reflect.Struct { | ||
return errorInvalidType | ||
} | ||
|
||
for i := 0; i < t.NumField(); i++ { | ||
f := value.Field(i) | ||
|
||
if !f.CanSet() { | ||
continue | ||
} | ||
|
||
if f.Kind() == reflect.Ptr { | ||
f = f.Elem() | ||
} | ||
|
||
if f.Kind() == reflect.Struct { | ||
if err := r.resolve(f); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if f.Kind() != reflect.String || f.String() == "" { | ||
continue | ||
} | ||
|
||
if baseDir, present := t.Field(i).Tag.Lookup(tagFieldName); present { | ||
if err := r.resolvePath(f, baseDir); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r pathResolver) resolvePath(field reflect.Value, baseDir string) error { | ||
path := field.String() | ||
if baseDir == "" { | ||
baseDir = r.baseDir | ||
} | ||
|
||
if !filepath.IsAbs(path) && !strings.HasPrefix(path, "/") { | ||
path = filepath.Join(baseDir, path) | ||
field.Set(reflect.ValueOf(path).Convert(field.Type())) | ||
} | ||
|
||
return nil | ||
} |
58 changes: 58 additions & 0 deletions
58
internal/app/vault_raft_snapshot_agent/config/resolve-path-tag_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestResolvesRelativePaths(t *testing.T) { | ||
var test struct { | ||
Path string `resolve-path:""` | ||
FixedPath string `resolve-path:"/tmp/"` | ||
Other string | ||
AbsolutePath string `resolve-path:""` | ||
} | ||
test.Path = "./relative" | ||
test.FixedPath = "./fixed" | ||
test.Other = "./other" | ||
test.AbsolutePath = "/test/abs" | ||
|
||
dir := t.TempDir() | ||
resolver := newPathResolver(dir) | ||
err := resolver.Resolve(&test) | ||
|
||
assert.NoError(t, err, "resolver.resolve failed unexepectedly") | ||
|
||
assert.Equal(t, filepath.Clean(fmt.Sprintf("%s/relative", dir)), test.Path) | ||
assert.Equal(t, filepath.Clean("/tmp/fixed"), test.FixedPath) | ||
assert.Equal(t, "/test/abs", test.AbsolutePath) | ||
assert.Equal(t, "./other", test.Other) | ||
} | ||
|
||
func TestResolvesRecursively(t *testing.T) { | ||
type inner struct { | ||
Path string `resolve-path:""` | ||
} | ||
|
||
innerPtr := inner{"./innerPtr"} | ||
|
||
var outer struct { | ||
Inner inner | ||
InnerPtr *inner | ||
} | ||
outer.Inner.Path = "./inner" | ||
outer.InnerPtr = &innerPtr | ||
|
||
dir := t.TempDir() | ||
resolver := newPathResolver(dir) | ||
err := resolver.Resolve(&outer) | ||
|
||
assert.NoError(t, err, "resolver.resolve failed unexepectedly") | ||
|
||
assert.Equal(t, filepath.Clean(fmt.Sprintf("%s/inner", dir)), outer.Inner.Path) | ||
assert.Equal(t, filepath.Clean(fmt.Sprintf("%s/innerPtr", dir)), innerPtr.Path) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
internal/app/vault_raft_snapshot_agent/vault/auth/kubernetes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters