File: gen.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (109 lines) | stat: -rw-r--r-- 2,194 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"text/template"

	"github.com/aptly-dev/aptly/cmd"
	"github.com/smira/commander"
	"github.com/smira/flag"
)

func allFlags(flags *flag.FlagSet) []*flag.Flag {
	result := []*flag.Flag{}
	flags.VisitAll(func(f *flag.Flag) {
		result = append(result, f)
	})
	return result
}

func findCommand(cmd *commander.Command, name string) (*commander.Command, error) {
	for _, c := range cmd.Subcommands {
		if c.Name() == name {
			return c, nil
		}
	}

	return nil, fmt.Errorf("command %s not found", name)
}

func capitalize(s string) string {
	parts := strings.Split(s, " ")
	for i, part := range parts {
		if part[0] != '<' && part[0] != '[' && part[len(part)-1] != '>' && part[len(part)-1] != ']' {
			parts[i] = "`" + part + "`"
		}
	}

	return strings.Join(parts, " ")
}

var authorsS string

func authors() string {
	return authorsS
}

func main() {
	command := cmd.RootCommand()
	command.UsageLine = "aptly"
	command.Dispatch(nil)

	_File, _ := filepath.Abs("./man")

	templ := template.New("man").Funcs(template.FuncMap{
		"allFlags":    allFlags,
		"findCommand": findCommand,
		"toUpper":     strings.ToUpper,
		"capitalize":  capitalize,
		"authors":     authors,
	})
	template.Must(templ.ParseFiles(filepath.Join(filepath.Dir(_File), "aptly.1.ronn.tmpl")))

	authorsF, err := os.Open(filepath.Join(filepath.Dir(_File), "..", "AUTHORS"))
	if err != nil {
		log.Fatal(err)
	}

	authorsB, err := ioutil.ReadAll(authorsF)
	if err != nil {
		log.Fatal(err)
	}

	authorsF.Close()

	authorsS = string(authorsB)

	output, err := os.Create(filepath.Join(filepath.Dir(_File), "aptly.1.ronn"))
	if err != nil {
		log.Fatal(err)
	}

	err = templ.ExecuteTemplate(output, "main", command)
	if err != nil {
		log.Fatal(err)
	}

	output.Close()

	out, err := exec.Command("ronn", filepath.Join(filepath.Dir(_File), "aptly.1.ronn")).CombinedOutput()
	if err != nil {
		os.Stdout.Write(out)
		log.Fatal(err)
	}

	cmd := exec.Command("man", filepath.Join(filepath.Dir(_File), "aptly.1"))
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
}