-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_docs.go
161 lines (136 loc) · 3.9 KB
/
create_docs.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package create
import (
"bytes"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/cmd/deprecation"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra/doc"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/spf13/cobra"
)
const (
gendocFrontmatterTemplate = `---
date: %s
title: "%s"
slug: %s
url: %s
description: %s
---
`
)
var (
create_docs_long = templates.LongDesc(`
Creates the documentation markdown files
`)
create_docs_example = templates.Examples(`
# Create the documentation files
jx create docs
`)
)
// CreateDocsOptions the options for the create spring command
type CreateDocsOptions struct {
options.CreateOptions
Dir string
}
// NewCmdCreateDocs creates a command object for the "create" command
func NewCmdCreateDocs(commonOpts *opts.CommonOptions) *cobra.Command {
options := &CreateDocsOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "docs",
Short: "Creates the documentation files",
Aliases: []string{"doc"},
Long: create_docs_long,
Example: create_docs_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run(cmd.Root())
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Dir, "dir", "d", ".", "the directory to generate the docs into")
return cmd
}
// Run implements the command
func (o *CreateDocsOptions) Run(jxCommand *cobra.Command) error {
dir := o.Dir
exists, err := util.DirExists(dir)
if err != nil {
return errors.Wrapf(err, "checking whether the file %q exists", dir)
}
if !exists {
err := os.Mkdir(dir, util.DefaultWritePermissions)
if err != nil {
return fmt.Errorf("failed to create %s: %s", dir, err)
}
}
now := time.Now().Format(time.RFC3339)
prepender := func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
url := "/commands/" + strings.ToLower(base) + "/"
return fmt.Sprintf(gendocFrontmatterTemplate, now, strings.Replace(base, "_", " ", -1), base, url, "list of jx commands")
}
linkHandler := func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name))
return "/commands/" + strings.ToLower(base) + "/"
}
if err := o.genMarkdownDeprecation(jxCommand, dir, now); err != nil {
return errors.Wrapf(err, "generating the deprecation doc")
}
return doc.GenMarkdownTreeCustom(jxCommand, dir, prepender, linkHandler)
}
func (o *CreateDocsOptions) genMarkdownDeprecation(cmd *cobra.Command, dir string, date string) error {
basename := "deprecation"
filename := filepath.Join(dir, basename+".md")
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close() //nolint:errcheck
header := fmt.Sprintf(gendocFrontmatterTemplate, date, "deprecated commands",
basename, "/commands/"+strings.ToLower(basename)+"/", "list of jx commands which have been deprecated")
if _, err := io.WriteString(f, header); err != nil {
return err
}
buf := new(bytes.Buffer)
buf.WriteString("\n\n")
buf.WriteString("## Deprecated Commands\n\n")
buf.WriteString("\n\n")
buf.WriteString("| Command | Removal Date | Replacement |\n")
buf.WriteString("|----------------|----------------|--------------|\n")
o.genMarkdownTableRows(cmd, buf)
_, err = buf.WriteTo(f)
if err != nil {
return err
}
return nil
}
//nolint:errcheck
func (o *CreateDocsOptions) genMarkdownTableRows(cmd *cobra.Command, buf io.StringWriter) {
if cmd.Deprecated != "" {
buf.WriteString(fmt.Sprintf("| %s | %s | %s |\n",
cmd.CommandPath(),
deprecation.GetRemovalDate(cmd),
deprecation.GetReplacement(cmd),
))
return
}
for _, c := range cmd.Commands() {
o.genMarkdownTableRows(c, buf)
}
}