github.com/fafucoder/cilium@v1.6.11/cilium/cmd/cmdref.go (about)

     1  // Copyright 2017 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/spf13/cobra"
    22  	"github.com/spf13/cobra/doc"
    23  )
    24  
    25  var cmdRefDir string
    26  
    27  var cmdRef = &cobra.Command{
    28  	Use:   "cmdref",
    29  	Short: "Generate Cilium command reference",
    30  	Run: func(cmd *cobra.Command, args []string) {
    31  		genCmdRef()
    32  	},
    33  	Hidden: true,
    34  }
    35  
    36  func genCmdRef() {
    37  	// Remove the line 'Auto generated by spf13/cobra on ...'
    38  	rootCmd.DisableAutoGenTag = true
    39  	if err := doc.GenMarkdownTreeCustom(rootCmd, cmdRefDir, filePrepend, linkHandler); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  }
    43  
    44  func linkHandler(s string) string {
    45  	// The generated files have a 'See also' section but the URL's are
    46  	// hardcoded to use Markdown but we only want / have them in HTML
    47  	// later.
    48  	return "../" + strings.Replace(s, ".md", "", 1)
    49  }
    50  
    51  func filePrepend(s string) string {
    52  	// Prepend a HTML comment that this file is autogenerated. So that
    53  	// users are warned before fixing issues in the Markdown files.  Should
    54  	// never show up on the web.
    55  	return fmt.Sprintf("%s\n\n", "<!-- This file was autogenerated via cilium cmdref, do not edit manually-->")
    56  }
    57  
    58  func init() {
    59  	cmdRef.Flags().StringVarP(&cmdRefDir, "directory", "d", "./", "Path to the output directory")
    60  	rootCmd.AddCommand(cmdRef)
    61  }