Commit 55d133ea authored by Eric Paris's avatar Eric Paris

doc/bash completions autogeneration directory helper

parent ebb0affc
...@@ -20,39 +20,28 @@ import ( ...@@ -20,39 +20,28 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"github.com/GoogleCloudPlatform/kubernetes/cmd/genutils"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
) )
func main() { func main() {
// use os.Args instead of "flags" because "flags" will mess up the man pages! // 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 { if len(os.Args) == 2 {
outDir = os.Args[1] path = os.Args[1]
} else if len(os.Args) > 2 { } else if len(os.Args) > 2 {
fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0])
os.Exit(1) os.Exit(1)
} }
outDir, err := filepath.Abs(outDir) outDir, err := genutils.OutDir(path)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
os.Exit(1) os.Exit(1)
} }
outFile := outDir + "kubectl"
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"
//TODO os.Stdin should really be something like ioutil.Discard, but a Reader //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) kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
......
...@@ -20,8 +20,8 @@ import ( ...@@ -20,8 +20,8 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"github.com/GoogleCloudPlatform/kubernetes/cmd/genutils"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -29,36 +29,24 @@ import ( ...@@ -29,36 +29,24 @@ import (
func main() { func main() {
// use os.Args instead of "flags" because "flags" will mess up the man pages! // 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 { if len(os.Args) == 2 {
docsDir = os.Args[1] path = os.Args[1]
} else if len(os.Args) > 2 { } else if len(os.Args) > 2 {
fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0]) 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 { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
return 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, // Set environment variables used by kubectl so the output is consistent,
// regardless of where we run. // regardless of where we run.
os.Setenv("HOME", "/home/username") os.Setenv("HOME", "/home/username")
//TODO os.Stdin should really be something like ioutil.Discard, but a Reader //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) kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
cobra.GenMarkdownTree(kubectl, docsDir) cobra.GenMarkdownTree(kubectl, outDir)
} }
...@@ -21,9 +21,9 @@ import ( ...@@ -21,9 +21,9 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/GoogleCloudPlatform/kubernetes/cmd/genutils"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/cpuguy83/go-md2man/mangen" "github.com/cpuguy83/go-md2man/mangen"
...@@ -34,40 +34,28 @@ import ( ...@@ -34,40 +34,28 @@ import (
func main() { func main() {
// use os.Args instead of "flags" because "flags" will mess up the man pages! // 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 { if len(os.Args) == 2 {
docsDir = os.Args[1] path = os.Args[1]
} else if len(os.Args) > 2 { } else if len(os.Args) > 2 {
fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0])
os.Exit(1) os.Exit(1)
} }
docsDir, err := filepath.Abs(docsDir) outDir, err := genutils.OutDir(path)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
os.Exit(1) 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, // Set environment variables used by kubectl so the output is consistent,
// regardless of where we run. // regardless of where we run.
os.Setenv("HOME", "/home/username") os.Setenv("HOME", "/home/username")
//TODO os.Stdin should really be something like ioutil.Discard, but a Reader //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) kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
genMarkdown(kubectl, "", docsDir) genMarkdown(kubectl, "", outDir)
for _, c := range kubectl.Commands() { for _, c := range kubectl.Commands() {
genMarkdown(c, "kubectl", docsDir) genMarkdown(c, "kubectl", outDir)
} }
} }
......
/*
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
}
/*
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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment