annotate cmd/sievemgr/gendocstr.go @ 10:44c07eb8ef08

Add man subcommand to display the user manual Generate the manual from the documentation in godoc format.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Thu, 03 Dec 2020 13:52:28 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
1 // Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name>
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
2 //
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
3 // Permission is hereby granted, free of charge, to any person obtaining
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
4 // a copy of this software and associated documentation files (the
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
5 // "Software"), to deal in the Software without restriction, including
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
6 // without limitation the rights to use, copy, modify, merge, publish,
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
7 // distribute, sublicense, and/or sell copies of the Software, and to
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
8 // permit persons to whom the Software is furnished to do so, subject to
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
9 // the following conditions:
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
10 //
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
11 // The above copyright notice and this permission notice shall be included
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
12 // in all copies or substantial portions of the Software.
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
13 //
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
21
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
22 // +build ignore
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
23
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
24 package main
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
25
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
26 import (
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
27 "fmt"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
28 "go/ast"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
29 "go/build"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
30 "go/doc"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
31 "go/parser"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
32 "go/token"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
33 "io/ioutil"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
34 "os"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
35 "path/filepath"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
36 "strings"
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
37 )
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
38
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
39 func newDocPkg(dir, importPath, outputName string) (*doc.Package, error) {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
40 fset := token.NewFileSet()
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
41 var files []*ast.File
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
42
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
43 filelist, err := ioutil.ReadDir(dir)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
44 if err != nil {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
45 return nil, err
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
46 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
47 for _, d := range filelist {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
48 if !strings.HasSuffix(d.Name(), ".go") ||
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
49 strings.HasSuffix(d.Name(), "_test.go") ||
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
50 d.Name() == outputName {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
51 continue
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
52 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
53 if ok, err := build.Default.MatchFile(dir, d.Name()); err != nil || !ok {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
54 continue
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
55 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
56
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
57 filename := filepath.Join(dir, d.Name())
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
58 f, err := parser.ParseFile(fset, filename, nil,
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
59 parser.ParseComments)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
60 if err != nil {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
61 return nil, err
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
62 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
63 files = append(files, f)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
64 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
65
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
66 return doc.NewFromFiles(fset, files, importPath)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
67 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
68
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
69 func main() {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
70 if len(os.Args) != 5 {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
71 fmt.Fprintf(os.Stderr,
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
72 "usage: genhelp dir import_path var file\n")
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
73 os.Exit(1)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
74 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
75
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
76 dir := os.Args[1]
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
77 importPath := os.Args[2]
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
78 varName := os.Args[3]
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
79 outputName := os.Args[4]
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
80
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
81 w, err := os.OpenFile(filepath.Join(dir, outputName),
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
82 os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
83 if err != nil {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
84 fmt.Fprintf(os.Stderr, "failed to open output file: %s\n", err)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
85 os.Exit(1)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
86 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
87 defer w.Close()
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
88
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
89 pkg, err := newDocPkg(dir, importPath, outputName)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
90 if err != nil {
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
91 fmt.Fprintf(os.Stderr, "failed to parse files: %s\n", err)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
92 os.Exit(1)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
93 }
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
94
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
95 var b strings.Builder
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
96 doc.ToText(&b, pkg.Doc, "", " ", 78)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
97
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
98 fmt.Fprintf(w, "// Code generated by \"gendocstr\"; DO NOT EDIT.\n\n")
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
99 fmt.Fprintf(w, "package %s\n\n", pkg.Name)
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
100 fmt.Fprintf(w, "const %s = %q", varName, b.String())
44c07eb8ef08 Add man subcommand to display the user manual
Guido Berhoerster <guido+sievemgr@berhoerster.name>
parents:
diff changeset
101 }