-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
169 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package sources | ||
|
||
import ( | ||
"fmt" | ||
. "github.com/ahmetalpbalkan/go-linq" | ||
"github.com/zeroturnaround/configo/flatmap" | ||
) | ||
|
||
type CompositeSource struct { | ||
Sources []map[string]interface{} `json:"sources"` | ||
} | ||
|
||
func (compositeSource *CompositeSource) Get() (map[string]interface{}, error) { | ||
|
||
resultEnv := make(map[string]interface{}) | ||
|
||
_, err := From(compositeSource.Sources). | ||
Select(func(rawSource T) (T, error) { | ||
loader, err := GetSource(rawSource.(map[string]interface{})) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Failed to parse source: %s", err) | ||
} | ||
|
||
return loader, nil | ||
}). | ||
// Resolve in parallel because some sources might use IO and will take some time | ||
AsParallel().AsOrdered(). | ||
Select(func(loader T) (T, error) { | ||
result, err := loader.(Source).Get() | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Failed to resolve source: %s", err) | ||
} | ||
|
||
return result, nil | ||
}). | ||
AsSequential(). | ||
CountBy(func(partialConfig T) (bool, error) { | ||
for key, value := range flatmap.Flatten(partialConfig.(map[string]interface{})) { | ||
resultEnv[key] = value | ||
} | ||
return true, nil | ||
}) | ||
|
||
return resultEnv, err | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env bats | ||
|
||
load ../test_helper | ||
|
||
@test "sources: composite works" { | ||
run_container <<EOC | ||
export CONFIGO_SOURCE_0='{ | ||
"type": "composite", | ||
"sources": [ | ||
{ | ||
"type": "shell", | ||
"format": "properties", | ||
"command": "echo MY_COOL_PROP_1=123" | ||
}, | ||
{ | ||
"type": "shell", | ||
"format": "properties", | ||
"command": "echo MY_COOL_PROP_1=override" | ||
}, | ||
{ | ||
"type": "shell", | ||
"format": "properties", | ||
"command": "echo MY_COOL_PROP_2=456" | ||
} | ||
] | ||
}' | ||
configo printenv MY_COOL_PROP_1 | ||
configo printenv MY_COOL_PROP_2 | ||
EOC | ||
|
||
assert_success "override | ||
456" | ||
} | ||
|
||
@test "sources: composite nested sources works" { | ||
run_container <<EOC | ||
export CONFIGO_SOURCE_0='{ | ||
"type": "composite", | ||
"sources": [ | ||
{ | ||
"type": "composite", | ||
"sources": [ | ||
{ | ||
"type": "shell", | ||
"format": "properties", | ||
"command": "echo MY_COOL_PROP_1=123" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "shell", | ||
"format": "properties", | ||
"command": "echo MY_COOL_PROP_2=456" | ||
} | ||
] | ||
}' | ||
configo printenv MY_COOL_PROP_1 | ||
configo printenv MY_COOL_PROP_2 | ||
EOC | ||
|
||
assert_success "123 | ||
456" | ||
} |
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