Skip to content

Commit

Permalink
feat(info): Added version subcommand
Browse files Browse the repository at this point in the history
demeter version now shows you the build version and release date
  • Loading branch information
gnur committed Nov 11, 2019
1 parent b6edf2f commit 0c3d8fd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
13 changes: 12 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,20 @@ retrieving books in epub format that are not in your local library.`,
},
}

// Info holds info related to the build process
type Info struct {
Version string
Commit string
Date string
BuiltBy string
}

var info Info

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
func Execute(in Info) {
info = in
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
47 changes: 47 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// 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 cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Shows version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Demeter", info.Version)
fmt.Println("Build date:", info.Date)
fmt.Println("Commit hash:", info.Commit)
fmt.Println("Built by:", info.BuiltBy)
},
}

func init() {
rootCmd.AddCommand(versionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// resultsCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// resultsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ package main

import "github.com/gnur/demeter/cmd"

var version string
var commit string
var date string
var builtBy string

func main() {
cmd.Execute()
cmd.Execute(cmd.Info{
Version: version,
Commit: commit,
Date: date,
BuiltBy: builtBy,
})
}

0 comments on commit 0c3d8fd

Please sign in to comment.