-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: goconfig merge files <src_path> <dest_path>
NOTE: upgrade from implicit gopkg.in/yaml.v2 to explicit gopkg.in/yaml.v3 to fix go-yaml/yaml#139
- Loading branch information
1 parent
37a7698
commit 7a651f3
Showing
8 changed files
with
549 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
Feature: merge files <src_path> <dest_path> | ||
|
||
this command can be used to merge two files together and the | ||
result is written to standard out and formatted based on the | ||
output flags (defaulting to yaml) | ||
|
||
NOTE: the keys in yaml output are sorted | ||
|
||
Background: | ||
Given I have installed "goconfig" locally into the path | ||
And a file named "config/app1/default.yaml" with: | ||
""" | ||
log_level: WARN | ||
env: REQUIRED | ||
auth: | ||
username: default_user | ||
password: default_pass | ||
""" | ||
And a file named "config/app1/dev.yaml" with: | ||
""" | ||
log_level: DEBUG | ||
env: dev | ||
auth: | ||
password: dev_pass | ||
""" | ||
|
||
# @announce-stdout | ||
Scenario: yaml merge to yaml output sorts keys | ||
When I successfully run `goconfig merge files config/app1/dev.yaml config/app1/default.yaml -o yaml` | ||
Then the stdout should contain: | ||
""" | ||
auth: | ||
password: dev_pass | ||
username: default_user | ||
env: dev | ||
log_level: DEBUG | ||
""" | ||
|
||
Scenario: yaml merge to json output sorts keys | ||
When I successfully run `goconfig merge files config/app1/dev.yaml config/app1/default.yaml -o json` | ||
Then the stdout should contain: | ||
""" | ||
{ | ||
"auth": { | ||
"password": "dev_pass", | ||
"username": "default_user" | ||
}, | ||
"env": "dev", | ||
"log_level": "DEBUG" | ||
} | ||
""" |
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,5 @@ | ||
package app | ||
|
||
import "github.com/spf13/afero" | ||
|
||
var Fs = afero.NewOsFs() |
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,19 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/davidalpert/go-printers/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdMerge(ioStreams printers.IOStreams) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "merge", | ||
Aliases: []string{"m"}, | ||
Short: "merge subcommands", | ||
Args: cobra.NoArgs, | ||
} | ||
|
||
cmd.AddCommand(NewCmdMergeFiles(ioStreams)) | ||
|
||
return cmd | ||
} |
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,93 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/davidalpert/go-deep-merge/internal/app" | ||
v1 "github.com/davidalpert/go-deep-merge/v1" | ||
"github.com/davidalpert/go-printers/v1" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type MergeFilesOptions struct { | ||
*printers.PrinterOptions | ||
Source []byte | ||
Destination []byte | ||
Debug bool | ||
} | ||
|
||
func NewMergeFilesOptions(ioStreams printers.IOStreams) *MergeFilesOptions { | ||
return &MergeFilesOptions{ | ||
PrinterOptions: printers.NewPrinterOptions().WithStreams(ioStreams).WithDefaultOutput("text"), | ||
} | ||
} | ||
|
||
func NewCmdMergeFiles(ioStreams printers.IOStreams) *cobra.Command { | ||
o := NewMergeFilesOptions(ioStreams) | ||
var cmd = &cobra.Command{ | ||
Use: "files <src_file> <dest_file>", | ||
Short: "merge two config files together", | ||
Aliases: []string{"f", "fs", "file"}, | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := o.Complete(cmd, args); err != nil { | ||
return err | ||
} | ||
if err := o.Validate(); err != nil { | ||
return err | ||
} | ||
return o.Run() | ||
}, | ||
} | ||
|
||
o.PrinterOptions.AddPrinterFlags(cmd.Flags()) | ||
cmd.Flags().BoolVarP(&o.Debug, "debug", "d", false, "enable debug output") | ||
|
||
return cmd | ||
} | ||
|
||
// Complete the options | ||
func (o *MergeFilesOptions) Complete(cmd *cobra.Command, args []string) error { | ||
if b, err := afero.ReadFile(app.Fs, args[0]); err != nil { | ||
return fmt.Errorf("reading %#v: %#v", args[0], err) | ||
} else { | ||
o.Source = b | ||
} | ||
if b, err := afero.ReadFile(app.Fs, args[1]); err != nil { | ||
return fmt.Errorf("reading %#v: %#v", args[1], err) | ||
} else { | ||
o.Destination = b | ||
} | ||
return nil | ||
} | ||
|
||
// Validate the options | ||
func (o *MergeFilesOptions) Validate() error { | ||
return o.PrinterOptions.Validate() | ||
} | ||
|
||
// Run the command | ||
func (o *MergeFilesOptions) Run() error { | ||
var src, dest map[string]interface{} | ||
if err := yaml.Unmarshal(o.Source, &src); err != nil { | ||
return fmt.Errorf("unmarshalling src: %#v", err) | ||
} | ||
|
||
if err := yaml.Unmarshal(o.Destination, &dest); err != nil { | ||
return fmt.Errorf("unmarshalling dest: %#v", err) | ||
} | ||
|
||
//fmt.Fprintln(o.Out, "source:") | ||
//o.WriteOutput(src) | ||
//fmt.Fprintln(o.Out, "dest:") | ||
//o.WriteOutput(dest) | ||
//fmt.Fprintln(o.Out, "result:") | ||
|
||
r, err := v1.MergeWithOptions(src, dest, v1.NewConfigDeeperMergeBang().WithMergeHashArrays(true).WithDebug(o.Debug)) | ||
if err != nil { | ||
return fmt.Errorf("merging files: %#v", err) | ||
} | ||
|
||
return o.WriteOutput(r) | ||
} |
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