Skip to content

Commit

Permalink
Add data command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 15, 2019
1 parent 6ac240c commit 55746d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/config.go
Expand Up @@ -36,6 +36,7 @@ type Config struct {
Data map[string]interface{}
funcs template.FuncMap
add addCommandConfig
data dataCommandConfig
dump dumpCommandConfig
edit editCommandConfig
_import importCommandConfig
Expand Down
39 changes: 39 additions & 0 deletions cmd/data.go
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
vfs "github.com/twpayne/go-vfs"
)

type dataCommandConfig struct {
format string
}

var dataCommand = &cobra.Command{
Use: "data",
Short: "Write the template data to stdout",
RunE: makeRunE(config.runDataCommand),
}

func init() {
rootCommand.AddCommand(dataCommand)

persistentFlags := dataCommand.PersistentFlags()
persistentFlags.StringVarP(&config.data.format, "format", "f", "json", "format (JSON or YAML)")
}

func (c *Config) runDataCommand(fs vfs.FS, args []string) error {
format, ok := formatMap[strings.ToLower(c.data.format)]
if !ok {
return fmt.Errorf("unknown format: %s", c.data.format)
}
ts, err := c.getTargetState(fs)
if err != nil {
return err
}
return format(os.Stdout, ts.Data)
}

0 comments on commit 55746d9

Please sign in to comment.