forked from elastic/beats
/
docs.go
184 lines (158 loc) · 4.7 KB
/
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package mage
import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"syscall"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)
const (
elasticDocsRepoURL = "https://github.com/elastic/docs.git"
)
type docsBuilder struct{}
type asciidocParams struct {
name string
indexFile string
}
// DocsOption is a documentation generation option for controlling how the docs
// are built.
type DocsOption func(params *asciidocParams)
// DocsName specifies the documentation's name (default to BeatName).
func DocsName(name string) DocsOption {
return func(params *asciidocParams) {
params.name = name
}
}
// DocsIndexFile specifies the index file (defaults to docs/index.asciidoc).
func DocsIndexFile(file string) DocsOption {
return func(params *asciidocParams) {
params.indexFile = file
}
}
// Docs holds the utilities for building documentation.
var Docs = docsBuilder{}
// FieldDocs generates docs/fields.asciidoc from the specified fields.yml file.
func (docsBuilder) FieldDocs(fieldsYML string) error {
// Run the docs_collector.py script.
ve, err := PythonVirtualenv()
if err != nil {
return err
}
python, err := LookVirtualenvPath(ve, "python")
if err != nil {
return err
}
esBeats, err := ElasticBeatsDir()
if err != nil {
return err
}
// TODO: Port this script to Go.
log.Println(">> Generating docs/fields.asciidoc for", BeatName)
return sh.Run(python, LibbeatDir("scripts/generate_fields_docs.py"),
fieldsYML, // Path to fields.yml.
BeatName, // Beat title.
esBeats, // Path to general beats folder.
"--output_path", OSSBeatDir()) // It writes to {output_path}/docs/fields.asciidoc.
}
func (b docsBuilder) AsciidocBook(opts ...DocsOption) error {
params := asciidocParams{
name: BeatName,
indexFile: CWD("docs/index.asciidoc"),
}
for _, opt := range opts {
opt(¶ms)
}
repo, err := GetProjectRepoInfo()
if err != nil {
return err
}
cloneDir := CreateDir(filepath.Join(repo.RootDir, "build/elastic_docs_repo"))
// Clone if elastic_docs_repo does not exist.
if _, err := os.Stat(cloneDir); err != nil {
log.Println("Cloning elastic/docs to", cloneDir)
if err = sh.Run("git", "clone", "--depth=1", elasticDocsRepoURL, cloneDir); err != nil {
return err
}
} else {
log.Println("Using existing elastic/docs at", cloneDir)
}
// Render HTML.
htmlDir := CWD("build/html_docs", params.name)
buildDocsScript := filepath.Join(cloneDir, "build_docs")
args := []string{
"--chunk=1",
"--doc", params.indexFile,
"--out", htmlDir,
}
fmt.Println(">> Building HTML docs at", filepath.Join(htmlDir, "index.html"))
if err := sh.Run(buildDocsScript, args...); err != nil {
return err
}
// Serve docs with and HTTP server and open the browser.
if preview, _ := strconv.ParseBool(os.Getenv("PREVIEW")); preview {
srv := b.servePreview(htmlDir)
url := "http://" + srv.Addr
fmt.Println("Serving docs preview at", url)
b.openBrowser(url)
// Wait
fmt.Println("Ctrl+C to stop")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
srv.Shutdown(context.Background())
}
return nil
}
// open opens the specified URL in the default browser.
func (docsBuilder) openBrowser(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "darwin":
cmd = "open"
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
default:
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
func (docsBuilder) servePreview(dir string) *http.Server {
server := &http.Server{
Addr: net.JoinHostPort("localhost", EnvOr("PREVIEW_PORT", "8000")),
Handler: http.FileServer(http.Dir(dir)),
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(errors.Wrap(err, "failed to start docs preview"))
}
}()
return server
}