From 0865c3357af5a01e14638ccd0e1ac539f8066e82 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 01:52:52 +0900 Subject: [PATCH 1/9] Do not use init func --- cmd/completion.go | 48 ++++++++++----------- cmd/delete.go | 4 -- cmd/gc.go | 20 ++++----- cmd/list.go | 4 -- cmd/pull.go | 4 -- cmd/push.go | 4 -- cmd/root.go | 105 +++++++++++++++++++++++++--------------------- cmd/show.go | 4 -- main.go | 5 ++- 9 files changed, 92 insertions(+), 106 deletions(-) diff --git a/cmd/completion.go b/cmd/completion.go index 50ff4db..83c253f 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -58,14 +58,11 @@ __git-ghost_custom_func() { ` ) -func init() { - RootCmd.AddCommand(completionCmd) -} - -var completionCmd = &cobra.Command{ - Use: "completion SHELL", - Short: "output shell completion code for the specified shell (bash or zsh)", - Long: `Write bash or zsh shell completion code to standard output. +func NewCompletionCommand(rootCmd *cobra.Command) *cobra.Command { + return &cobra.Command{ + Use: "completion SHELL", + Short: "output shell completion code for the specified shell (bash or zsh)", + Long: `Write bash or zsh shell completion code to standard output. For bash, ensure you have bash completions installed and enabled. To access completions in your current shell, run @@ -75,21 +72,22 @@ var completionCmd = &cobra.Command{ For zsh, output to a file in a directory referenced by the $fpath shell variable. `, - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - shell := args[0] - RootCmd.BashCompletionFunction = bashCompletionFunc - availableCompletions := map[string]func(io.Writer) error{ - "bash": RootCmd.GenBashCompletion, - "zsh": RootCmd.GenZshCompletion, - } - completion, ok := availableCompletions[shell] - if !ok { - fmt.Printf("Invalid shell '%s'. The supported shells are bash and zsh.\n", shell) - os.Exit(1) - } - if err := completion(os.Stdout); err != nil { - log.Fatal(err) - } - }, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + shell := args[0] + rootCmd.BashCompletionFunction = bashCompletionFunc + availableCompletions := map[string]func(io.Writer) error{ + "bash": rootCmd.GenBashCompletion, + "zsh": rootCmd.GenZshCompletion, + } + completion, ok := availableCompletions[shell] + if !ok { + fmt.Printf("Invalid shell '%s'. The supported shells are bash and zsh.\n", shell) + os.Exit(1) + } + if err := completion(os.Stdout); err != nil { + log.Fatal(err) + } + }, + } } diff --git a/cmd/delete.go b/cmd/delete.go index b25618c..55529b8 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -25,10 +25,6 @@ import ( "github.com/spf13/cobra" ) -func init() { - RootCmd.AddCommand(NewDeleteCommand()) -} - type deleteFlags struct { hashFrom string hashTo string diff --git a/cmd/gc.go b/cmd/gc.go index 4086d26..2442f21 100644 --- a/cmd/gc.go +++ b/cmd/gc.go @@ -20,15 +20,13 @@ import ( "github.com/spf13/cobra" ) -func init() { - RootCmd.AddCommand(gcCmd) -} - -var gcCmd = &cobra.Command{ - Use: "gc", - Short: "gc ghost commits from remote repository.", - Long: "gc ghost commits from remote repository.", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("gc command") - }, +func NewGCCommand() *cobra.Command { + return &cobra.Command{ + Use: "gc", + Short: "gc ghost commits from remote repository.", + Long: "gc ghost commits from remote repository.", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("gc command") + }, + } } diff --git a/cmd/list.go b/cmd/list.go index dfe0957..fee3391 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -27,10 +27,6 @@ import ( "github.com/spf13/cobra" ) -func init() { - RootCmd.AddCommand(NewListCommand()) -} - var outputTypes = []string{"only-from", "only-to"} var regexpOutputPattern = regexp.MustCompile("^(|" + strings.Join(outputTypes, "|") + ")$") diff --git a/cmd/pull.go b/cmd/pull.go index 0d0ef31..f8774fb 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -25,10 +25,6 @@ import ( "github.com/spf13/cobra" ) -func init() { - RootCmd.AddCommand(NewPullCommand()) -} - type pullFlags struct { // forceApply bool } diff --git a/cmd/push.go b/cmd/push.go index 32ceab3..bc56604 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -30,10 +30,6 @@ type pushFlags struct { followSymlinks bool } -func init() { - RootCmd.AddCommand(NewPushCommand()) -} - func NewPushCommand() *cobra.Command { var ( flags pushFlags diff --git a/cmd/root.go b/cmd/root.go index 9c10279..64c8279 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -55,59 +55,68 @@ var ( Revision string ) -var RootCmd = &cobra.Command{ - Use: "git-ghost", - Short: "git-ghost", - SilenceErrors: false, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if cmd.Use == "version" { - return nil - } - err := validateEnvironment() - if err != nil { - return err - } - err = globalOpts.SetDefaults() - if err != nil { - return err - } - err = globalOpts.Validate() - if err != nil { - return err - } - switch globalOpts.verbose { - case 0: - log.SetLevel(log.ErrorLevel) - case 1: - log.SetLevel(log.InfoLevel) - case 2: - log.SetLevel(log.DebugLevel) - case 3: - log.SetLevel(log.TraceLevel) - } - return nil - }, -} - var globalOpts globalFlags -func init() { +func NewRootCmd() *cobra.Command { cobra.OnInitialize() - RootCmd.PersistentFlags().StringVar(&globalOpts.srcDir, "src-dir", "", "source directory which you create ghost from (default to PWD env)") - RootCmd.PersistentFlags().StringVar(&globalOpts.ghostWorkDir, "ghost-working-dir", "", "local root directory for git-ghost interacting with ghost repository (default to a temporary directory)") - RootCmd.PersistentFlags().StringVar(&globalOpts.ghostPrefix, "ghost-prefix", "", "prefix of ghost branch name (default to GIT_GHOST_PREFIX env, or ghost)") - RootCmd.PersistentFlags().StringVar(&globalOpts.ghostRepo, "ghost-repo", "", "git remote url for ghosts repository (default to GIT_GHOST_REPO env)") - RootCmd.PersistentFlags().CountVarP(&globalOpts.verbose, "verbose", "v", "verbose mode") - RootCmd.AddCommand(versionCmd) + rootCmd := &cobra.Command{ + Use: "git-ghost", + Short: "git-ghost", + SilenceErrors: false, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if cmd.Use == "version" { + return nil + } + err := validateEnvironment() + if err != nil { + return err + } + err = globalOpts.SetDefaults() + if err != nil { + return err + } + err = globalOpts.Validate() + if err != nil { + return err + } + switch globalOpts.verbose { + case 0: + log.SetLevel(log.ErrorLevel) + case 1: + log.SetLevel(log.InfoLevel) + case 2: + log.SetLevel(log.DebugLevel) + case 3: + log.SetLevel(log.TraceLevel) + } + return nil + }, + } + rootCmd.PersistentFlags().StringVar(&globalOpts.srcDir, "src-dir", "", "source directory which you create ghost from (default to PWD env)") + rootCmd.PersistentFlags().StringVar(&globalOpts.ghostWorkDir, "ghost-working-dir", "", "local root directory for git-ghost interacting with ghost repository (default to a temporary directory)") + rootCmd.PersistentFlags().StringVar(&globalOpts.ghostPrefix, "ghost-prefix", "", "prefix of ghost branch name (default to GIT_GHOST_PREFIX env, or ghost)") + rootCmd.PersistentFlags().StringVar(&globalOpts.ghostRepo, "ghost-repo", "", "git remote url for ghosts repository (default to GIT_GHOST_REPO env)") + rootCmd.PersistentFlags().CountVarP(&globalOpts.verbose, "verbose", "v", "verbose mode") + rootCmd.AddCommand(NewPushCommand()) + rootCmd.AddCommand(NewPullCommand()) + rootCmd.AddCommand(NewShowCommand()) + rootCmd.AddCommand(NewListCommand()) + rootCmd.AddCommand(NewDeleteCommand()) + rootCmd.AddCommand(NewGCCommand()) + rootCmd.AddCommand(NewVersionCommand()) + rootCmd.AddCommand(NewCompletionCommand(rootCmd)) + return rootCmd } -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Print the version number of git-ghost", - Long: `Print the version number of git-ghost`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("git-ghost %s (revision: %s)", Version, Revision) - }, +func NewVersionCommand() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Print the version number of git-ghost", + Long: `Print the version number of git-ghost`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("git-ghost %s (revision: %s)", Version, Revision) + }, + } } func validateEnvironment() errors.GitGhostError { diff --git a/cmd/show.go b/cmd/show.go index cf1c32e..bc390bc 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -25,10 +25,6 @@ import ( "github.com/spf13/cobra" ) -func init() { - RootCmd.AddCommand(NewShowCommand()) -} - func NewShowCommand() *cobra.Command { command := &cobra.Command{ Use: "show [from-hash(default=HEAD)] [diff-hash]", diff --git a/main.go b/main.go index 7ae3c83..854f604 100644 --- a/main.go +++ b/main.go @@ -27,8 +27,9 @@ func init() { } func main() { - // RootCmd prints errors if exists - if err := cmd.RootCmd.Execute(); err != nil { + // rootCmd prints errors if exists + rootCmd := cmd.NewRootCmd() + if err := rootCmd.Execute(); err != nil { os.Exit(-1) } } From 01a67cf204d41d47a3f83a798c093db95ce89d12 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 01:52:57 +0900 Subject: [PATCH 2/9] Remove unnecessary lint exclustion --- golangci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/golangci.yml b/golangci.yml index 61b800c..55e5555 100644 --- a/golangci.yml +++ b/golangci.yml @@ -1,7 +1,5 @@ run: tests: false - skip-files: - - "pkg/.*_k8s.go$" output: format: colored-line-number print-issued-lines: true From c22ea2d8a3d34ef86e09255ac416c6fd9cb5f632 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 13:10:13 +0900 Subject: [PATCH 3/9] Check license in CI --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 304097d..5f369dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ install: - go get -u github.com/mattn/goveralls - dep ensure -before_script: make lint +before_script: make check-license lint script: - make test coverage From 9e8d2e7e9c8f4565b31c85f8a8893a574e9a908c Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 13:15:34 +0900 Subject: [PATCH 4/9] Add cmd test --- cmd/root_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 cmd/root_test.go diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..86e6981 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,12 @@ +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRootCmd(t *testing.T) { + cmd := NewRootCmd() + assert.NotNil(t, cmd) +} From ac81e8622fe7f06d07d7bfd38ec1b92ae16d895e Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 13:54:59 +0900 Subject: [PATCH 5/9] Update license scripts --- scripts/license/add.py | 16 ++++++++-------- scripts/license/check.py | 26 ++++++++++++-------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/scripts/license/add.py b/scripts/license/add.py index da9e24c..78f1f12 100644 --- a/scripts/license/add.py +++ b/scripts/license/add.py @@ -16,22 +16,22 @@ Adds a license header to files. """ +import os from pathlib import Path import subprocess from license_header import license_header, has_license_header -PROJECT_ROOT = Path(subprocess.check_output( - "git rev-parse --show-toplevel".split()).strip().decode("utf-8")) +PROJECT_ROOT = subprocess.check_output( + "git rev-parse --show-toplevel".split()).strip().decode("utf-8") +target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) if d != "vendor"] def main(verbose=False): - add(PROJECT_ROOT.glob("*[!vendor]/**/*_k8s.go"), - license_header("//", True), verbose) - add([p for p in PROJECT_ROOT.glob("*[!vendor]/**/*.go") - if p.name[-7:] != "_k8s.go"], license_header("//"), verbose) - add(PROJECT_ROOT.glob("*[!vendor]/**/*.py"), license_header("#"), verbose) - add(PROJECT_ROOT.glob("*[!vendor]/**/*.sh"), license_header("#"), verbose) + for d in target_dirs: + add(Path(d).glob("./**/*.go"), license_header("//"), verbose) + add(Path(d).glob("./**/*.py"), license_header("#"), verbose) + add(Path(d).glob("./**/*.sh"), license_header("#"), verbose) def add(paths, license_header, verbose): diff --git a/scripts/license/check.py b/scripts/license/check.py index 24deadb..ab18efc 100644 --- a/scripts/license/check.py +++ b/scripts/license/check.py @@ -16,29 +16,27 @@ Checks whether files have an appropriate license header. """ -from pathlib import Path +import os import subprocess +from pathlib import Path from license_header import license_header, has_license_header -PROJECT_ROOT = Path(subprocess.check_output( - "git rev-parse --show-toplevel".split()).strip().decode("utf-8")) +PROJECT_ROOT = subprocess.check_output( + "git rev-parse --show-toplevel".split()).strip().decode("utf-8") +target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) if d != "vendor"] def main(verbose=False): ok = True - ok &= check( - PROJECT_ROOT.glob("./*[!vendor]/**/*_k8s.go"), - license_header("//", modification=True), verbose) - ok &= check( - [p for p in PROJECT_ROOT.glob( - "./*[!vendor]/**/*.go") if p.name[-7:] != "_k8s.go"], - license_header("//"), verbose) - ok &= check( - PROJECT_ROOT.glob("./*[!vendor]/**/*.py"), license_header("#"), verbose) - ok &= check( - PROJECT_ROOT.glob("./*[!vendor]/**/*.sh"), license_header("#"), verbose) + for d in target_dirs: + ok &= check( + Path(d).glob("./**/*.go"), license_header("//"), verbose) + ok &= check( + Path(d).glob("./**/*.py"), license_header("#"), verbose) + ok &= check( + Path(d).glob("./**/*.sh"), license_header("#"), verbose) return 0 if ok else 1 From 97eab9715508c3e51aa77d45dc8da65e8a57f027 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 13:55:19 +0900 Subject: [PATCH 6/9] Add license header --- cmd/root_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/root_test.go b/cmd/root_test.go index 86e6981..17dbca8 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1,3 +1,17 @@ +// Copyright 2019 Preferred Networks, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package cmd import ( From ba87016d42782254aed783d528e2b7bc1cdaa463 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 13:57:44 +0900 Subject: [PATCH 7/9] Update license scripts --- scripts/license/add.py | 6 +++--- scripts/license/check.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/license/add.py b/scripts/license/add.py index 78f1f12..c95957c 100644 --- a/scripts/license/add.py +++ b/scripts/license/add.py @@ -29,9 +29,9 @@ def main(verbose=False): for d in target_dirs: - add(Path(d).glob("./**/*.go"), license_header("//"), verbose) - add(Path(d).glob("./**/*.py"), license_header("#"), verbose) - add(Path(d).glob("./**/*.sh"), license_header("#"), verbose) + add(Path(d).glob("**/*.go"), license_header("//"), verbose) + add(Path(d).glob("**/*.py"), license_header("#"), verbose) + add(Path(d).glob("**/*.sh"), license_header("#"), verbose) def add(paths, license_header, verbose): diff --git a/scripts/license/check.py b/scripts/license/check.py index ab18efc..e71219e 100644 --- a/scripts/license/check.py +++ b/scripts/license/check.py @@ -32,11 +32,11 @@ def main(verbose=False): for d in target_dirs: ok &= check( - Path(d).glob("./**/*.go"), license_header("//"), verbose) + Path(d).glob("**/*.go"), license_header("//"), verbose) ok &= check( - Path(d).glob("./**/*.py"), license_header("#"), verbose) + Path(d).glob("**/*.py"), license_header("#"), verbose) ok &= check( - Path(d).glob("./**/*.sh"), license_header("#"), verbose) + Path(d).glob("**/*.sh"), license_header("#"), verbose) return 0 if ok else 1 From a7001e315cec1339ae4668529bc236fba8535cfa Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 14:31:52 +0900 Subject: [PATCH 8/9] Include files in the root directory for license check --- scripts/license/add.py | 7 ++++++- scripts/license/check.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts/license/add.py b/scripts/license/add.py index c95957c..ae9415b 100644 --- a/scripts/license/add.py +++ b/scripts/license/add.py @@ -24,10 +24,15 @@ PROJECT_ROOT = subprocess.check_output( "git rev-parse --show-toplevel".split()).strip().decode("utf-8") -target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) if d != "vendor"] +target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) + if d != "vendor" and os.path.isdir(os.path.join(PROJECT_ROOT, d))] def main(verbose=False): + add(Path(PROJECT_ROOT).glob("*.go"), license_header("//"), verbose) + add(Path(PROJECT_ROOT).glob("*.py"), license_header("#"), verbose) + add(Path(PROJECT_ROOT).glob("*.sh"), license_header("#"), verbose) + for d in target_dirs: add(Path(d).glob("**/*.go"), license_header("//"), verbose) add(Path(d).glob("**/*.py"), license_header("#"), verbose) diff --git a/scripts/license/check.py b/scripts/license/check.py index e71219e..fc20472 100644 --- a/scripts/license/check.py +++ b/scripts/license/check.py @@ -24,12 +24,17 @@ PROJECT_ROOT = subprocess.check_output( "git rev-parse --show-toplevel".split()).strip().decode("utf-8") -target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) if d != "vendor"] +target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) + if d != "vendor" and os.path.isdir(os.path.join(PROJECT_ROOT, d))] def main(verbose=False): ok = True + ok &= check(Path(PROJECT_ROOT).glob("*.go"), license_header("//"), verbose) + ok &= check(Path(PROJECT_ROOT).glob("*.py"), license_header("#"), verbose) + ok &= check(Path(PROJECT_ROOT).glob("*.sh"), license_header("#"), verbose) + for d in target_dirs: ok &= check( Path(d).glob("**/*.go"), license_header("//"), verbose) From c189e5ab8fb7319916ebdb0c910c5333e32ea77e Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sat, 13 Apr 2019 22:44:36 +0900 Subject: [PATCH 9/9] Refactor license scripts --- scripts/license/add.py | 25 ++++++++++++------------- scripts/license/check.py | 23 +++++++++++------------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/scripts/license/add.py b/scripts/license/add.py index ae9415b..63cdaa1 100644 --- a/scripts/license/add.py +++ b/scripts/license/add.py @@ -16,27 +16,26 @@ Adds a license header to files. """ -import os from pathlib import Path import subprocess from license_header import license_header, has_license_header -PROJECT_ROOT = subprocess.check_output( - "git rev-parse --show-toplevel".split()).strip().decode("utf-8") -target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) - if d != "vendor" and os.path.isdir(os.path.join(PROJECT_ROOT, d))] +PROJECT_ROOT = Path(subprocess.check_output( + "git rev-parse --show-toplevel".split()).strip().decode("utf-8")) def main(verbose=False): - add(Path(PROJECT_ROOT).glob("*.go"), license_header("//"), verbose) - add(Path(PROJECT_ROOT).glob("*.py"), license_header("#"), verbose) - add(Path(PROJECT_ROOT).glob("*.sh"), license_header("#"), verbose) - - for d in target_dirs: - add(Path(d).glob("**/*.go"), license_header("//"), verbose) - add(Path(d).glob("**/*.py"), license_header("#"), verbose) - add(Path(d).glob("**/*.sh"), license_header("#"), verbose) + add(PROJECT_ROOT.glob("*.go"), license_header("//"), verbose) + add(PROJECT_ROOT.glob("*.py"), license_header("#"), verbose) + add(PROJECT_ROOT.glob("*.sh"), license_header("#"), verbose) + + for p in PROJECT_ROOT.glob("*"): + if p.name == "vendor" or not p.is_dir(): + continue + add(p.glob("**/*.go"), license_header("//"), verbose) + add(p.glob("**/*.py"), license_header("#"), verbose) + add(p.glob("**/*.sh"), license_header("#"), verbose) def add(paths, license_header, verbose): diff --git a/scripts/license/check.py b/scripts/license/check.py index fc20472..4ebecf4 100644 --- a/scripts/license/check.py +++ b/scripts/license/check.py @@ -16,32 +16,31 @@ Checks whether files have an appropriate license header. """ -import os import subprocess from pathlib import Path from license_header import license_header, has_license_header -PROJECT_ROOT = subprocess.check_output( - "git rev-parse --show-toplevel".split()).strip().decode("utf-8") -target_dirs = [os.path.join(PROJECT_ROOT, d) for d in os.listdir(PROJECT_ROOT) - if d != "vendor" and os.path.isdir(os.path.join(PROJECT_ROOT, d))] +PROJECT_ROOT = Path(subprocess.check_output( + "git rev-parse --show-toplevel".split()).strip().decode("utf-8")) def main(verbose=False): ok = True - ok &= check(Path(PROJECT_ROOT).glob("*.go"), license_header("//"), verbose) - ok &= check(Path(PROJECT_ROOT).glob("*.py"), license_header("#"), verbose) - ok &= check(Path(PROJECT_ROOT).glob("*.sh"), license_header("#"), verbose) + ok &= check(PROJECT_ROOT.glob("*.go"), license_header("//"), verbose) + ok &= check(PROJECT_ROOT.glob("*.py"), license_header("#"), verbose) + ok &= check(PROJECT_ROOT.glob("*.sh"), license_header("#"), verbose) - for d in target_dirs: + for p in PROJECT_ROOT.glob("*"): + if p.name == "vendor" or not p.is_dir(): + continue ok &= check( - Path(d).glob("**/*.go"), license_header("//"), verbose) + p.glob("**/*.go"), license_header("//"), verbose) ok &= check( - Path(d).glob("**/*.py"), license_header("#"), verbose) + p.glob("**/*.py"), license_header("#"), verbose) ok &= check( - Path(d).glob("**/*.sh"), license_header("#"), verbose) + p.glob("**/*.sh"), license_header("#"), verbose) return 0 if ok else 1