github.com/blend/go-sdk@v1.20220411.3/cmd/codeowners/main.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package main 9 10 import ( 11 "context" 12 "flag" 13 "fmt" 14 "os" 15 16 "github.com/blend/go-sdk/ansi" 17 "github.com/blend/go-sdk/codeowners" 18 ) 19 20 var ( 21 flagPath string 22 flagGithubURL string 23 flagGithubToken string 24 25 flagValidate bool 26 flagGenerate bool 27 28 flagQuiet bool 29 flagVerbose bool 30 flagDebug bool 31 ) 32 33 func init() { 34 flag.StringVar(&flagPath, "path", codeowners.DefaultPath, "The codeowners file path") 35 flag.StringVar(&flagGithubURL, "github-url", codeowners.DefaultGithubURL, "The github api url") 36 flag.StringVar(&flagGithubToken, "github-token", os.Getenv(codeowners.DefaultGithubTokenEnvVar), "The github api token") 37 38 flag.BoolVar(&flagQuiet, "quiet", false, "If all output should be suppressed") 39 flag.BoolVar(&flagVerbose, "verbose", false, "If verbose output should be shown") 40 flag.BoolVar(&flagDebug, "debug", false, "If debug output should be shown") 41 42 flag.BoolVar(&flagValidate, "validate", false, "If we should validate the codeowners file (exclusive with -generate) (this is the default)") 43 flag.BoolVar(&flagGenerate, "generate", false, "If we should generate the codeowners file (exclusive with -validate)") 44 45 oldUsage := flag.Usage 46 flag.Usage = func() { 47 fmt.Fprint(flag.CommandLine.Output(), `github codeowners management cli 48 49 Verify or generate github codeowners files. 50 51 By default, this tool verifies that the codeowners file is present, and that it 52 contains valid user and team references. 53 54 To generate the codeowners file: 55 56 > codeowners --generate 57 58 This will scan the file tree for '.codeowners' file that relatively nominate 59 owners for a given directory and child directories. 60 61 You can also annotate files with the //github:codeowner source comment. 62 `, 63 ) 64 oldUsage() 65 } 66 67 flag.Parse() 68 } 69 70 func main() { 71 ctx := context.Background() 72 73 engine := codeowners.Codeowners{ 74 Config: codeowners.Config{ 75 Path: flagPath, 76 GithubURL: flagGithubURL, 77 GithubToken: flagGithubToken, 78 Quiet: &flagQuiet, 79 Verbose: &flagVerbose, 80 Debug: &flagDebug, 81 }, 82 } 83 84 var actionLabel string 85 var err error 86 if flagGenerate { 87 actionLabel = "generate" 88 var root string 89 if args := flag.Args(); len(args) > 0 { 90 root = args[0] 91 } else { 92 root = "." 93 } 94 err = engine.GenerateFile(ctx, root) 95 } else if flagValidate { 96 actionLabel = "validate" 97 err = engine.ValidateFile(ctx) 98 } else { // the default 99 actionLabel = "validate" 100 err = engine.ValidateFile(ctx) 101 } 102 103 if err != nil { 104 if !flagQuiet { 105 fmt.Fprintf(os.Stderr, "%+v\n", err) 106 fmt.Printf("codeowners %s %s!\n", actionLabel, ansi.Red("failed")) 107 } 108 os.Exit(1) 109 } 110 if !flagQuiet { 111 fmt.Printf("codeowners %s %s!\n", actionLabel, ansi.Green("ok")) 112 } 113 }