From b3e189fa8d69178e5315e4e42bcc889853e6fa12 Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Sun, 15 Jan 2023 02:19:05 -0600 Subject: [PATCH] prevent diff from tripping on formatting --- cmd/flush.go | 2 +- internal/op-client/op.go | 2 +- pkg/config/config.go | 4 ++-- pkg/config/entry.go | 19 ++++++++++++++++++- pkg/config/input.go | 4 ++-- pkg/config/output.go | 2 ++ 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cmd/flush.go b/cmd/flush.go index e788311..ec38874 100644 --- a/cmd/flush.go +++ b/cmd/flush.go @@ -54,7 +54,7 @@ var Flush = &command.Command{ } if cmd.Options["redact"].ToValue().(bool) { - if err := cfg.AsFile(path); err != nil { + if err := cfg.AsFile(path, config.OutputModeRedacted); err != nil { return err } } diff --git a/internal/op-client/op.go b/internal/op-client/op.go index 9fbad1e..a1dc038 100644 --- a/internal/op-client/op.go +++ b/internal/op-client/op.go @@ -42,7 +42,7 @@ func Update(vault, name string, item *op.Item) error { } if remote.GetValue("password") == item.GetValue("password") { - logrus.Warn("item is already up to date") + logrus.Warnf("item %s/%s is already up to date", item.Vault.ID, item.Title) return nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index 6c695e7..5079c8b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -162,7 +162,7 @@ func (cfg *Config) DiffRemote(path string, stdout io.Writer, stderr io.Writer) e return err } - localBytes, err := cfg.AsYAML(OutputModeNoComments, OutputModeSorted, OutputModeNoConfig) + localBytes, err := cfg.AsYAML(OutputModeNoComments, OutputModeSorted, OutputModeNoConfig, OutputModeStandardYAML) if err != nil { return err } @@ -173,7 +173,7 @@ func (cfg *Config) DiffRemote(path string, stdout io.Writer, stderr io.Writer) e } defer cleanupLocalDiff() - remoteBytes, err := remote.AsYAML(OutputModeNoComments, OutputModeSorted) + remoteBytes, err := remote.AsYAML(OutputModeNoComments, OutputModeSorted, OutputModeStandardYAML) if err != nil { return err } diff --git a/pkg/config/entry.go b/pkg/config/entry.go index 769682f..d3fc4bb 100644 --- a/pkg/config/entry.go +++ b/pkg/config/entry.go @@ -195,6 +195,23 @@ func (e *Entry) asNode() *yaml.Node { n.FootComment = e.FootComment } + if yamlOutput.Has(OutputModeStandardYAML) { + if e.IsScalar() { + if len(e.Path) >= 0 { + if !strings.Contains(n.Value, "\n") { + n.Style &= yaml.FoldedStyle + } else { + n.Style &= yaml.FlowStyle + } + } + } else { + n.Style = yaml.FoldedStyle | yaml.LiteralStyle + for _, v := range n.Content { + v.Style = yaml.FlowStyle + } + } + } + return n } @@ -209,7 +226,7 @@ func (e *Entry) MarshalYAML() (*yaml.Node, error) { } n.Content = append(n.Content, node) } - } else { + } else if e.Kind == yaml.MappingNode || e.Kind == yaml.DocumentNode { entries := e.Contents() if len(entries)%2 != 0 { return nil, fmt.Errorf("cannot decode odd numbered contents list: %s", e.Path) diff --git a/pkg/config/input.go b/pkg/config/input.go index 3631195..e4bdf49 100644 --- a/pkg/config/input.go +++ b/pkg/config/input.go @@ -14,7 +14,7 @@ import ( "gopkg.in/yaml.v3" ) -const warnChecksumMismatch = `1Password item changed and checksum was not updated. +const warnChecksumMismatch = `1Password item %s changed and checksum was not updated. Expected: %s found : %s` @@ -111,7 +111,7 @@ func FromOP(item *op.Item) (*Config, error) { } if cs := checksum(item.Fields); cs != item.GetValue("password") { - logrus.Warnf(warnChecksumMismatch, cs, item.GetValue("password")) + logrus.Warnf(warnChecksumMismatch, fmt.Sprintf("%s/%s", item.Vault.ID, item.Title), cs, item.GetValue("password")) } err := cfg.Tree.FromOP(item.Fields) return cfg, err diff --git a/pkg/config/output.go b/pkg/config/output.go index b640c63..f01e469 100644 --- a/pkg/config/output.go +++ b/pkg/config/output.go @@ -43,6 +43,8 @@ const ( OutputModeSorted OutputMode = 4 // OutputModeNoConfig does not output the _config key if any. OutputModeNoConfig OutputMode = 8 + // OutputModeStandardYAML formats strings and arrays uniformly + OutputModeStandardYAML OutputMode = 16 ) var defaultYamlOutput = &outputOptions{OutputModeRoundTrip}