gendocshelper.go (1652B)
1 // Copyright 2017-present The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package commands
15
16 import (
17 "encoding/json"
18 "fmt"
19 "os"
20 "path/filepath"
21
22 "github.com/gohugoio/hugo/docshelper"
23 "github.com/spf13/cobra"
24 )
25
26 var _ cmder = (*genDocsHelper)(nil)
27
28 type genDocsHelper struct {
29 target string
30 *baseCmd
31 }
32
33 func createGenDocsHelper() *genDocsHelper {
34 g := &genDocsHelper{
35 baseCmd: newBaseCmd(&cobra.Command{
36 Use: "docshelper",
37 Short: "Generate some data files for the Hugo docs.",
38 Hidden: true,
39 }),
40 }
41
42 g.cmd.RunE = func(cmd *cobra.Command, args []string) error {
43 return g.generate()
44 }
45
46 g.cmd.PersistentFlags().StringVarP(&g.target, "dir", "", "docs/data", "data dir")
47
48 return g
49 }
50
51 func (g *genDocsHelper) generate() error {
52 fmt.Println("Generate docs data to", g.target)
53
54 targetFile := filepath.Join(g.target, "docs.json")
55
56 f, err := os.Create(targetFile)
57 if err != nil {
58 return err
59 }
60 defer f.Close()
61
62 enc := json.NewEncoder(f)
63 enc.SetIndent("", " ")
64
65 if err := enc.Encode(docshelper.GetDocProvider()); err != nil {
66 return err
67 }
68
69 fmt.Println("Done!")
70 return nil
71 }