mungedocs.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "errors"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "os/exec"
  20. "path"
  21. "path/filepath"
  22. "strings"
  23. flag "github.com/spf13/pflag"
  24. )
  25. // This needs to be updated when we cut a new release series.
  26. const latestReleaseBranch = "release-1.3"
  27. var (
  28. verbose = flag.Bool("verbose", false, "On verification failure, emit pre-munge and post-munge versions.")
  29. verify = flag.Bool("verify", false, "Exit with status 1 if files would have needed changes but do not change.")
  30. norecurse = flag.Bool("norecurse", false, "Only process the files of --root-dir.")
  31. upstream = flag.String("upstream", "upstream", "The name of the upstream Git remote to pull from")
  32. rootDir = flag.String("root-dir", "", "Root directory containing documents to be processed.")
  33. // "repo-root" seems like a dumb name, this is the relative path (from rootDir) to get to the repoRoot
  34. relRoot = flag.String("repo-root", "..", `Appended to --root-dir to get the repository root.
  35. It's done this way so that generally you just have to set --root-dir.
  36. Examples:
  37. * --root-dir=docs/ --repo-root=.. means the repository root is ./
  38. * --root-dir=/usr/local/long/path/repo/docs/ --repo-root=.. means the repository root is /usr/local/long/path/repo/
  39. * --root-dir=/usr/local/long/path/repo/docs/admin --repo-root=../.. means the repository root is /usr/local/long/path/repo/`)
  40. skipMunges = flag.String("skip-munges", "", "Comma-separated list of munges to *not* run. Available munges are: "+availableMungeList)
  41. repoRoot string
  42. ErrChangesNeeded = errors.New("mungedocs: changes required")
  43. // This records the files in the rootDir in upstream/latest-release
  44. filesInLatestRelease string
  45. // This indicates if the munger is running inside Jenkins
  46. inJenkins bool
  47. // All of the munge operations to perform.
  48. // TODO: allow selection from command line. (e.g., just check links in the examples directory.)
  49. allMunges = []munge{
  50. // Simple "check something" functions must run first.
  51. {"preformat-balance", checkPreformatBalance},
  52. // Functions which modify state.
  53. {"remove-whitespace", updateWhitespace},
  54. {"table-of-contents", updateTOC},
  55. {"unversioned-warning", updateUnversionedWarning},
  56. {"md-links", updateLinks},
  57. {"blank-lines-surround-preformatted", updatePreformatted},
  58. {"header-lines", updateHeaderLines},
  59. {"analytics", updateAnalytics},
  60. {"kubectl-dash-f", updateKubectlFileTargets},
  61. {"sync-examples", syncExamples},
  62. }
  63. availableMungeList = func() string {
  64. names := []string{}
  65. for _, m := range allMunges {
  66. names = append(names, m.name)
  67. }
  68. return strings.Join(names, ",")
  69. }()
  70. )
  71. // a munge processes a document, returning an updated document xor an error.
  72. // The fn is NOT allowed to mutate 'before', if changes are needed it must copy
  73. // data into a new byte array and return that.
  74. type munge struct {
  75. name string
  76. fn func(filePath string, mlines mungeLines) (after mungeLines, err error)
  77. }
  78. type fileProcessor struct {
  79. // Which munge functions should we call?
  80. munges []munge
  81. // Are we allowed to make changes?
  82. verifyOnly bool
  83. }
  84. // Either change a file or verify that it needs no changes (according to modify argument)
  85. func (f fileProcessor) visit(path string) error {
  86. if !strings.HasSuffix(path, ".md") {
  87. return nil
  88. }
  89. fileBytes, err := ioutil.ReadFile(path)
  90. if err != nil {
  91. return err
  92. }
  93. mungeLines := getMungeLines(string(fileBytes))
  94. modificationsMade := false
  95. errFound := false
  96. filePrinted := false
  97. for _, munge := range f.munges {
  98. after, err := munge.fn(path, mungeLines)
  99. if err != nil || !after.Equal(mungeLines) {
  100. if !filePrinted {
  101. fmt.Printf("%s\n----\n", path)
  102. filePrinted = true
  103. }
  104. fmt.Printf("%s:\n", munge.name)
  105. if *verbose {
  106. if len(mungeLines) <= 20 {
  107. fmt.Printf("INPUT: <<<%v>>>\n", mungeLines)
  108. fmt.Printf("MUNGED: <<<%v>>>\n", after)
  109. } else {
  110. fmt.Printf("not printing failed chunk: too many lines\n")
  111. }
  112. }
  113. if err != nil {
  114. fmt.Println(err)
  115. errFound = true
  116. } else {
  117. fmt.Println("contents were modified")
  118. modificationsMade = true
  119. }
  120. fmt.Println("")
  121. }
  122. mungeLines = after
  123. }
  124. // Write out new file with any changes.
  125. if modificationsMade {
  126. if f.verifyOnly {
  127. // We're not allowed to make changes.
  128. return ErrChangesNeeded
  129. }
  130. ioutil.WriteFile(path, mungeLines.Bytes(), 0644)
  131. }
  132. if errFound {
  133. return ErrChangesNeeded
  134. }
  135. return nil
  136. }
  137. func newWalkFunc(fp *fileProcessor, changesNeeded *bool) filepath.WalkFunc {
  138. return func(path string, info os.FileInfo, err error) error {
  139. stat, err := os.Stat(path)
  140. if err != nil {
  141. if os.IsNotExist(err) {
  142. return nil
  143. }
  144. return err
  145. }
  146. if path != *rootDir && stat.IsDir() && *norecurse {
  147. return filepath.SkipDir
  148. }
  149. if err := fp.visit(path); err != nil {
  150. *changesNeeded = true
  151. if err != ErrChangesNeeded {
  152. return err
  153. }
  154. }
  155. return nil
  156. }
  157. }
  158. func wantedMunges() (filtered []munge) {
  159. skipList := strings.Split(*skipMunges, ",")
  160. skipped := map[string]bool{}
  161. for _, m := range skipList {
  162. if len(m) > 0 {
  163. skipped[m] = true
  164. }
  165. }
  166. for _, m := range allMunges {
  167. if !skipped[m.name] {
  168. filtered = append(filtered, m)
  169. } else {
  170. // Remove from the map so we can verify that everything
  171. // requested was in fact valid.
  172. delete(skipped, m.name)
  173. }
  174. }
  175. if len(skipped) != 0 {
  176. fmt.Fprintf(os.Stderr, "ERROR: requested to skip %v, but these are not valid munges. (valid: %v)\n", skipped, availableMungeList)
  177. os.Exit(1)
  178. }
  179. return filtered
  180. }
  181. func main() {
  182. var err error
  183. flag.Parse()
  184. if *rootDir == "" {
  185. fmt.Fprintf(os.Stderr, "usage: %s [--help] [--verify] [--norecurse] --root-dir [--skip-munges=<skip list>] [--upstream=<git remote>] <docs root>\n", flag.Arg(0))
  186. os.Exit(1)
  187. }
  188. repoRoot = path.Join(*rootDir, *relRoot)
  189. repoRoot, err = filepath.Abs(repoRoot)
  190. if err != nil {
  191. fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
  192. os.Exit(2)
  193. }
  194. absRootDir, err := filepath.Abs(*rootDir)
  195. if err != nil {
  196. fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
  197. os.Exit(2)
  198. }
  199. inJenkins = len(os.Getenv("JENKINS_HOME")) != 0
  200. out, err := exec.Command("git", "ls-tree", "-r", "--name-only", fmt.Sprintf("%s/%s", *upstream, latestReleaseBranch), absRootDir).CombinedOutput()
  201. if err != nil {
  202. if inJenkins {
  203. fmt.Fprintf(os.Stderr, "output: %s,\nERROR: %v\n", out, err)
  204. os.Exit(2)
  205. } else {
  206. fmt.Fprintf(os.Stdout, "output: %s,\nERROR: %v\n", out, err)
  207. fmt.Fprintf(os.Stdout, "`git ls-tree -r --name-only %s/%s failed. We'll ignore this error locally, but Jenkins may pick an error. Munger uses the output of this command to determine in unversioned warning, if it should add a link to the doc in release branch.\n", *upstream, latestReleaseBranch)
  208. filesInLatestRelease = ""
  209. }
  210. } else {
  211. filesInLatestRelease = string(out)
  212. }
  213. fp := fileProcessor{
  214. munges: wantedMunges(),
  215. verifyOnly: *verify,
  216. }
  217. // For each markdown file under source docs root, process the doc.
  218. // - If any error occurs: exit with failure (exit >1).
  219. // - If verify is true: exit 0 if no changes needed, exit 1 if changes
  220. // needed.
  221. // - If verify is false: exit 0 if changes successfully made or no
  222. // changes needed, exit 1 if manual changes are needed.
  223. var changesNeeded bool
  224. err = filepath.Walk(*rootDir, newWalkFunc(&fp, &changesNeeded))
  225. if err != nil {
  226. fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
  227. os.Exit(2)
  228. }
  229. if changesNeeded {
  230. if *verify {
  231. fmt.Fprintf(os.Stderr, "FAIL: changes needed but not made due to --verify\n")
  232. } else {
  233. fmt.Fprintf(os.Stderr, "FAIL: some manual changes are still required.\n")
  234. }
  235. os.Exit(1)
  236. }
  237. }