diff --git a/cmd/genbashcomp/gen_kubectl_bash_comp.go b/cmd/genbashcomp/gen_kubectl_bash_comp.go index aec7dc6f80..3db7688e55 100644 --- a/cmd/genbashcomp/gen_kubectl_bash_comp.go +++ b/cmd/genbashcomp/gen_kubectl_bash_comp.go @@ -20,39 +20,28 @@ import ( "fmt" "io/ioutil" "os" - "path/filepath" + "github.com/GoogleCloudPlatform/kubernetes/cmd/genutils" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" ) func main() { // use os.Args instead of "flags" because "flags" will mess up the man pages! - outDir := "contrib/completions/bash/" + path := "contrib/completions/bash/" if len(os.Args) == 2 { - outDir = os.Args[1] + path = os.Args[1] } else if len(os.Args) > 2 { fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0]) os.Exit(1) } - outDir, err := filepath.Abs(outDir) + outDir, err := genutils.OutDir(path) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err) os.Exit(1) } - - stat, err := os.Stat(outDir) - if err != nil { - fmt.Fprintf(os.Stderr, "output directory %s does not exist\n", outDir) - os.Exit(1) - } - - if !stat.IsDir() { - fmt.Fprintf(os.Stderr, "output directory %s is not a directory\n", outDir) - os.Exit(1) - } - outFile := outDir + "/kubectl" + outFile := outDir + "kubectl" //TODO os.Stdin should really be something like ioutil.Discard, but a Reader kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard) diff --git a/cmd/gendocs/gen_kubectl_docs.go b/cmd/gendocs/gen_kubectl_docs.go index d345d550c5..1ab1868945 100644 --- a/cmd/gendocs/gen_kubectl_docs.go +++ b/cmd/gendocs/gen_kubectl_docs.go @@ -20,8 +20,8 @@ import ( "fmt" "io/ioutil" "os" - "path/filepath" + "github.com/GoogleCloudPlatform/kubernetes/cmd/genutils" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/spf13/cobra" @@ -29,36 +29,24 @@ import ( func main() { // use os.Args instead of "flags" because "flags" will mess up the man pages! - docsDir := "docs/man/man1/" + path := "docs/" if len(os.Args) == 2 { - docsDir = os.Args[1] + path = os.Args[1] } else if len(os.Args) > 2 { fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0]) - return + os.Exit(1) } - docsDir, err := filepath.Abs(docsDir) + outDir, err := genutils.OutDir(path) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - return + fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err) + os.Exit(1) } - stat, err := os.Stat(docsDir) - if err != nil { - fmt.Fprintf(os.Stderr, "output directory %s does not exist\n", docsDir) - return - } - - if !stat.IsDir() { - fmt.Fprintf(os.Stderr, "output directory %s is not a directory\n", docsDir) - return - } - docsDir = docsDir + "/" - // Set environment variables used by kubectl so the output is consistent, // regardless of where we run. os.Setenv("HOME", "/home/username") //TODO os.Stdin should really be something like ioutil.Discard, but a Reader kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard) - cobra.GenMarkdownTree(kubectl, docsDir) + cobra.GenMarkdownTree(kubectl, outDir) } diff --git a/cmd/genman/gen_kubectl_man.go b/cmd/genman/gen_kubectl_man.go index 80a844100b..84bac5e6a8 100644 --- a/cmd/genman/gen_kubectl_man.go +++ b/cmd/genman/gen_kubectl_man.go @@ -21,9 +21,9 @@ import ( "fmt" "io/ioutil" "os" - "path/filepath" "strings" + "github.com/GoogleCloudPlatform/kubernetes/cmd/genutils" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/cpuguy83/go-md2man/mangen" @@ -34,40 +34,28 @@ import ( func main() { // use os.Args instead of "flags" because "flags" will mess up the man pages! - docsDir := "docs/man/man1/" + path := "docs/man/man1" if len(os.Args) == 2 { - docsDir = os.Args[1] + path = os.Args[1] } else if len(os.Args) > 2 { fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0]) os.Exit(1) } - docsDir, err := filepath.Abs(docsDir) + outDir, err := genutils.OutDir(path) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err) os.Exit(1) } - stat, err := os.Stat(docsDir) - if err != nil { - fmt.Fprintf(os.Stderr, "output directory %s does not exist\n", docsDir) - os.Exit(1) - } - - if !stat.IsDir() { - fmt.Fprintf(os.Stderr, "output directory %s is not a directory\n", docsDir) - os.Exit(1) - } - docsDir = docsDir + "/" - // Set environment variables used by kubectl so the output is consistent, // regardless of where we run. os.Setenv("HOME", "/home/username") //TODO os.Stdin should really be something like ioutil.Discard, but a Reader kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard) - genMarkdown(kubectl, "", docsDir) + genMarkdown(kubectl, "", outDir) for _, c := range kubectl.Commands() { - genMarkdown(c, "kubectl", docsDir) + genMarkdown(c, "kubectl", outDir) } } diff --git a/cmd/genutils/genutils.go b/cmd/genutils/genutils.go new file mode 100644 index 0000000000..b772819a37 --- /dev/null +++ b/cmd/genutils/genutils.go @@ -0,0 +1,41 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +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 genutils + +import ( + "fmt" + "os" + "path/filepath" +) + +func OutDir(path string) (string, error) { + outDir, err := filepath.Abs(path) + if err != nil { + return "", err + } + + stat, err := os.Stat(outDir) + if err != nil { + return "", err + } + + if !stat.IsDir() { + return "", fmt.Errorf("output directory %s is not a directory\n", outDir) + } + outDir = outDir + "/" + return outDir, nil +} diff --git a/cmd/genutils/genutils_test.go b/cmd/genutils/genutils_test.go new file mode 100644 index 0000000000..56f8de67eb --- /dev/null +++ b/cmd/genutils/genutils_test.go @@ -0,0 +1,42 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +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 genutils + +import ( + "testing" +) + +func TestValidDir(t *testing.T) { + _, err := OutDir("./") + if err != nil { + t.Fatal(err) + } +} + +func TestInvalidDir(t *testing.T) { + _, err := OutDir("./nondir") + if err == nil { + t.Fatal(err) + } +} + +func TestNotDir(t *testing.T) { + _, err := OutDir("./genutils_test.go") + if err == nil { + t.Fatal(err) + } +}