Skip to content

Commit 6e8fc77

Browse files
committed
minor style changes in repostructure.go
Signed-off-by: Callum Styan <[email protected]>
1 parent 85743cd commit 6e8fc77

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

cmd/readmevalidation/repostructure.go

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,37 @@ import (
1212
var supportedUserNameSpaceDirectories = append(supportedResourceTypes[:], ".icons", ".images")
1313

1414
func validateCoderResourceSubdirectory(dirPath string) []error {
15-
errs := []error{}
15+
var (
16+
err error
17+
subDir os.FileInfo
18+
)
1619

17-
subDir, err := os.Stat(dirPath)
18-
if err != nil {
19-
// It's valid for a specific resource directory not to exist. It's just
20-
// that if it does exist, it must follow specific rules
20+
if subDir, err = os.Stat(dirPath); err != nil {
21+
// It's valid for a specific resource directory not to exist. It's just that if it does exist, it must follow specific rules.
2122
if !errors.Is(err, os.ErrNotExist) {
22-
errs = append(errs, addFilePathToError(dirPath, err))
23+
return []error{addFilePathToError(dirPath, err)}
2324
}
24-
return errs
2525
}
2626

2727
if !subDir.IsDir() {
28-
errs = append(errs, fmt.Errorf("%q: path is not a directory", dirPath))
29-
return errs
28+
return []error{fmt.Errorf("%q: path is not a directory", dirPath)}
3029
}
3130

3231
files, err := os.ReadDir(dirPath)
3332
if err != nil {
34-
errs = append(errs, addFilePathToError(dirPath, err))
35-
return errs
33+
return []error{addFilePathToError(dirPath, err)}
3634
}
35+
36+
errs := []error{}
3737
for _, f := range files {
38-
// The .coder subdirectories are sometimes generated as part of Bun
39-
// tests. These subdirectories will never be committed to the repo, but
40-
// in the off chance that they don't get cleaned up properly, we want to
41-
// skip over them
38+
// The .coder subdirectories are sometimes generated as part of Bun tests. These subdirectories will never be
39+
// committed to the repo, but in the off chance that they don't get cleaned up properly, we want to skip over them.
4240
if !f.IsDir() || f.Name() == ".coder" {
4341
continue
4442
}
4543

4644
resourceReadmePath := path.Join(dirPath, f.Name(), "README.md")
47-
_, err := os.Stat(resourceReadmePath)
48-
if err != nil {
45+
if _, err = os.Stat(resourceReadmePath); err != nil {
4946
if errors.Is(err, os.ErrNotExist) {
5047
errs = append(errs, fmt.Errorf("%q: 'README.md' does not exist", resourceReadmePath))
5148
} else {
@@ -54,27 +51,28 @@ func validateCoderResourceSubdirectory(dirPath string) []error {
5451
}
5552

5653
mainTerraformPath := path.Join(dirPath, f.Name(), "main.tf")
57-
_, err = os.Stat(mainTerraformPath)
58-
if err != nil {
54+
if _, err = os.Stat(mainTerraformPath); err != nil {
5955
if errors.Is(err, os.ErrNotExist) {
6056
errs = append(errs, fmt.Errorf("%q: 'main.tf' file does not exist", mainTerraformPath))
6157
} else {
6258
errs = append(errs, addFilePathToError(mainTerraformPath, err))
6359
}
6460
}
65-
6661
}
67-
6862
return errs
6963
}
7064

7165
func validateRegistryDirectory() []error {
72-
userDirs, err := os.ReadDir(rootRegistryPath)
73-
if err != nil {
66+
var (
67+
userDirs []os.DirEntry
68+
err error
69+
)
70+
if userDirs, err = os.ReadDir(rootRegistryPath); err != nil {
7471
return []error{err}
7572
}
7673

7774
allErrs := []error{}
75+
var iterFiles []os.DirEntry
7876
for _, d := range userDirs {
7977
dirPath := path.Join(rootRegistryPath, d.Name())
8078
if !d.IsDir() {
@@ -83,20 +81,17 @@ func validateRegistryDirectory() []error {
8381
}
8482

8583
contributorReadmePath := path.Join(dirPath, "README.md")
86-
_, err := os.Stat(contributorReadmePath)
87-
if err != nil {
84+
if _, err = os.Stat(contributorReadmePath); err != nil {
8885
allErrs = append(allErrs, err)
8986
}
9087

91-
files, err := os.ReadDir(dirPath)
92-
if err != nil {
88+
if iterFiles, err = os.ReadDir(dirPath); err != nil {
9389
allErrs = append(allErrs, err)
9490
continue
9591
}
9692

97-
for _, f := range files {
98-
// Todo: Decide if there's anything more formal that we want to
99-
// ensure about non-directories scoped to user namespaces
93+
for _, f := range iterFiles {
94+
// TODO: Decide if there's anything more formal that we want to ensure about non-directories scoped to user namespaces.
10095
if !f.IsDir() {
10196
continue
10297
}
@@ -110,8 +105,7 @@ func validateRegistryDirectory() []error {
110105
}
111106

112107
if slices.Contains(supportedResourceTypes, segment) {
113-
errs := validateCoderResourceSubdirectory(filePath)
114-
if len(errs) != 0 {
108+
if errs := validateCoderResourceSubdirectory(filePath); len(errs) != 0 {
115109
allErrs = append(allErrs, errs...)
116110
}
117111
}
@@ -122,20 +116,19 @@ func validateRegistryDirectory() []error {
122116
}
123117

124118
func validateRepoStructure() error {
125-
var problems []error
126-
if errs := validateRegistryDirectory(); len(errs) != 0 {
127-
problems = append(problems, errs...)
119+
var errs []error
120+
if vrdErrs := validateRegistryDirectory(); len(vrdErrs) != 0 {
121+
errs = append(errs, errs...)
128122
}
129123

130-
_, err := os.Stat("./.icons")
131-
if err != nil {
132-
problems = append(problems, errors.New("missing top-level .icons directory (used for storing reusable Coder resource icons)"))
124+
if _, err := os.Stat("./.icons"); err != nil {
125+
errs = append(errs, errors.New("missing top-level .icons directory (used for storing reusable Coder resource icons)"))
133126
}
134127

135-
if len(problems) != 0 {
128+
if len(errs) != 0 {
136129
return validationPhaseError{
137130
phase: validationPhaseFileStructureValidation,
138-
errors: problems,
131+
errors: errs,
139132
}
140133
}
141134
return nil

0 commit comments

Comments
 (0)