Compare commits

..

2 Commits

Author SHA1 Message Date
Roberto Hidalgo
034aa8648f update deps 2023-01-17 16:35:50 -06:00
Roberto Hidalgo
e3297de534 output diff on fetch/flush --dry-run 2023-01-17 16:25:18 -06:00
11 changed files with 104 additions and 74 deletions

View File

@ -11,7 +11,7 @@ import (
var Diff = &command.Command{
Path: []string{"diff"},
Summary: "Shows differences between local and remote configs",
Description: `Fetches remote and compares against local, ignoring comments but respecting order.`,
Description: `Fetches remote and compares against local, ignoring comments but respecting order. The diff output shows what would happen upon running ﹅joao fetch﹅. Specify ﹅--remote﹅ to show what would happen upon ﹅joao flush﹅`,
Arguments: command.Arguments{
{
Name: "config",
@ -26,7 +26,7 @@ var Diff = &command.Command{
Options: command.Options{
"output": {
Description: "How to format the differences",
Type: "string",
Type: command.ValueTypeString,
Default: "auto",
Values: &command.ValueSource{
Static: &[]string{
@ -34,15 +34,21 @@ var Diff = &command.Command{
},
},
},
"remote": {
Description: "Shows what would happen on `flush` instead of `fetch`",
Type: command.ValueTypeString,
Default: false,
},
"redacted": {
Description: "Compare redacted versions",
Type: "bool",
Type: command.ValueTypeBoolean,
Default: false,
},
},
Action: func(cmd *command.Command) error {
paths := cmd.Arguments[0].ToValue().([]string)
redacted := cmd.Options["redacted"].ToValue().(bool)
remote := cmd.Options["remote"].ToValue().(bool)
for _, path := range paths {
local, err := config.Load(path, false)
@ -50,7 +56,7 @@ var Diff = &command.Command{
return err
}
if err := local.DiffRemote(path, redacted, cmd.Cobra.OutOrStdout(), cmd.Cobra.OutOrStderr()); err != nil {
if err := local.DiffRemote(path, redacted, remote, cmd.Cobra.OutOrStdout(), cmd.Cobra.OutOrStderr()); err != nil {
return err
}
}

View File

@ -32,11 +32,21 @@ var Fetch = &command.Command{
Action: func(cmd *command.Command) error {
paths := cmd.Arguments[0].ToValue().([]string)
for _, path := range paths {
remote, err := config.Load(path, true)
local, err := config.Load(path, false)
if err != nil {
return err
}
local, err := config.Load(path, false)
if dryRun := cmd.Options["dry-run"].ToValue().(bool); dryRun {
logrus.Warnf("dry-run: comparing %s to %s", local.OPURL(), path)
if err := local.DiffRemote(path, false, true, cmd.Cobra.OutOrStdout(), cmd.Cobra.OutOrStderr()); err != nil {
return err
}
logrus.Warnf("dry-run: did not update %s", path)
continue
}
remote, err := config.Load(path, true)
if err != nil {
return err
}
@ -45,20 +55,11 @@ var Fetch = &command.Command{
return err
}
if dryRun := cmd.Options["dry-run"].ToValue().(bool); dryRun {
b, err := local.AsYAML()
if err != nil {
return err
}
logrus.Warnf("dry-run: would write to %s", path)
_, _ = cmd.Cobra.OutOrStdout().Write(b)
} else {
if err := local.AsFile(path); err != nil {
return err
}
if err := local.AsFile(path); err != nil {
return err
}
logrus.Infof("Updated %s", path)
logrus.Infof("Fetched %s => %s", remote.OPURL(), path)
}
logrus.Info("Done")

View File

@ -69,36 +69,26 @@ func TestFetch(t *testing.T) {
t.Fatalf("could not get: %s", err)
}
expected := `_config: !!joao
name: some:test
vault: example
# not sorted on purpose
int: 1 # line
# foot
string: pato
bool: false
secret: !!secret very secret
nested:
int: 1
bool: true
list:
- 1
- 2
- 3
secret: !!secret very secret
second_secret: !!secret very secret
string: quem
list:
- one
- two
- three
o:
ganso:
gosto: da dupla
e-fez-tambem:
- quém!
- quém!
- quém!`
expected := `--- /Users/roberto/src/joao/test.yaml
+++ op://example/some:test
@@ -1,4 +1,8 @@
bool: false
+e-fez-tambem:
+ - quém!
+ - quém!
+ - quém!
int: 1
list:
- one
@@ -14,5 +18,8 @@
second_secret: !!secret very secret
secret: !!secret very secret
string: quem
+o:
+ ganso:
+ gosto: da dupla
secret: !!secret very secret
string: pato`
if got := out.String(); strings.TrimSpace(got) != expected {
t.Fatalf("did not get expected output:\nwanted: %s\ngot: %s", expected, got)

View File

@ -38,8 +38,9 @@ var Flush = &command.Command{
},
Action: func(cmd *command.Command) error {
paths := cmd.Arguments[0].ToValue().([]string)
dryRun := cmd.Options["dry-run"].ToValue().(bool)
if dryRun := cmd.Options["dry-run"].ToValue().(bool); dryRun {
if dryRun {
opclient.Use(&opclient.CLI{DryRun: true})
}
@ -49,6 +50,15 @@ var Flush = &command.Command{
return err
}
if dryRun {
logrus.Warnf("dry-run: comparing %s to %s", path, cfg.OPURL())
if err := cfg.DiffRemote(path, false, false, cmd.Cobra.OutOrStdout(), cmd.Cobra.OutOrStderr()); err != nil {
return err
}
logrus.Warnf("dry-run: did not update %s", cfg.OPURL())
continue
}
if err := opclient.Update(cfg.Vault, cfg.Name, cfg.ToOP()); err != nil {
return fmt.Errorf("could not flush to 1password: %w", err)
}
@ -58,6 +68,7 @@ var Flush = &command.Command{
return err
}
}
logrus.Infof("Flushed %s to %s", path, cfg.OPURL())
}
logrus.Info("Done")

2
go.mod
View File

@ -86,7 +86,7 @@ require (
golang.org/x/term v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5 // indirect
google.golang.org/genproto v0.0.0-20230117162540-28d6b9783ac4 // indirect
google.golang.org/grpc v1.52.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect

8
go.sum
View File

@ -1,6 +1,4 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.rob.mx/nidito/chinampa v0.0.0-20230111054043-db21a53b2d20 h1:hHzbCNAFx9wdi3NyliugN6dTEX8dGsuup6wk2+8TmnI=
git.rob.mx/nidito/chinampa v0.0.0-20230111054043-db21a53b2d20/go.mod h1:obhWsLkUIlKJyhfa7uunrSs2O44JBqsegSAtAvY2LRM=
git.rob.mx/nidito/chinampa v0.0.0-20230114231059-a14ffd07b963 h1:PRpd8b8qRkrQB8kSlF2jD1NxRC0Vau78qg1zMC3EJgs=
git.rob.mx/nidito/chinampa v0.0.0-20230114231059-a14ffd07b963/go.mod h1:obhWsLkUIlKJyhfa7uunrSs2O44JBqsegSAtAvY2LRM=
github.com/1Password/connect-sdk-go v1.5.0 h1:F0WJcLSzGg3iXEDY49/ULdszYKsQLGTzn+2cyYXqiyk=
@ -45,8 +43,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
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/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.8.0 h1:rJD5HeGIT/2b5CDk63FVCwZA3qgYElfg+oQK7uH5pfE=
github.com/dlclark/regexp2 v1.8.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
@ -360,8 +356,8 @@ gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJ
gonum.org/v1/gonum v0.8.2/go.mod h1:oe/vMfY3deqTw+1EZJhuvEW2iwGF1bW9wwu7XCu0+v0=
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5 h1:wJT65XLOzhpSPCdAmmKfz94SlmnQzDzjm3Cj9k3fsXY=
google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/genproto v0.0.0-20230117162540-28d6b9783ac4 h1:yF0uHwqqYt2tIL2F4hxRWA1ZFX43SEunWAK8MnQiclk=
google.golang.org/genproto v0.0.0-20230117162540-28d6b9783ac4/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/grpc v1.52.0 h1:kd48UiU7EHsV4rnLyOJRuP/Il/UHE7gdDAQ+SZI7nZk=
google.golang.org/grpc v1.52.0/go.mod h1:pu6fVzoFb+NBYNAvQL08ic+lvB2IojljRYuun5vorUY=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=

View File

@ -34,7 +34,7 @@ func Get(vault, name string) (*op.Item, error) {
func Update(vault, name string, item *op.Item) error {
remote, err := client.Get(vault, name)
if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf("\"%s\" isn't an item in the ", name)) {
if strings.Contains(err.Error(), fmt.Sprintf("\"%s\" isn't an item in ", name)) {
return client.Create(item)
}

View File

@ -156,11 +156,21 @@ func (cfg *Config) Merge(other *Config) error {
return cfg.Tree.Merge(other.Tree)
}
func (cfg *Config) DiffRemote(path string, redacted bool, stdout, stderr io.Writer) error {
func (cfg *Config) OPURL() string {
return fmt.Sprintf("op://%s/%s", cfg.Vault, cfg.Name)
}
func (cfg *Config) DiffRemote(path string, redacted, asFetch bool, stdout, stderr io.Writer) error {
logrus.Debugf("loading remote for %s", path)
remote, err := Load(path, true)
if err != nil {
return err
if asFetch {
return err
}
if !strings.Contains(err.Error(), " isn't an item in ") {
return fmt.Errorf("could not fetch remote item: %w", err)
}
}
modes := []OutputMode{OutputModeNoComments, OutputModeSorted, OutputModeNoConfig, OutputModeStandardYAML}
@ -168,6 +178,7 @@ func (cfg *Config) DiffRemote(path string, redacted bool, stdout, stderr io.Writ
modes = append(modes, OutputModeRedacted)
}
logrus.Debugf("loading local for %s", path)
localBytes, err := cfg.AsYAML(modes...)
if err != nil {
return err
@ -179,18 +190,30 @@ func (cfg *Config) DiffRemote(path string, redacted bool, stdout, stderr io.Writ
}
defer cleanupLocalDiff()
remoteBytes, err := remote.AsYAML(modes...)
if err != nil {
return err
}
file2, cleanupRemoteDiff, err := tempfile(remoteBytes)
if err != nil {
return err
}
defer cleanupRemoteDiff()
file2 := "/dev/null"
opPath := "(new) " + cfg.OPURL()
if remote != nil {
remoteBytes, err := remote.AsYAML(modes...)
if err != nil {
return err
}
f2, cleanupRemoteDiff, err := tempfile(remoteBytes)
if err != nil {
return err
}
file2 = f2
opPath = remote.OPURL()
defer cleanupRemoteDiff()
}
var diff *exec.Cmd
if asFetch {
diff = exec.Command("diff", "-u", "-L", path, file1, "-L", opPath, file2)
} else {
diff = exec.Command("diff", "-u", "-L", opPath, file2, "-L", path, file1)
}
opPath := fmt.Sprintf("op://%s/%s", cfg.Vault, remote.Name)
diff := exec.Command("diff", "-u", "-L", path, file1, "-L", opPath, file2)
diff.Env = os.Environ()
diff.Stdout = stdout

View File

@ -197,7 +197,7 @@ func (e *Entry) asNode() *yaml.Node {
if yamlOutput.Has(OutputModeStandardYAML) {
if e.IsScalar() {
if len(e.Path) >= 0 {
if len(e.Path) > 0 {
if !strings.Contains(n.Value, "\n") {
n.Style &= yaml.FoldedStyle
} else {

View File

@ -24,8 +24,11 @@ func Load(ref string, preferRemote bool) (*Config, error) {
vault := ""
if argIsYAMLFile(ref) {
var err error
name, vault, err = VaultAndNameFrom(ref, nil)
path, err := filepath.Abs(ref)
if err != nil {
return nil, fmt.Errorf("could not find asbolute path to file %s: %w", ref, err)
}
name, vault, err = VaultAndNameFrom(path, nil)
if err != nil {
return nil, err
}

View File

@ -43,7 +43,7 @@ const (
OutputModeSorted OutputMode = 4
// OutputModeNoConfig does not output the _config key if any.
OutputModeNoConfig OutputMode = 8
// OutputModeStandardYAML formats strings and arrays uniformly
// OutputModeStandardYAML formats strings and arrays uniformly.
OutputModeStandardYAML OutputMode = 16
)