-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdiffidentity.go
More file actions
197 lines (160 loc) · 6.13 KB
/
diffidentity.go
File metadata and controls
197 lines (160 loc) · 6.13 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package component
import (
"encoding/json"
"fmt"
"sort"
"github.com/microsoft/azure-linux-dev-tools/internal/app/azldev"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
"github.com/spf13/cobra"
)
func diffIdentityOnAppInit(_ *azldev.App, parentCmd *cobra.Command) {
parentCmd.AddCommand(NewDiffIdentityCommand())
}
// diffIdentityArgCount is the number of positional arguments required by the diff-identity command.
const diffIdentityArgCount = 2
// NewDiffIdentityCommand constructs a [cobra.Command] for "component diff-identity".
func NewDiffIdentityCommand() *cobra.Command {
var options struct {
ChangedOnly bool
}
cmd := &cobra.Command{
Use: "diff-identity <base.json> <head.json>",
Short: "Compare two identity files and report changed components",
Long: `Compare two component identity JSON files (produced by 'component identity -a -O json')
and report which components have changed, been added, or been removed.
CI uses the 'changed' and 'added' lists to determine the build queue.`,
Example: ` # Compare base and head identity files
azldev component diff-identity base-identity.json head-identity.json
# JSON output for CI
azldev component diff-identity base.json head.json -O json`,
Args: cobra.ExactArgs(diffIdentityArgCount),
RunE: azldev.RunFuncWithoutRequiredConfigWithExtraArgs(
func(env *azldev.Env, args []string) (interface{}, error) {
return DiffIdentities(env, args[0], args[1], options.ChangedOnly)
},
),
}
cmd.Flags().BoolVarP(&options.ChangedOnly, "changed-only", "c", false,
"Only show changed and added components (the build queue)")
return cmd
}
// IdentityDiffStatus represents the change status of a component.
type IdentityDiffStatus string
const (
// IdentityDiffChanged indicates the component's fingerprint changed.
IdentityDiffChanged IdentityDiffStatus = "changed"
// IdentityDiffAdded indicates the component is new in the head.
IdentityDiffAdded IdentityDiffStatus = "added"
// IdentityDiffRemoved indicates the component was removed in the head.
IdentityDiffRemoved IdentityDiffStatus = "removed"
// IdentityDiffUnchanged indicates the component's fingerprint is identical.
IdentityDiffUnchanged IdentityDiffStatus = "unchanged"
)
// IdentityDiffResult is the per-component row in table output.
type IdentityDiffResult struct {
Component string `json:"component" table:",sortkey"`
Status IdentityDiffStatus `json:"status"`
}
// IdentityDiffReport is the structured output for JSON format.
type IdentityDiffReport struct {
Changed []string `json:"changed"`
Added []string `json:"added"`
Removed []string `json:"removed"`
Unchanged []string `json:"unchanged"`
}
// DiffIdentities reads two identity JSON files and computes the diff.
func DiffIdentities(env *azldev.Env, basePath string, headPath string, changedOnly bool) (interface{}, error) {
baseIdentities, err := readIdentityFile(env, basePath)
if err != nil {
return nil, fmt.Errorf("reading base identity file %#q:\n%w", basePath, err)
}
headIdentities, err := readIdentityFile(env, headPath)
if err != nil {
return nil, fmt.Errorf("reading head identity file %#q:\n%w", headPath, err)
}
report := ComputeDiff(baseIdentities, headIdentities, changedOnly)
// Return table-friendly results for table/CSV format, or the report for JSON.
if env.DefaultReportFormat() == azldev.ReportFormatJSON {
return report, nil
}
return buildTableResults(report), nil
}
// readIdentityFile reads and parses a component identity JSON file into a map of
// component name to fingerprint.
func readIdentityFile(
env *azldev.Env, filePath string,
) (map[string]string, error) {
data, err := fileutils.ReadFile(env.FS(), filePath)
if err != nil {
return nil, fmt.Errorf("reading file:\n%w", err)
}
var entries []ComponentIdentityResult
err = json.Unmarshal(data, &entries)
if err != nil {
return nil, fmt.Errorf("parsing JSON:\n%w", err)
}
result := make(map[string]string, len(entries))
for _, entry := range entries {
result[entry.Component] = entry.Fingerprint
}
return result, nil
}
// ComputeDiff compares base and head identity maps and produces a diff report.
// When changedOnly is true, the Removed and Unchanged lists are left empty.
func ComputeDiff(base map[string]string, head map[string]string, changedOnly bool) *IdentityDiffReport {
// Initialize all slices so JSON serialization produces [] instead of null.
report := &IdentityDiffReport{
Changed: make([]string, 0),
Added: make([]string, 0),
Removed: make([]string, 0),
Unchanged: make([]string, 0),
}
// Check base components against head.
for name, baseFP := range base {
headFP, exists := head[name]
switch {
case !exists:
if !changedOnly {
report.Removed = append(report.Removed, name)
}
case baseFP != headFP:
report.Changed = append(report.Changed, name)
default:
if !changedOnly {
report.Unchanged = append(report.Unchanged, name)
}
}
}
// Check for new components in head.
for name := range head {
if _, exists := base[name]; !exists {
report.Added = append(report.Added, name)
}
}
// Sort all lists for deterministic output.
sort.Strings(report.Changed)
sort.Strings(report.Added)
sort.Strings(report.Removed)
sort.Strings(report.Unchanged)
return report
}
// buildTableResults converts the diff report into a slice for table output.
func buildTableResults(report *IdentityDiffReport) []IdentityDiffResult {
results := make([]IdentityDiffResult, 0,
len(report.Changed)+len(report.Added)+len(report.Removed)+len(report.Unchanged))
for _, name := range report.Changed {
results = append(results, IdentityDiffResult{Component: name, Status: IdentityDiffChanged})
}
for _, name := range report.Added {
results = append(results, IdentityDiffResult{Component: name, Status: IdentityDiffAdded})
}
for _, name := range report.Removed {
results = append(results, IdentityDiffResult{Component: name, Status: IdentityDiffRemoved})
}
for _, name := range report.Unchanged {
results = append(results, IdentityDiffResult{Component: name, Status: IdentityDiffUnchanged})
}
return results
}