Skip to content

Commit

Permalink
preserve --key=val1,val2 ordering if --out=raw is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
mkatychev committed Jan 29, 2021
1 parent f8da5f2 commit 9df1e69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/cogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func main() {
// convert all key values to uppercase
output, err = godotenv.Marshal(modKeys(cfgMap, modFuncs...))
case cogs.Raw:
output = getRawValue(cfgMap, conf.Delimiter)
// it's important for raw to preserve the order of keys specified
output, err = getRawValue(cfgMap, strings.Split(conf.Keys, ","), conf.Delimiter)
}
ifErr(err)

Expand Down
14 changes: 9 additions & 5 deletions cmd/cogs/optparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ifErr(err error) {
}
}

func getRawValue(cfgMap map[string]interface{}, delimiter string) string {
func getRawValue(cfgMap map[string]interface{}, keyList []string, delimiter string) (string, error) {
var values []string
// Interpret --sep='\n' and --sep='\t' as newlines and tabs
switch delimiter {
Expand All @@ -28,11 +28,15 @@ func getRawValue(cfgMap map[string]interface{}, delimiter string) string {
case "\\t":
delimiter = "\t"
}
// for now, no delimiter
for _, v := range cfgMap {
values = append(values, fmt.Sprintf("%s", v))
// it's important for raw to preserve the order of keys specified
for _, v := range keyList {
keyName, ok := cfgMap[v]
if !ok {
return "", fmt.Errorf("getRawValue: key %s is missing from cfgMap")
}
values = append(values, fmt.Sprintf("%s", keyName))
}
return strings.Join(values, delimiter)
return strings.Join(values, delimiter), nil

}

Expand Down
12 changes: 9 additions & 3 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
// read format derived from filepath suffix
rWhole readType = "whole" // indicates to associate the entirety of a file to the given key name
deferred readType = "" // defer file config type to filename suffix
rGear readType = "gear" // treat TOML table as a nested gear object
)

// Validate ensures that a string is a valid readType enum
Expand All @@ -52,6 +53,8 @@ func (t readType) String() string {
return "complex json"
case rWhole:
return "whole file"
case rGear:
return "gear object"
case deferred:
return "deferred"
default:
Expand Down Expand Up @@ -194,7 +197,10 @@ type visitor struct {

// SetValue assigns the Value for a given Cfg using the existing Cfg.Path and Cfg.SubPath
func (vi *visitor) SetValue(cfg *Cfg) (err error) {
if cfg.readType == rWhole || cfg.readType == rJSONComplex {
switch cfg.readType {
case rWhole, rJSONComplex:
return vi.visitComplex(cfg)
case rGear:
return vi.visitComplex(cfg)
}

Expand All @@ -212,7 +218,7 @@ func (vi *visitor) SetValue(cfg *Cfg) (err error) {
return err
}

supporedKind := func() bool {
supportedKind := func() bool {
for _, kind := range []yaml.Kind{yaml.MappingNode, yaml.ScalarNode, yaml.SequenceNode} {
if node.Kind == kind {
return true
Expand All @@ -221,7 +227,7 @@ func (vi *visitor) SetValue(cfg *Cfg) (err error) {
return false
}()

if !supporedKind {
if !supportedKind {
return fmt.Errorf("%s: NodeKind/readType unsupported: %s/%s",
cfg.Name, kindStr[node.Kind], cfg.readType)
}
Expand Down

0 comments on commit 9df1e69

Please sign in to comment.