Support version printout (#37)

This commit is contained in:
Nguyen Ngoc Trung (Steven) 2018-10-24 18:00:53 -07:00 committed by Alex Goodman
parent 4385723d14
commit 5dfd9fb9e8
4 changed files with 61 additions and 6 deletions

View file

@ -2,19 +2,33 @@ package cmd
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/wagoodman/dive/image"
"github.com/wagoodman/dive/ui"
"os"
)
// analyze takes a docker image tag, digest, or id and displayes the
// image analysis to the screen
func analyze(cmd *cobra.Command, args []string) {
if len(args) == 0 {
printVersionFlag, err := cmd.PersistentFlags().GetBool("version")
if err == nil && printVersionFlag {
printVersion(cmd, args)
return
}
fmt.Println("No image argument given")
cmd.Help()
os.Exit(1)
}
userImage := args[0]
if userImage == "" {
fmt.Println("No image argument given")
cmd.Help()
os.Exit(1)
}
color.New(color.Bold).Println("Analyzing Image")

View file

@ -2,13 +2,14 @@ package cmd
import (
"fmt"
"os"
"github.com/k0kubun/go-ansi"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tebeka/atexit"
"os"
)
var cfgFile string
@ -19,7 +20,7 @@ var rootCmd = &cobra.Command{
Short: "Docker Image Visualizer & Explorer",
Long: `This tool provides a way to discover and explore the contents of a docker image. Additionally the tool estimates
the amount of wasted space and identifies the offending files from the image.`,
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Run: analyze,
}
@ -44,6 +45,8 @@ func init() {
// TODO: add config options
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dive.yaml)")
rootCmd.PersistentFlags().BoolP("version", "v", false, "display version number")
}
// initConfig reads in config file and ENV variables if set.

34
cmd/version.go Normal file
View file

@ -0,0 +1,34 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
type Version struct {
Version string
Commit string
BuildTime string
}
var version *Version
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "print the version number and exit (also --version)",
Run: printVersion,
}
func init() {
rootCmd.AddCommand(versionCmd)
}
func SetVersion(v *Version) {
version = v
}
func printVersion(cmd *cobra.Command, args []string) {
fmt.Printf("dive %s\n", version.Version)
}

10
main.go
View file

@ -20,9 +20,7 @@
package main
import (
"github.com/wagoodman/dive/cmd"
)
import "github.com/wagoodman/dive/cmd"
var (
version = "No version provided"
@ -31,5 +29,11 @@ var (
)
func main() {
cmd.SetVersion(&cmd.Version{
Version: version,
Commit: commit,
BuildTime: buildTime,
})
cmd.Execute()
}