2017-04-05 10:18:53 -04:00
|
|
|
// Copyright 2017-present The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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 commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/docshelper"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-04-05 10:18:53 -04:00
|
|
|
)
|
|
|
|
|
2018-04-09 14:42:08 -04:00
|
|
|
var (
|
|
|
|
_ cmder = (*genDocsHelper)(nil)
|
|
|
|
)
|
|
|
|
|
2017-04-05 10:18:53 -04:00
|
|
|
type genDocsHelper struct {
|
|
|
|
target string
|
2018-04-10 03:19:26 -04:00
|
|
|
*baseCmd
|
2018-04-09 14:42:08 -04:00
|
|
|
}
|
|
|
|
|
2017-04-05 10:18:53 -04:00
|
|
|
func createGenDocsHelper() *genDocsHelper {
|
|
|
|
g := &genDocsHelper{
|
2018-04-10 03:19:26 -04:00
|
|
|
baseCmd: newBaseCmd(&cobra.Command{
|
2017-04-05 10:18:53 -04:00
|
|
|
Use: "docshelper",
|
|
|
|
Short: "Generate some data files for the Hugo docs.",
|
|
|
|
Hidden: true,
|
2018-04-10 03:19:26 -04:00
|
|
|
}),
|
2017-04-05 10:18:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
g.cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
|
|
|
return g.generate()
|
|
|
|
}
|
|
|
|
|
2017-07-06 04:34:04 -04:00
|
|
|
g.cmd.PersistentFlags().StringVarP(&g.target, "dir", "", "docs/data", "data dir")
|
2017-04-05 10:18:53 -04:00
|
|
|
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *genDocsHelper) generate() error {
|
|
|
|
fmt.Println("Generate docs data to", g.target)
|
|
|
|
|
|
|
|
targetFile := filepath.Join(g.target, "docs.json")
|
|
|
|
|
|
|
|
f, err := os.Create(targetFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(f)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
|
2020-03-20 11:34:53 -04:00
|
|
|
if err := enc.Encode(docshelper.GetDocProvider()); err != nil {
|
2017-04-05 10:18:53 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Done!")
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|