diff --git a/BUILD.bazel b/BUILD.bazel index 9970e894f..e7391b7f4 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -95,6 +95,7 @@ filegroup( "//test/integration:all-srcs", "//test/unit/gen:all-srcs", "//test/unit/listers:all-srcs", + "//tools/cobra:all-srcs", ], tags = ["automanaged"], visibility = ["//visibility:public"], diff --git a/hack/BUILD.bazel b/hack/BUILD.bazel index f599e8424..56a859796 100644 --- a/hack/BUILD.bazel +++ b/hack/BUILD.bazel @@ -276,7 +276,6 @@ filegroup( "//hack/bin:all-srcs", "//hack/boilerplate:all-srcs", "//hack/build:all-srcs", - "//hack/cobra:all-srcs", "//hack/filter-crd:all-srcs", ], tags = ["automanaged"], diff --git a/hack/cobra/BUILD.bazel b/tools/cobra/BUILD.bazel similarity index 75% rename from hack/cobra/BUILD.bazel rename to tools/cobra/BUILD.bazel index d1756df40..bdb2baf4b 100644 --- a/hack/cobra/BUILD.bazel +++ b/tools/cobra/BUILD.bazel @@ -1,13 +1,13 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") go_library( name = "go_default_library", srcs = ["main.go"], - importpath = "github.com/jetstack/cert-manager/hack/cobra", + importpath = "github.com/jetstack/cert-manager/tools/cobra", visibility = ["//visibility:private"], deps = [ - "//cmd/cainjector/cmd:go_default_library", - "//cmd/controller/cmd:go_default_library", + "//cmd/cainjector/app:go_default_library", + "//cmd/controller/app:go_default_library", "//cmd/ctl/cmd:go_default_library", "@com_github_mitchellh_go_homedir//:go_default_library", "@com_github_spf13_cobra//:go_default_library", @@ -35,3 +35,9 @@ filegroup( tags = ["automanaged"], visibility = ["//visibility:public"], ) + +go_test( + name = "go_default_test", + srcs = ["main_test.go"], + embed = [":go_default_library"], +) diff --git a/hack/cobra/main.go b/tools/cobra/main.go similarity index 69% rename from hack/cobra/main.go rename to tools/cobra/main.go index 2b7b6bc1c..9f16fd920 100644 --- a/hack/cobra/main.go +++ b/tools/cobra/main.go @@ -27,34 +27,54 @@ import ( "github.com/spf13/cobra/doc" "github.com/spf13/pflag" - cainjcmd "github.com/jetstack/cert-manager/cmd/cainjector/cmd" - controllercmd "github.com/jetstack/cert-manager/cmd/controller/cmd" + cainjectorapp "github.com/jetstack/cert-manager/cmd/cainjector/app" + controllerapp "github.com/jetstack/cert-manager/cmd/controller/app" ctlcmd "github.com/jetstack/cert-manager/cmd/ctl/cmd" ) func main() { - args := os.Args + if err := run(os.Args); err != nil { + fmt.Fprintf(os.Stderr, "error: %s\n", err) + os.Exit(1) + } + + os.Exit(0) +} + +func run(args []string) error { if len(args) != 2 { - must(errors.New("expecting single output directory argument")) + return errors.New("expecting single output directory argument") } // remove all global flags that are imported in pflag.CommandLine = nil root, err := homedir.Expand(args[1]) - must(err) + if err != nil { + return err + } - must(ensureDirectory(root)) + if err := ensureDirectory(root); err != nil { + return err + } for _, c := range []*cobra.Command{ - cainjcmd.NewCommandStartInjectorController(nil, nil, nil), - controllercmd.NewCommandStartCertManagerController(nil), + cainjectorapp.NewCommandStartInjectorController(nil, nil, nil), + controllerapp.NewCommandStartCertManagerController(nil), ctlcmd.NewCertManagerCtlCommand(nil, nil, nil, nil), } { dir := filepath.Join(root, c.Use) - must(ensureDirectory(dir)) - must(doc.GenMarkdownTree(c, dir)) + + if err := ensureDirectory(dir); err != nil { + return err + } + + if err := doc.GenMarkdownTree(c, dir); err != nil { + return err + } } + + return nil } func ensureDirectory(dir string) error { @@ -72,10 +92,3 @@ func ensureDirectory(dir string) error { return nil } - -func must(err error) { - if err != nil { - fmt.Fprintf(os.Stderr, "error: %s\n", err) - os.Exit(1) - } -} diff --git a/tools/cobra/main_test.go b/tools/cobra/main_test.go new file mode 100644 index 000000000..2f852b7bb --- /dev/null +++ b/tools/cobra/main_test.go @@ -0,0 +1,71 @@ +/* +Copyright 2020 The Jetstack cert-manager contributors. + +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 main + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +func TestRun(t *testing.T) { + rootDir, err := ioutil.TempDir(os.TempDir(), "cert-manager-cobra") + if err != nil { + t.Fatal(err) + } + defer func() { + if err := os.RemoveAll(rootDir); err != nil { + t.Fatal(err) + } + }() + + tests := map[string]struct { + input []string + expDirs []string + expErr bool + }{ + "if no arguments given should error": { + input: []string{"cobra"}, + expErr: true, + }, + "if two arguments given should error": { + input: []string{"cobra", "foo", "bar"}, + expErr: true, + }, + "if directory given, should write docs": { + input: []string{"cobra", filepath.Join(rootDir, "foo")}, + expDirs: []string{"foo/ca-injector", "foo/cert-manager-controller", "foo/cert-manager-ctl"}, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + err := run(test.input) + if test.expErr != (err != nil) { + t.Errorf("got unexpected error, exp=%t got=%v", + test.expErr, err) + } + + for _, dir := range test.expDirs { + if _, err := os.Stat(filepath.Join(rootDir, dir)); err != nil { + t.Errorf("stat error on expected directory: %s", err) + } + } + }) + } +}