66package configdiff
77
88import (
9- "fmt"
10- "io"
11- "strings"
12-
13- "github.com/fatih/color"
14- "github.com/hexops/gotextdiff"
15- "github.com/hexops/gotextdiff/myers"
16- "github.com/hexops/gotextdiff/span"
17-
189 "github.com/siderolabs/talos/pkg/machinery/config"
1910 "github.com/siderolabs/talos/pkg/machinery/config/encoder"
11+ "github.com/siderolabs/talos/pkg/machinery/textdiff"
2012)
2113
22- // Diff outputs (optionally) colorized diff between two machine configurations.
14+ // DiffConfigs returns a string representation of the diff between two machine configurations.
2315//
2416// One of the resources might be nil.
25- func Diff ( w io. Writer , oldCfg , newCfg config.Encoder ) error {
17+ func DiffConfigs ( oldCfg , newCfg config.Encoder ) ( string , error ) {
2618 var (
2719 oldYaml , newYaml []byte
2820 err error
@@ -31,99 +23,16 @@ func Diff(w io.Writer, oldCfg, newCfg config.Encoder) error {
3123 if oldCfg != nil {
3224 oldYaml , err = oldCfg .EncodeBytes (encoder .WithComments (encoder .CommentsDisabled ))
3325 if err != nil {
34- return err
26+ return "" , err
3527 }
3628 }
3729
3830 if newCfg != nil {
3931 newYaml , err = newCfg .EncodeBytes (encoder .WithComments (encoder .CommentsDisabled ))
4032 if err != nil {
41- return err
33+ return "" , err
4234 }
4335 }
4436
45- edits := myers .ComputeEdits (span .URIFromPath ("a" ), string (oldYaml ), string (newYaml ))
46- diff := gotextdiff .ToUnified ("a" , "b" , string (oldYaml ), edits )
47-
48- outputDiff (w , diff , true )
49-
50- return nil
51- }
52-
53- // DiffToString returns a string representation of the diff between two machine configurations.
54- func DiffToString (oldCfg , newCfg config.Encoder ) (string , error ) {
55- var sb strings.Builder
56-
57- err := Diff (& sb , oldCfg , newCfg )
58-
59- return sb .String (), err
60- }
61-
62- //nolint:gocyclo
63- func outputDiff (w io.Writer , u gotextdiff.Unified , noColor bool ) {
64- if len (u .Hunks ) == 0 {
65- return
66- }
67-
68- bold := color .New (color .Bold )
69- cyan := color .New (color .FgCyan )
70- red := color .New (color .FgRed )
71- green := color .New (color .FgGreen )
72-
73- if noColor {
74- bold .DisableColor ()
75- cyan .DisableColor ()
76- red .DisableColor ()
77- green .DisableColor ()
78- }
79-
80- bold .Fprintf (w , "--- %s\n " , u .From ) //nolint:errcheck
81- bold .Fprintf (w , "+++ %s\n " , u .To ) //nolint:errcheck
82-
83- for _ , hunk := range u .Hunks {
84- fromCount , toCount := 0 , 0
85-
86- for _ , l := range hunk .Lines {
87- switch l .Kind { //nolint:exhaustive
88- case gotextdiff .Delete :
89- fromCount ++
90- case gotextdiff .Insert :
91- toCount ++
92- default :
93- fromCount ++
94- toCount ++
95- }
96- }
97-
98- cyan .Fprintf (w , "@@" ) //nolint:errcheck
99-
100- if fromCount > 1 {
101- cyan .Fprintf (w , " -%d,%d" , hunk .FromLine , fromCount ) //nolint:errcheck
102- } else {
103- cyan .Fprintf (w , " -%d" , hunk .FromLine ) //nolint:errcheck
104- }
105-
106- if toCount > 1 {
107- cyan .Fprintf (w , " +%d,%d" , hunk .ToLine , toCount ) //nolint:errcheck
108- } else {
109- cyan .Fprintf (w , " +%d" , hunk .ToLine ) //nolint:errcheck
110- }
111-
112- cyan .Fprintf (w , " @@\n " ) //nolint:errcheck
113-
114- for _ , l := range hunk .Lines {
115- switch l .Kind { //nolint:exhaustive
116- case gotextdiff .Delete :
117- red .Fprintf (w , "-%s" , l .Content ) //nolint:errcheck
118- case gotextdiff .Insert :
119- green .Fprintf (w , "+%s" , l .Content ) //nolint:errcheck
120- default :
121- fmt .Fprintf (w , " %s" , l .Content )
122- }
123-
124- if ! strings .HasSuffix (l .Content , "\n " ) {
125- red .Fprintf (w , "\n \\ No newline at end of file\n " ) //nolint:errcheck
126- }
127- }
128- }
37+ return textdiff .Diff (string (oldYaml ), string (newYaml ))
12938}
0 commit comments