Skip to content

Commit

Permalink
Tweak gendocs to accept --admin flag to generate admin or non-admin c…
Browse files Browse the repository at this point in the history
…ommands docs
  • Loading branch information
soltysh committed May 18, 2021
1 parent 7c73075 commit a13cf05
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ generate-docs:
.PHONY: generate-docs

generate-docs-admin:
go run ./tools/gendocs-admin
go run ./tools/gendocs --admin
.PHONY: generate-docs-admin

generate-versioninfo:
Expand Down
4 changes: 0 additions & 4 deletions hack/clibyexample/OWNERS

This file was deleted.

55 changes: 0 additions & 55 deletions tools/gendocs-admin/gen_openshift_docs_admin.go

This file was deleted.

93 changes: 0 additions & 93 deletions tools/gendocs-admin/gendocs-admin/gendocs-admin.go

This file was deleted.

18 changes: 13 additions & 5 deletions tools/gendocs/gen_openshift_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/openshift/oc/tools/gendocs/gendocs"
)

var admin = flag.Bool("admin", false, "Generate admin commands docs")

func OutDir(path string) (string, error) {
outDir, err := filepath.Abs(path)
if err != nil {
Expand All @@ -31,9 +34,10 @@ func OutDir(path string) (string, error) {

func main() {
path := "docs/generated/"
if len(os.Args) == 2 {
path = os.Args[1]
} else if len(os.Args) > 2 {
flag.Parse()
if flag.NArg() == 1 {
path = flag.Arg(0)
} else if flag.NArg() > 1 {
fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0])
os.Exit(1)
}
Expand All @@ -44,11 +48,15 @@ func main() {
os.Exit(1)
}

outFile := outDir + "oc-by-example-content.adoc"
out := os.Stdout
cmd := cli.NewOcCommand(&bytes.Buffer{}, out, ioutil.Discard)

if err := gendocs.GenDocs(cmd, outFile); err != nil {
outFile := filepath.Join(outDir, "oc-by-example-content.adoc")
if *admin {
outFile = filepath.Join(outDir, "oc-adm-by-example-content.adoc")
}

if err := gendocs.GenDocs(cmd, outFile, *admin); err != nil {
fmt.Fprintf(os.Stderr, "failed to generate docs: %v\n", err)
os.Exit(1)
}
Expand Down
18 changes: 12 additions & 6 deletions tools/gendocs/gendocs/gendocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ func (x Examples) Less(i, j int) bool {
return xi < xj
}

func GenDocs(cmd *cobra.Command, filename string) error {
func GenDocs(cmd *cobra.Command, filename string, admin bool) error {
out := new(bytes.Buffer)
templateFile, err := filepath.Abs("hack/clibyexample/template")
templatePath := "hack/clibyexample/template"
if admin {
templatePath = "hack/clibyexample/template-admin"
}
templateFile, err := filepath.Abs(templatePath)
if err != nil {
return err
}
Expand All @@ -39,7 +43,7 @@ func GenDocs(cmd *cobra.Command, filename string) error {
output := &unstructured.UnstructuredList{}
output.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("List"))

examples := extractExamples(cmd)
examples := extractExamples(cmd, admin)
for i := range examples {
output.Items = append(output.Items, *examples[i])
}
Expand All @@ -66,16 +70,18 @@ func GenDocs(cmd *cobra.Command, filename string) error {
return nil
}

func extractExamples(cmd *cobra.Command) Examples {
func extractExamples(cmd *cobra.Command, admin bool) Examples {
objs := Examples{}
for _, c := range cmd.Commands() {
if len(c.Deprecated) > 0 {
continue
}
if strings.HasPrefix(c.CommandPath(), "oc adm") {
if strings.HasPrefix(c.CommandPath(), "oc adm") && !admin {
continue
} else if !strings.HasPrefix(c.CommandPath(), "oc adm") && admin {
continue
} else {
objs = append(objs, extractExamples(c)...)
objs = append(objs, extractExamples(c, admin)...)
}
}
if cmd.HasExample() {
Expand Down

0 comments on commit a13cf05

Please sign in to comment.