github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/mdtogo/main.go (about) 1 // Copyright 2019 The Kubernetes Authors. 2 // SPDX-License-Identifier: Apache-2.0 3 4 // Package main generates cobra.Command go variables containing documentation read from .md files. 5 // Usage: mdtogo SOURCE_MD_DIR/ DEST_GO_DIR/ [--recursive=true] [--license=license.txt|none] 6 // 7 // The command will create a docs.go file under DEST_GO_DIR/ containing string variables to be 8 // used by cobra commands for documentation. The variable names are generated from the name of 9 // the directory in which the files resides, replacing '-' with ”, title casing the name. 10 // All *.md files will be read from DEST_GO_DIR/, including subdirectories if --recursive=true, 11 // and a single DEST_GO_DIR/docs.go file is generated. 12 // 13 // The content for each of the three variables created per folder, are set 14 // by looking for a HTML comment on one of two forms: 15 // 16 // <!--mdtogo:<VARIABLE_NAME>--> 17 // 18 // ..some content.. 19 // 20 // <!--mdtogo--> 21 // 22 // or 23 // 24 // <!--mdtogo:<VARIABLE_NAME> 25 // ..some content.. 26 // --> 27 // 28 // The first are for content that should show up in the rendered HTML, while 29 // the second is for content that should be hidden in the rendered HTML. 30 // 31 // <VARIABLE_NAME> must be suffixed with Short, Long or Examples; <VARIABLE_NAME>s without 32 // a prefix will have an assumed prefix of the parent directory of the markdown file. 33 // 34 // Flags: 35 // 36 // --recursive=true 37 // Scan the directory structure recursively for .md files 38 // --license 39 // Controls the license header added to the files. Specify a path to a license file, 40 // or "none" to skip adding a license. 41 package main 42 43 import ( 44 "errors" 45 "fmt" 46 "os" 47 "strings" 48 49 "github.com/GoogleContainerTools/kpt/mdtogo/cmddocs" 50 "github.com/GoogleContainerTools/kpt/mdtogo/common" 51 ) 52 53 var recursive bool 54 var licenseFile string 55 var strategy string 56 57 const ( 58 cmdDocsStrategy = "cmdDocs" 59 futureStrategy = "future" // please replace it with the next strategy we add 60 ) 61 62 func main() { 63 for _, a := range os.Args { 64 if a == "--recursive=true" { 65 recursive = true 66 } 67 if strings.HasPrefix(a, "--strategy=") { 68 switch a { 69 case "--strategy=cmdDocs": 70 strategy = cmdDocsStrategy 71 default: 72 fmt.Fprintf(os.Stderr, "Invalid strategy %s\n", a) 73 os.Exit(1) 74 } 75 } 76 if strings.HasPrefix(a, "--license=") { 77 licenseFile = strings.ReplaceAll(a, "--license=", "") 78 } 79 } 80 81 if len(os.Args) < 3 { 82 fmt.Fprintf(os.Stderr, "Usage: mdtogo SOURCE_MD_DIR/ DEST_GO_DIR/\n") 83 os.Exit(1) 84 } 85 source := os.Args[1] 86 dest := os.Args[2] 87 88 files, err := common.ReadFiles(source, recursive) 89 if err != nil { 90 fmt.Fprintf(os.Stderr, "%v\n", err) 91 os.Exit(1) 92 } 93 94 license := getLicense() 95 96 switch strategy { 97 case cmdDocsStrategy: 98 docs := cmddocs.ParseCmdDocs(files) 99 err = cmddocs.Write(docs, dest, license) 100 case futureStrategy: 101 err = errors.New("this strategy should not be used, please replace it with a real strategy") 102 } 103 104 if err != nil { 105 fmt.Fprintf(os.Stderr, "%v\n", err) 106 os.Exit(1) 107 } 108 } 109 110 func getLicense() string { 111 var license string 112 113 switch licenseFile { 114 case "": 115 license = `// Copyright 2019 The Kubernetes Authors. 116 // SPDX-License-Identifier: Apache-2.0` 117 case "none": 118 // no license -- maybe added by another tool 119 default: 120 b, err := os.ReadFile(licenseFile) 121 if err != nil { 122 fmt.Fprintf(os.Stderr, "%v\n", err) 123 os.Exit(1) 124 } 125 license = string(b) 126 } 127 return license 128 }