1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 20:57:45 +01:00
gojekyll/commands/variables.go
2017-08-11 13:25:51 -04:00

48 lines
1.0 KiB
Go

package commands
import (
"fmt"
"strings"
"github.com/osteele/gojekyll/site"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
yaml "gopkg.in/yaml.v1"
)
var variables = app.Command("variables", "Display a file or URL path's variables").Alias("v").Alias("var").Alias("vars")
var variablePath = variables.Arg("PATH", "Path, URL, site, or site...").String()
func variablesCommand(site *site.Site) (err error) {
var data interface{}
switch {
case strings.HasPrefix(*variablePath, "site"):
data, err = utils.FollowDots(site, strings.Split(*variablePath, ".")[1:])
if err != nil {
return
}
case *variablePath != "":
data, err = pageFromPathOrRoute(site, *variablePath)
if err != nil {
return
}
default:
data = site
}
data = liquid.FromDrop(data)
if m, ok := data.(map[string]interface{}); ok {
for k, v := range m {
if b, ok := v.([]byte); ok {
m[k] = string(b)
}
}
}
b, err := yaml.Marshal(data)
if err != nil {
return err
}
logger.label("Variables:", "")
fmt.Println(string(b))
return nil
}