2021-03-26 15:24:19 +08:00
/ *
Copyright 2021 The KubeVela Authors .
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 .
* /
2020-10-20 14:50:30 +08:00
package main
import (
2022-01-10 15:28:04 +08:00
"fmt"
2022-01-10 17:45:20 +08:00
"io"
2022-01-10 15:28:04 +08:00
"io/fs"
"io/ioutil"
2020-10-20 14:50:30 +08:00
"log"
2022-01-10 15:28:04 +08:00
"os"
"path/filepath"
2022-01-10 17:45:20 +08:00
"sort"
2022-01-10 15:28:04 +08:00
"strings"
2020-10-20 14:50:30 +08:00
2022-01-10 17:45:20 +08:00
"github.com/spf13/cobra"
2022-04-29 15:59:29 +08:00
"github.com/spf13/cobra/doc"
2022-01-10 17:45:20 +08:00
"github.com/oam-dev/kubevela/apis/types"
2021-02-23 13:03:38 +08:00
"github.com/oam-dev/kubevela/references/cli"
2020-10-20 14:50:30 +08:00
)
2022-01-10 17:45:20 +08:00
// PrintCLIByTag print custom defined index
func PrintCLIByTag ( cmd * cobra . Command , all [ ] * cobra . Command , tag string ) string {
var result string
pl := cli . PrintList { }
for _ , c := range all {
if ! c . IsAvailableCommand ( ) || c . IsAdditionalHelpTopicCommand ( ) {
continue
}
if val , ok := c . Annotations [ types . TagCommandType ] ; ! ok || val != tag {
continue
}
cname := cmd . Name ( ) + " " + c . Name ( )
link := cname
link = strings . Replace ( link , " " , "_" , - 1 )
pl = append ( pl , cli . Printable { Order : c . Annotations [ types . TagCommandOrder ] , Long : fmt . Sprintf ( "* [%s](%s)\t - %s\n" , cname , link , c . Long ) } )
}
sort . Sort ( pl )
for _ , v := range pl {
result += v . Long
}
result += "\n"
return result
}
// GenMarkdownTreeForIndex will generate the markdown doc for vela index
func GenMarkdownTreeForIndex ( cmd * cobra . Command , dir string ) error {
basename := strings . Replace ( cmd . CommandPath ( ) , " " , "_" , - 1 ) + ".md"
filename := filepath . Join ( dir , basename )
f , err := os . Create ( filename )
if err != nil {
return err
}
defer f . Close ( )
if _ , err = io . WriteString ( f , "---\ntitle: CLI Commands\n---\n\n\n" ) ; err != nil {
return err
}
for _ , tp := range [ ] string { types . TypeStart , types . TypeApp , types . TypeCD , types . TypeExtension , types . TypeSystem } {
// write header of type
_ , err = io . WriteString ( f , "## " + tp + "\n\n" )
if err != nil {
return err
}
str := PrintCLIByTag ( cmd , cmd . Commands ( ) , tp )
// write header of type
_ , err = io . WriteString ( f , str )
if err != nil {
return err
}
}
2022-04-29 15:59:29 +08:00
_ , err = io . WriteString ( f , "###### Auto generated by [script in KubeVela](https://github.com/kubevela/kubevela/tree/master/hack/docgen)." )
2022-01-10 17:45:20 +08:00
if err != nil {
return err
}
return nil
}
2020-10-20 14:50:30 +08:00
func main ( ) {
2022-01-10 15:28:04 +08:00
rootPath := "../kubevela.io/docs/cli/"
if len ( os . Args ) > 1 {
rootPath = os . Args [ 1 ]
}
fmt . Println ( "scanning rootPath of CLI docs for replace: " , rootPath )
2021-02-23 13:03:38 +08:00
vela := cli . NewCommand ( )
2022-01-10 15:28:04 +08:00
err := doc . GenMarkdownTree ( vela , rootPath )
if err != nil {
log . Fatal ( err )
}
2022-01-10 17:45:20 +08:00
err = GenMarkdownTreeForIndex ( vela , rootPath )
if err != nil {
log . Fatal ( "generate docs for vela index fail" , err )
}
2022-01-10 15:28:04 +08:00
err = filepath . Walk ( rootPath , func ( path string , info fs . FileInfo , err error ) error {
if err != nil {
return err
}
if info . IsDir ( ) {
return nil
}
data , err := ioutil . ReadFile ( path )
if err != nil {
return err
}
lines := strings . Split ( string ( data ) , "\n" )
if len ( lines ) < 1 {
return nil
}
// check first line to make sure it's autogenerated docs
firstL := lines [ 0 ]
if ! strings . HasPrefix ( firstL , "## vela" ) {
return nil
}
// find the last line and add some link.
var lastIdx int
for idx := len ( lines ) - 1 ; idx >= 0 ; idx -- {
if strings . Contains ( lines [ idx ] , "Auto generated" ) {
lastIdx = idx
break
}
}
if lastIdx == 0 {
return nil
}
2022-04-29 15:59:29 +08:00
lines [ lastIdx ] = "#### Go Back to [CLI Commands](vela) Homepage.\n\n\n###### Auto generated by [spf13/cobra script in KubeVela](https://github.com/kubevela/kubevela/tree/master/hack/docgen)."
2022-01-10 15:28:04 +08:00
// update the title format
title := strings . TrimPrefix ( firstL , "## " )
2022-01-10 17:45:20 +08:00
2022-01-10 15:28:04 +08:00
lines [ 0 ] = "---"
newlines := [ ] string { "---" , "title: " + title }
for idx , line := range lines {
2022-01-10 17:45:20 +08:00
if strings . Contains ( line , "[vela](vela.md)" ) {
lines [ idx ] = ""
continue
}
2022-01-10 15:28:04 +08:00
if strings . Contains ( line , ".md" ) && strings . Contains ( line , "](" ) {
lines [ idx ] = strings . Replace ( line , ".md" , "" , - 1 )
}
}
newlines = append ( newlines , lines ... )
newcontent := strings . Join ( newlines , "\n" )
return ioutil . WriteFile ( path , [ ] byte ( newcontent ) , info . Mode ( ) )
} )
2020-10-20 14:50:30 +08:00
if err != nil {
log . Fatal ( err )
}
}