summaryrefslogtreecommitdiff
path: root/cmd/ponzu/version.go
diff options
context:
space:
mode:
authorMartin Treusch von Buttlar <martin.tvb@vitraum.de>2017-05-22 09:00:23 +0200
committerMartin Treusch von Buttlar <martin.tvb@vitraum.de>2017-05-22 09:00:23 +0200
commit9f4c550b8be0f80fd670f7ded32dbd4aab8d1f4e (patch)
treed4b3ef3ec0ac61a210ca079861e1334ca29a70c8 /cmd/ponzu/version.go
parent4c03187fbef64573ded62f40d5d4dace6c48747b (diff)
add help flags for sub commands to root cmd
Diffstat (limited to 'cmd/ponzu/version.go')
-rw-r--r--cmd/ponzu/version.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/cmd/ponzu/version.go b/cmd/ponzu/version.go
new file mode 100644
index 0000000..d22d5a4
--- /dev/null
+++ b/cmd/ponzu/version.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+
+ "github.com/spf13/cobra"
+)
+
+var versionCmd = &cobra.Command{
+ Use: "version",
+ Aliases: []string{"v"},
+ Short: "Prints the version of Ponzu your project is using.",
+ Long: `Prints the version of Ponzu your project is using. Must be called from
+within a Ponzu project directory.`,
+ Example: `$ ponzu version
+> Ponzu v0.7.1
+(or)
+$ ponzu --cli version
+> Ponzu v0.7.2`,
+ Run: func(cmd *cobra.Command, args []string) {
+ p, err := version(cli)
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+
+ fmt.Fprintf(os.Stdout, "Ponzu v%s\n", p["version"])
+ },
+}
+
+func version(isCLI bool) (map[string]interface{}, error) {
+ kv := make(map[string]interface{})
+
+ info := filepath.Join("cmd", "ponzu", "ponzu.json")
+ if isCLI {
+ gopath, err := getGOPATH()
+ if err != nil {
+ return nil, err
+ }
+ repo := filepath.Join(gopath, "src", "github.com", "ponzu-cms", "ponzu")
+ info = filepath.Join(repo, "cmd", "ponzu", "ponzu.json")
+ }
+
+ b, err := ioutil.ReadFile(info)
+ if err != nil {
+ return nil, err
+ }
+
+ err = json.Unmarshal(b, &kv)
+ if err != nil {
+ return nil, err
+ }
+
+ return kv, nil
+}
+
+func init() {
+ versionCmd.Flags().BoolVar(&cli, "cli", false, "specify that information should be returned about the CLI, not project")
+ RegisterCmdlineCommand(versionCmd)
+}