Skip to content

Commit 1a5c102

Browse files
committed
refactor: more domain splitting
1 parent 36d3389 commit 1a5c102

File tree

3 files changed

+144
-32
lines changed

3 files changed

+144
-32
lines changed

cmd/readmevalidation/contributors.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"log"
67
"net/url"
78
"os"
89
"path"
@@ -12,9 +13,7 @@ import (
1213
"gopkg.in/yaml.v3"
1314
)
1415

15-
var (
16-
validContributorStatuses = []string{"official", "partner", "community"}
17-
)
16+
var validContributorStatuses = []string{"official", "partner", "community"}
1817

1918
type contributorProfileFrontmatter struct {
2019
DisplayName string `yaml:"display_name"`
@@ -48,10 +47,7 @@ func validateContributorGithubUsername(githubUsername string) error {
4847
return nil
4948
}
5049

51-
func validateContributorEmployerGithubUsername(
52-
employerGithubUsername *string,
53-
githubUsername string,
54-
) []error {
50+
func validateContributorEmployerGithubUsername(employerGithubUsername *string, githubUsername string) []error {
5551
if employerGithubUsername == nil {
5652
return nil
5753
}
@@ -337,9 +333,7 @@ func aggregateContributorReadmeFiles() ([]readme, error) {
337333
return allReadmeFiles, nil
338334
}
339335

340-
func validateContributorRelativeUrls(
341-
contributors map[string]contributorProfile,
342-
) error {
336+
func validateContributorRelativeUrls(contributors map[string]contributorProfile) error {
343337
// This function only validates relative avatar URLs for now, but it can be
344338
// beefed up to validate more in the future
345339
problems := []error{}
@@ -376,3 +370,26 @@ func validateContributorRelativeUrls(
376370
errors: problems,
377371
}
378372
}
373+
374+
func validateAllContributorFiles() error {
375+
allReadmeFiles, err := aggregateContributorReadmeFiles()
376+
if err != nil {
377+
return err
378+
}
379+
380+
log.Printf("Processing %d README files\n", len(allReadmeFiles))
381+
contributors, err := parseContributorFiles(allReadmeFiles)
382+
if err != nil {
383+
return err
384+
}
385+
log.Printf("Processed %d README files as valid contributor profiles", len(contributors))
386+
387+
err = validateContributorRelativeUrls(contributors)
388+
if err != nil {
389+
return err
390+
}
391+
log.Println("All relative URLs for READMEs are valid")
392+
393+
log.Printf("Processed all READMEs in the %q directory\n", rootRegistryPath)
394+
return nil
395+
}

cmd/readmevalidation/main.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,8 @@ import (
1111

1212
func main() {
1313
log.Println("Starting README validation")
14-
allReadmeFiles, err := aggregateContributorReadmeFiles()
14+
err := validateAllContributorFiles()
1515
if err != nil {
1616
log.Panic(err)
1717
}
18-
19-
log.Printf("Processing %d README files\n", len(allReadmeFiles))
20-
contributors, err := parseContributorFiles(allReadmeFiles)
21-
log.Printf(
22-
"Processed %d README files as valid contributor profiles",
23-
len(contributors),
24-
)
25-
if err != nil {
26-
log.Panic(err)
27-
}
28-
29-
err = validateContributorRelativeUrls(contributors)
30-
if err != nil {
31-
log.Panic(err)
32-
}
33-
log.Println("All relative URLs for READMEs are valid")
34-
35-
log.Printf(
36-
"Processed all READMEs in the %q directory\n",
37-
rootRegistryPath,
38-
)
3918
}

cmd/readmevalidation/repoStructure.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"path"
8+
)
9+
10+
var supportedResourceTypes = []string{"modules", "templates"}
11+
12+
func validateCoderResourceSubdirectory(dirPath string) []error {
13+
errs := []error{}
14+
15+
dir, err := os.Stat(dirPath)
16+
if err != nil {
17+
// It's valid for a specific resource directory not to exist. It's just
18+
// that if it does exist, it must follow specific rules
19+
if !errors.Is(err, os.ErrNotExist) {
20+
errs = append(errs, addFilePathToError(dirPath, err))
21+
}
22+
return errs
23+
}
24+
25+
if !dir.IsDir() {
26+
errs = append(errs, fmt.Errorf("%q: path is not a directory", dirPath))
27+
return errs
28+
}
29+
30+
files, err := os.ReadDir(dirPath)
31+
if err != nil {
32+
errs = append(errs, fmt.Errorf("%q: %v", dirPath, err))
33+
return errs
34+
}
35+
for _, f := range files {
36+
if !f.IsDir() {
37+
continue
38+
}
39+
40+
resourceReadmePath := path.Join(dirPath, f.Name(), "README.md")
41+
_, err := os.Stat(resourceReadmePath)
42+
if err != nil {
43+
if errors.Is(err, os.ErrNotExist) {
44+
errs = append(errs, fmt.Errorf("%q: 'README.md' does not exist", resourceReadmePath))
45+
} else {
46+
errs = append(errs, addFilePathToError(resourceReadmePath, err))
47+
}
48+
}
49+
50+
mainTerraformPath := path.Join(dirPath, f.Name(), "main.tf")
51+
_, err = os.Stat(mainTerraformPath)
52+
if err != nil {
53+
if errors.Is(err, os.ErrNotExist) {
54+
errs = append(errs, fmt.Errorf("%q: 'main.tf' file does not exist", mainTerraformPath))
55+
} else {
56+
errs = append(errs, addFilePathToError(mainTerraformPath, err))
57+
}
58+
}
59+
60+
}
61+
62+
return errs
63+
}
64+
65+
func validateRegistryDirectory() []error {
66+
dirEntries, err := os.ReadDir(rootRegistryPath)
67+
if err != nil {
68+
return []error{err}
69+
}
70+
71+
problems := []error{}
72+
for _, e := range dirEntries {
73+
dirPath := path.Join(rootRegistryPath, e.Name())
74+
if !e.IsDir() {
75+
problems = append(problems, fmt.Errorf("detected non-directory file %q at base of main Registry directory", dirPath))
76+
continue
77+
}
78+
79+
readmePath := path.Join(dirPath, "README.md")
80+
_, err := os.Stat(readmePath)
81+
if err != nil {
82+
problems = append(problems, err)
83+
}
84+
85+
for _, rType := range supportedResourceTypes {
86+
resourcePath := path.Join(dirPath, rType)
87+
if errs := validateCoderResourceSubdirectory(resourcePath); len(errs) != 0 {
88+
problems = append(problems, errs...)
89+
}
90+
}
91+
}
92+
93+
return problems
94+
}
95+
96+
func validateRepoStructure() error {
97+
var problems []error
98+
if errs := validateRegistryDirectory(); len(errs) != 0 {
99+
problems = append(problems, errs...)
100+
}
101+
102+
_, err := os.Stat("./.icons")
103+
if err != nil {
104+
problems = append(problems, err)
105+
}
106+
107+
// Todo: figure out what other directories we want to make guarantees for
108+
// and add them to this function
109+
if len(problems) != 0 {
110+
return validationPhaseError{
111+
phase: validationPhaseFileStructureValidation,
112+
errors: problems,
113+
}
114+
}
115+
return nil
116+
}

0 commit comments

Comments
 (0)