Skip to content

Commit 9cc574d

Browse files
Address review feedback for diff-contract
- Register -q shorthand via sconfig flag tag instead of a duplicate manual flag registration in init() - Add 30s timeout to URL fetch client - Use HexWithPrefix() consistently for address output - Derive exit code from identical instead of storing it; drop duplicated location assignment
1 parent 635da08 commit 9cc574d

2 files changed

Lines changed: 17 additions & 28 deletions

File tree

internal/diffcontract/diff-contract.go

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"net/http"
2626
"strings"
27+
"time"
2728

2829
"github.com/pmezard/go-difflib/difflib"
2930
"github.com/spf13/cobra"
@@ -39,7 +40,7 @@ import (
3940
)
4041

4142
type diffContractFlags struct {
42-
Quiet bool `default:"false" flag:"quiet" info:"Exit with non-zero code if contracts differ, without output"`
43+
Quiet bool `default:"false" flag:"quiet,q" info:"Exit with non-zero code if contracts differ, without output"`
4344
}
4445

4546
var diffFlags = diffContractFlags{}
@@ -56,10 +57,6 @@ var DiffContractCommand = &command.Command{
5657
RunS: diffContract,
5758
}
5859

59-
func init() {
60-
DiffContractCommand.Cmd.Flags().BoolVarP(&diffFlags.Quiet, "quiet", "q", false, "Exit with non-zero code if contracts differ, without output")
61-
}
62-
6360
func diffContract(
6461
args []string,
6562
globalFlags command.GlobalFlags,
@@ -71,25 +68,22 @@ func diffContract(
7168

7269
// Read source code from file or URL
7370
var code []byte
74-
var location string
7571
var err error
7672

7773
if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") {
7874
code, err = fetchURL(source)
7975
if err != nil {
8076
return nil, fmt.Errorf("error fetching contract from URL: %w", err)
8177
}
82-
location = source
8378
} else {
8479
code, err = state.ReadFile(source)
8580
if err != nil {
8681
return nil, fmt.Errorf("error loading contract file: %w", err)
8782
}
88-
location = source
8983
}
9084

9185
// Extract contract name from source
92-
program, err := project.NewProgram(code, nil, location)
86+
program, err := project.NewProgram(code, nil, source)
9387
if err != nil {
9488
return nil, fmt.Errorf("error parsing contract source: %w", err)
9589
}
@@ -103,7 +97,7 @@ func diffContract(
10397
ctx := context.Background()
10498
resolved, err := flow.ReplaceImportsInScript(ctx, flowkit.Script{
10599
Code: code,
106-
Location: location,
100+
Location: source,
107101
})
108102
if err != nil {
109103
return nil, fmt.Errorf("error resolving imports: %w", err)
@@ -124,7 +118,7 @@ func diffContract(
124118
}
125119

126120
// Fetch deployed contract
127-
logger.StartProgress(fmt.Sprintf("Fetching contract '%s' from %s...", contractName, address))
121+
logger.StartProgress(fmt.Sprintf("Fetching contract '%s' from %s...", contractName, address.HexWithPrefix()))
128122
defer logger.StopProgress()
129123

130124
account, err := flow.GetAccount(ctx, address)
@@ -134,7 +128,7 @@ func diffContract(
134128

135129
deployedCode, ok := account.Contracts[contractName]
136130
if !ok {
137-
return nil, fmt.Errorf("contract '%s' not found on account %s", contractName, address)
131+
return nil, fmt.Errorf("contract '%s' not found on account %s", contractName, address.HexWithPrefix())
138132
}
139133

140134
// Normalize and diff
@@ -143,20 +137,13 @@ func diffContract(
143137

144138
identical := localCode == remoteCode
145139

146-
exitCode := 0
147-
if !identical {
148-
exitCode = 1
149-
}
150-
151140
diffText := ""
152141
if !identical {
153-
localLabel := source
154-
remoteLabel := fmt.Sprintf("0x%s/%s (deployed)", address, contractName)
155142
diff := difflib.UnifiedDiff{
156143
A: difflib.SplitLines(remoteCode),
157144
B: difflib.SplitLines(localCode),
158-
FromFile: remoteLabel,
159-
ToFile: localLabel,
145+
FromFile: fmt.Sprintf("%s/%s (deployed)", address.HexWithPrefix(), contractName),
146+
ToFile: source,
160147
Context: 3,
161148
}
162149
diffText, err = difflib.GetUnifiedDiffString(diff)
@@ -168,10 +155,9 @@ func diffContract(
168155
return &diffContractResult{
169156
diff: diffText,
170157
contractName: contractName,
171-
address: address.String(),
158+
address: address.HexWithPrefix(),
172159
identical: identical,
173160
quiet: diffFlags.Quiet,
174-
exitCode: exitCode,
175161
}, nil
176162
}
177163

@@ -204,7 +190,8 @@ func resolveAddressFromConfig(state *flowkit.State, contractName string, network
204190
}
205191

206192
func fetchURL(url string) ([]byte, error) {
207-
resp, err := http.Get(url) //nolint:gosec
193+
client := http.Client{Timeout: 30 * time.Second}
194+
resp, err := client.Get(url)
208195
if err != nil {
209196
return nil, err
210197
}
@@ -224,7 +211,6 @@ type diffContractResult struct {
224211
address string
225212
identical bool
226213
quiet bool
227-
exitCode int
228214
}
229215

230216
var _ command.ResultWithExitCode = &diffContractResult{}
@@ -234,7 +220,7 @@ func (r *diffContractResult) String() string {
234220
return ""
235221
}
236222
if r.identical {
237-
return fmt.Sprintf("Contract '%s' on 0x%s is up to date", r.contractName, r.address)
223+
return fmt.Sprintf("Contract '%s' on %s is up to date", r.contractName, r.address)
238224
}
239225
return r.diff
240226
}
@@ -259,5 +245,8 @@ func (r *diffContractResult) JSON() any {
259245
}
260246

261247
func (r *diffContractResult) ExitCode() int {
262-
return r.exitCode
248+
if r.identical {
249+
return 0
250+
}
251+
return 1
263252
}

internal/diffcontract/diff_contract_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func Test_DiffContract(t *testing.T) {
198198
)
199199

200200
assert.Nil(t, result)
201-
assert.EqualError(t, err, "contract 'TestContract' not found on account f8d6e0586b0a20c7")
201+
assert.EqualError(t, err, "contract 'TestContract' not found on account 0xf8d6e0586b0a20c7")
202202
})
203203

204204
t.Run("Non-existing file", func(t *testing.T) {

0 commit comments

Comments
 (0)