view 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
line wrap: on
line source

// Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// +build ignore

package main

import (
	"fmt"
	"go/ast"
	"go/build"
	"go/doc"
	"go/parser"
	"go/token"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

func newDocPkg(dir, importPath, outputName string) (*doc.Package, error) {
	fset := token.NewFileSet()
	var files []*ast.File

	filelist, err := ioutil.ReadDir(dir)
	if err != nil {
		return nil, err
	}
	for _, d := range filelist {
		if !strings.HasSuffix(d.Name(), ".go") ||
			strings.HasSuffix(d.Name(), "_test.go") ||
			d.Name() == outputName {
			continue
		}
		if ok, err := build.Default.MatchFile(dir, d.Name()); err != nil || !ok {
			continue
		}

		filename := filepath.Join(dir, d.Name())
		f, err := parser.ParseFile(fset, filename, nil,
			parser.ParseComments)
		if err != nil {
			return nil, err
		}
		files = append(files, f)
	}

	return doc.NewFromFiles(fset, files, importPath)
}

func main() {
	if len(os.Args) != 5 {
		fmt.Fprintf(os.Stderr,
			"usage: genhelp dir import_path var file\n")
		os.Exit(1)
	}

	dir := os.Args[1]
	importPath := os.Args[2]
	varName := os.Args[3]
	outputName := os.Args[4]

	w, err := os.OpenFile(filepath.Join(dir, outputName),
		os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to open output file: %s\n", err)
		os.Exit(1)
	}
	defer w.Close()

	pkg, err := newDocPkg(dir, importPath, outputName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse files: %s\n", err)
		os.Exit(1)
	}

	var b strings.Builder
	doc.ToText(&b, pkg.Doc, "", "    ", 78)

	fmt.Fprintf(w, "// Code generated by \"gendocstr\"; DO NOT EDIT.\n\n")
	fmt.Fprintf(w, "package %s\n\n", pkg.Name)
	fmt.Fprintf(w, "const %s = %q", varName, b.String())
}