Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 8, 2019
2 parents 48eb049 + 284a0cb commit ece452b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
23 changes: 20 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"github.com/leanovate/mite-go/config"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
Expand All @@ -9,19 +10,35 @@ import (
type dependencies struct {
conf config.Config
miteApi mite.Api
version Version
}

type Version struct {
Version string
Commit string
Date string
}

var deps dependencies

func HandleCommands(c config.Config, m mite.Api) error {
deps = dependencies{conf: c, miteApi: m}
func HandleCommands(c config.Config, m mite.Api, v Version) error {
deps = dependencies{conf: c, miteApi: m, version: v}
rootCmd.Flags().BoolP("version", "v", false, "prints version")
return rootCmd.Execute()
}

var rootCmd = &cobra.Command{
Use: "mite-go",
Use: "mite",
Short: "cli client for mite time tracking",
RunE: func(cmd *cobra.Command, args []string) error {
printShortVersion, err := cmd.Flags().GetBool("version")
if err != nil {
return err
}

if printShortVersion {
fmt.Printf("%s\n", deps.version.Version)
}
return nil
},
}
19 changes: 19 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(versionCommand)
}

var versionCommand = &cobra.Command{
Use: "version",
Short: "prints version",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Version: %s\nCommit: %s\nDate: %s\n", deps.version.Version, deps.version.Commit, deps.version.Date)
return nil
},
}
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@ import (
const configFileName = ".mite-go"
const configType = "toml"

// these flags will be overwritten during the build process by goreleaser
var (
version = "dev"
commit = "none"
date = "none"
)

func main() {
homeDirectory, err := homedir.Dir()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
c := config.NewConfig(configFileName, homeDirectory, configType)
api := mite.NewApi(c.GetApiUrl(), c.GetApiKey())
v := cmd.Version{
Version: version,
Commit: commit,
Date: date,
}

err = cmd.HandleCommands(c, api)
err = cmd.HandleCommands(c, api, v)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down

0 comments on commit ece452b

Please sign in to comment.