Compare commits

..

No commits in common. "034aa8648f356d024b17b9c726ee891f22a597a8" and "3d84f46112cc8cac7b264e0c462f72e88017447d" have entirely different histories.

11 changed files with 72 additions and 102 deletions

View File

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

View File

@ -32,21 +32,11 @@ var Fetch = &command.Command{
Action: func(cmd *command.Command) error { Action: func(cmd *command.Command) error {
paths := cmd.Arguments[0].ToValue().([]string) paths := cmd.Arguments[0].ToValue().([]string)
for _, path := range paths { for _, path := range paths {
local, err := config.Load(path, false) remote, err := config.Load(path, true)
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }
@ -55,11 +45,20 @@ var Fetch = &command.Command{
return err return err
} }
if err := local.AsFile(path); err != nil { if dryRun := cmd.Options["dry-run"].ToValue().(bool); dryRun {
return err 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
}
} }
logrus.Infof("Fetched %s => %s", remote.OPURL(), path) logrus.Infof("Updated %s", path)
} }
logrus.Info("Done") logrus.Info("Done")

View File

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

View File

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

2
go.mod
View File

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

8
go.sum
View File

@ -1,4 +1,6 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= 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 h1:PRpd8b8qRkrQB8kSlF2jD1NxRC0Vau78qg1zMC3EJgs=
git.rob.mx/nidito/chinampa v0.0.0-20230114231059-a14ffd07b963/go.mod h1:obhWsLkUIlKJyhfa7uunrSs2O44JBqsegSAtAvY2LRM= 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= github.com/1Password/connect-sdk-go v1.5.0 h1:F0WJcLSzGg3iXEDY49/ULdszYKsQLGTzn+2cyYXqiyk=
@ -43,6 +45,8 @@ 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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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.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 h1:rJD5HeGIT/2b5CDk63FVCwZA3qgYElfg+oQK7uH5pfE=
github.com/dlclark/regexp2 v1.8.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dlclark/regexp2 v1.8.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww= github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
@ -356,8 +360,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/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/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= gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
google.golang.org/genproto v0.0.0-20230117162540-28d6b9783ac4 h1:yF0uHwqqYt2tIL2F4hxRWA1ZFX43SEunWAK8MnQiclk= google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5 h1:wJT65XLOzhpSPCdAmmKfz94SlmnQzDzjm3Cj9k3fsXY=
google.golang.org/genproto v0.0.0-20230117162540-28d6b9783ac4/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= google.golang.org/genproto v0.0.0-20230113154510-dbe35b8444a5/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 h1:kd48UiU7EHsV4rnLyOJRuP/Il/UHE7gdDAQ+SZI7nZk=
google.golang.org/grpc v1.52.0/go.mod h1:pu6fVzoFb+NBYNAvQL08ic+lvB2IojljRYuun5vorUY= 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= 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 { func Update(vault, name string, item *op.Item) error {
remote, err := client.Get(vault, name) remote, err := client.Get(vault, name)
if err != nil { if err != nil {
if strings.Contains(err.Error(), fmt.Sprintf("\"%s\" isn't an item in ", name)) { if strings.Contains(err.Error(), fmt.Sprintf("\"%s\" isn't an item in the ", name)) {
return client.Create(item) return client.Create(item)
} }

View File

@ -156,21 +156,11 @@ func (cfg *Config) Merge(other *Config) error {
return cfg.Tree.Merge(other.Tree) return cfg.Tree.Merge(other.Tree)
} }
func (cfg *Config) OPURL() string { func (cfg *Config) DiffRemote(path string, redacted bool, stdout, stderr io.Writer) error {
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) logrus.Debugf("loading remote for %s", path)
remote, err := Load(path, true) remote, err := Load(path, true)
if err != nil { if err != nil {
if asFetch { return err
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} modes := []OutputMode{OutputModeNoComments, OutputModeSorted, OutputModeNoConfig, OutputModeStandardYAML}
@ -178,7 +168,6 @@ func (cfg *Config) DiffRemote(path string, redacted, asFetch bool, stdout, stder
modes = append(modes, OutputModeRedacted) modes = append(modes, OutputModeRedacted)
} }
logrus.Debugf("loading local for %s", path)
localBytes, err := cfg.AsYAML(modes...) localBytes, err := cfg.AsYAML(modes...)
if err != nil { if err != nil {
return err return err
@ -190,30 +179,18 @@ func (cfg *Config) DiffRemote(path string, redacted, asFetch bool, stdout, stder
} }
defer cleanupLocalDiff() defer cleanupLocalDiff()
file2 := "/dev/null" remoteBytes, err := remote.AsYAML(modes...)
opPath := "(new) " + cfg.OPURL() if err != nil {
return err
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()
} }
file2, cleanupRemoteDiff, err := tempfile(remoteBytes)
var diff *exec.Cmd if err != nil {
if asFetch { return err
diff = exec.Command("diff", "-u", "-L", path, file1, "-L", opPath, file2)
} else {
diff = exec.Command("diff", "-u", "-L", opPath, file2, "-L", path, file1)
} }
defer cleanupRemoteDiff()
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.Env = os.Environ()
diff.Stdout = stdout diff.Stdout = stdout

View File

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

View File

@ -24,11 +24,8 @@ func Load(ref string, preferRemote bool) (*Config, error) {
vault := "" vault := ""
if argIsYAMLFile(ref) { if argIsYAMLFile(ref) {
path, err := filepath.Abs(ref) var err error
if err != nil { name, vault, err = VaultAndNameFrom(ref, 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 { if err != nil {
return nil, err return nil, err
} }

View File

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