Skip to content

Commit 7079d7f

Browse files
authored
Allow flow cadence lint with empty args (#2212)
1 parent da43284 commit 7079d7f

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

internal/cadence/lint.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package cadence
2020

2121
import (
2222
"fmt"
23+
"os"
24+
"path/filepath"
2325
"strings"
2426

2527
"golang.org/x/exp/slices"
@@ -53,10 +55,14 @@ var lintFlags = lintFlagsCollection{}
5355

5456
var lintCommand = &command.Command{
5557
Cmd: &cobra.Command{
56-
Use: "lint [files]",
57-
Short: "Lint Cadence code to identify potential issues or errors",
58-
Example: "flow cadence lint **/*.cdc",
59-
Args: cobra.MinimumNArgs(1),
58+
Use: "lint [files...]",
59+
Short: "Lint Cadence code to identify potential issues or errors",
60+
Example: `# Lint all .cdc files in the project
61+
flow cadence lint
62+
63+
# Lint specific files
64+
flow cadence lint file1.cdc file2.cdc`,
65+
Args: cobra.ArbitraryArgs,
6066
},
6167
Flags: &lintFlags,
6268
RunS: lint,
@@ -76,7 +82,20 @@ func lint(
7682
flow flowkit.Services,
7783
state *flowkit.State,
7884
) (command.Result, error) {
79-
filePaths := args
85+
var filePaths []string
86+
if len(args) == 0 {
87+
var err error
88+
filePaths, err = findAllCadenceFiles(".")
89+
if err != nil {
90+
return nil, fmt.Errorf("error finding Cadence files: %w", err)
91+
}
92+
if len(filePaths) == 0 {
93+
return nil, fmt.Errorf("no .cdc files found in the project")
94+
}
95+
} else {
96+
filePaths = args
97+
}
98+
8099
result, err := lintFiles(state, filePaths...)
81100
if err != nil {
82101
return nil, err
@@ -236,3 +255,24 @@ func (r *lintResult) Oneliner() string {
236255
func (r *lintResult) ExitCode() int {
237256
return r.exitCode
238257
}
258+
259+
func findAllCadenceFiles(baseDir string) ([]string, error) {
260+
var filenames []string
261+
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
262+
if err != nil {
263+
return err
264+
}
265+
266+
if !strings.HasSuffix(path, ".cdc") {
267+
return nil
268+
}
269+
270+
filenames = append(filenames, path)
271+
return nil
272+
})
273+
if err != nil {
274+
return nil, err
275+
}
276+
277+
return filenames, nil
278+
}

0 commit comments

Comments
 (0)