grafana/scripts/modowners/modowners.go

180 lines
4.5 KiB
Go
Raw Normal View History

Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/fs"
"log"
"strings"
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
"os"
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
"golang.org/x/mod/modfile"
)
type Module struct {
Name string
Owners []string
Indirect bool
}
func parseModule(mod *modfile.Require) Module {
m := Module{Name: mod.Mod.String()}
// For each require, access the comment.
for _, comment := range mod.Syntax.Comments.Suffix {
owners := strings.Fields(comment.Token)
// For each comment, determine if it contains owner(s).
for _, owner := range owners {
if strings.Contains(owner, "indirect") {
m.Indirect = true
}
// If there is an owner, add to owners list.
if strings.Contains(owner, "@") {
m.Owners = append(m.Owners, owner)
}
}
}
return m
}
func parseGoMod(fileSystem fs.FS, name string) ([]Module, error) {
file, err := fileSystem.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
// Turn modfile into array of bytes.
data, err := io.ReadAll(file)
if err != nil {
return nil, err
}
// Parse modfile.
modFile, err := modfile.Parse(name, data, nil)
if err != nil {
return nil, err
}
modules := []Module{}
// Iterate through requires in modfile.
for _, mod := range modFile.Require {
m := parseModule(mod)
modules = append(modules, m)
}
return modules, nil
}
// Validate that each module has an owner.
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
// An example CLI command is `go run scripts/modowners/modowners.go check go.mod`
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
func check(fileSystem fs.FS, logger *log.Logger, args []string) error {
m, err := parseGoMod(fileSystem, args[0])
if err != nil {
return err
}
fail := false
for _, mod := range m {
if !mod.Indirect && len(mod.Owners) == 0 {
logger.Println(mod.Name)
fail = true
}
}
if fail {
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
return errors.New("one or more newly added dependencies do not have an assigned owner - please assign a team as an owner")
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
}
return nil
}
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
// Print owner(s) for a given dependency.
// An example CLI command to get a list of all owners in go.mod with a count of the number of dependencies they own is `go run scripts/modowners/modowners.go owners -a -c go.mod`
// An example CLI command to get the owner for a specific dependency is `go run scripts/modowners/modowners.go owners -d cloud.google.com/go/storage@v1.30.1 go.mod`. You must use `dependency@version`, not `dependency version`.
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
func owners(fileSystem fs.FS, logger *log.Logger, args []string) error {
fs := flag.NewFlagSet("owners", flag.ExitOnError)
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
allOwners := fs.Bool("a", false, "print all owners in specified file")
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
count := fs.Bool("c", false, "print count of dependencies per owner")
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
dep := fs.String("d", "", "name of dependency")
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
fs.Parse(args)
m, err := parseGoMod(fileSystem, fs.Arg(0))
if err != nil {
return err
}
owners := map[string]int{}
for _, mod := range m {
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
if len(*dep) > 0 && mod.Name == *dep {
for _, owner := range mod.Owners {
logger.Println(owner)
break
}
}
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
if mod.Indirect == false {
for _, owner := range mod.Owners {
owners[owner]++
}
}
}
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
if *allOwners {
for owner, n := range owners {
if *count {
logger.Println(owner, n)
} else {
logger.Println(owner)
}
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
}
}
return nil
}
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
// Print dependencies for a given owner. Can specify one or more owners.
// An example CLI command to list all direct dependencies owned by Delivery and Authnz `go run scripts/modowners/modowners.go modules -o @grafana/grafana-release-guild,@grafana/identity-access-team go.mod`
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
func modules(fileSystem fs.FS, logger *log.Logger, args []string) error {
fs := flag.NewFlagSet("modules", flag.ExitOnError)
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
indirect := fs.Bool("i", false, "print indirect dependencies")
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
owner := fs.String("o", "", "one or more owners")
fs.Parse(args)
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
m, err := parseGoMod(fileSystem, fs.Arg(0))
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
if err != nil {
return err
}
ownerFlags := strings.Split(*owner, ",")
for _, mod := range m {
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
if len(*owner) == 0 || hasCommonElement(mod.Owners, ownerFlags) {
if *indirect || !mod.Indirect {
logger.Println(mod.Name)
}
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
}
}
return nil
}
func hasCommonElement(a []string, b []string) bool {
for _, u := range a {
for _, v := range b {
if u == v {
return true
}
}
}
return false
}
func main() {
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
log.SetFlags(0)
log.SetOutput(os.Stdout)
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
if len(os.Args) < 2 {
fmt.Println("usage: modowners subcommand go.mod...")
os.Exit(1)
}
type CmdFunc func(fs.FS, *log.Logger, []string) error
cmds := map[string]CmdFunc{"check": check, "owners": owners, "modules": modules}
tool: generate owners for modules in `go.mod` (#69583) * test: add test for getFiles * fix: fix getFiles test to take in a module name (not modfile name) * move functionality unrelated to getFiles into main func * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * test * Revert "test" This reverts commit 2b519f3725f5709e5ace412f6bd1de4bc9f15e01. * Revert "attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go" This reverts commit eb952474870243110bdc1087c8468981d5eda5b2. * attempt to getFiles with ParseDir, empty map returned; TODO: restore modowners_generation_script_test.go * post-pairing with daniel, can access imports in a file * clean up comments for readability * try to return map of importName: files that import the improt * refactor: change getFiles to accept single import name and return list of files that import it * add log to see importPath and importName * hasImport working * start modowners script & add comments for hasImport * fix modules() and uncomment main * start script to add team names to go.mod, currently can access a map of the import and name * :shit: * calculate root directory to point to correct go.mod * chore: delete unnecessary files * chore: uncomment tests * chore: remove unnecessary comments, update documentation comments with correct cli commands * fix: revert changes in go.mod and go.sum * where is my dependency flag value?? * fix: owners function now can list all owners (with counts) or list a specific owner for a given dependency * fix: change fmt.Println to logger.Println for owners func * partial fix: modules now only prints dependencies owned by given team. -i functionality still not working properly * fix: fix TestModules, modules * chore: update check error message to specify user needs to assign owner to new dependency * fix: adjust punctuation in error string * fix: clean up comments in modowners * chore: remove note in modowners_test
2023-07-18 12:42:09 -05:00
if f, ok := cmds[os.Args[1]]; !ok {
Feat: Add command line app to validate go.mod (#67796) * chore: start modowners * read go.mod, parse modfile, iterate through requires; add dummy go.mod * make BEP owners of all grafana dependencies :scream: * push attempt at logging the require comments * shrink dummy modfile * revert changes in go.mod * access comments suffix * add Module struct; attempt to separate ParseGoMod functionality into its own func; add owner (third) for loop when interating modfile * feat: print all owners in modfile * add additional question in comment * feat: add subcommands: check, owners, modules; chunk out some functions * chunk out subcommand functions * add flags * start tests for common element * refactor: test for common element * attempt #1 to refactor modules to accept multiple args * refactor: refactor modfule func to take 1+ owner arguments (0 arguments not working atm) * chore: remove debug logging * refine existing comments * comment out indirect flag stuff, add example cli command for modules * unsuccessful attempt #2 to refactor modules to accept -o and -i flags * refactor funcs to take filesystem and logger * test: add test for check when all modules have owners * fail attempt 1 to get TestModules to work * assert expected log result in TestModules; unsure if properly reading logs * test: add TestModules to test modules func without any flags returns direct dependencies * test: add TestInvalidCheck for scenario when some dependencies are missing an owner * attempt 1 at refactoring TestCheck into a table * chore: clean TestCheck * chore: clean up comments for func check * move files under scripts/modowners * revert go.mod and go.sum
2023-05-30 10:18:05 -05:00
log.Fatal("invalid command")
} else if err := f(os.DirFS("."), log.Default(), os.Args[2:]); err != nil {
log.Fatal(err)
}
}