-
Notifications
You must be signed in to change notification settings - Fork 142
/
diff.go
133 lines (117 loc) · 2.97 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2022 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cloudrun
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/pipe-cd/pipecd/pkg/diff"
)
const (
diffCommand = "diff"
)
type DiffResult struct {
Diff *diff.Result
Old ServiceManifest
New ServiceManifest
}
func (d *DiffResult) NoChange() bool {
return len(d.Diff.Nodes()) == 0
}
func Diff(old, new ServiceManifest, opts ...diff.Option) (*DiffResult, error) {
d, err := diff.DiffUnstructureds(*old.u, *new.u, opts...)
if err != nil {
return nil, err
}
if !d.HasDiff() {
return &DiffResult{Diff: d}, nil
}
ret := &DiffResult{
Old: old,
New: new,
Diff: d,
}
return ret, nil
}
type DiffRenderOptions struct {
// If true, use "diff" command to render.
UseDiffCommand bool
}
func (d *DiffResult) Render(opt DiffRenderOptions) string {
var b strings.Builder
opts := []diff.RenderOption{
diff.WithLeftPadding(1),
}
renderer := diff.NewRenderer(opts...)
if !opt.UseDiffCommand {
b.WriteString(renderer.Render(d.Diff.Nodes()))
} else {
d, err := diffByCommand(diffCommand, d.Old, d.New)
if err != nil {
b.WriteString(fmt.Sprintf("An error occurred while rendering diff (%v)", err))
} else {
b.Write(d)
}
}
b.WriteString("\n")
return b.String()
}
func diffByCommand(command string, old, new ServiceManifest) ([]byte, error) {
oldBytes, err := old.YamlBytes()
if err != nil {
return nil, err
}
newBytes, err := new.YamlBytes()
if err != nil {
return nil, err
}
oldFile, err := os.CreateTemp("", "old")
if err != nil {
return nil, err
}
defer os.Remove(oldFile.Name())
if _, err := oldFile.Write(oldBytes); err != nil {
return nil, err
}
newFile, err := os.CreateTemp("", "new")
if err != nil {
return nil, err
}
defer os.Remove(newFile.Name())
if _, err := newFile.Write(newBytes); err != nil {
return nil, err
}
var stdout, stderr bytes.Buffer
cmd := exec.Command(command, "-u", "-N", oldFile.Name(), newFile.Name())
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if stdout.Len() > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
err = nil
}
if err != nil {
return nil, fmt.Errorf("failed to run diff, err = %w, %s", err, stderr.String())
}
// Remove two-line header from output.
data := bytes.TrimSpace(stdout.Bytes())
rows := bytes.SplitN(data, []byte("\n"), 3)
if len(rows) == 3 {
return rows[2], nil
}
return data, nil
}