Skip to content

Commit

Permalink
Add a version command
Browse files Browse the repository at this point in the history
  • Loading branch information
ccremer committed Nov 20, 2019
1 parent 091d76d commit 57e5500
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
7 changes: 5 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -20,6 +21,8 @@ func init() {
}

// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
func Execute() {
if err:= rootCmd.Execute(); err != nil {
log.WithError(err).Fatal("Could not execute command.")
}
}
30 changes: 30 additions & 0 deletions commands/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package commands

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

var (
Version string
Commit string
Date string
)

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

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version and exit",
Long: `Print the version and exit`,
Run: func(cmd *cobra.Command, args []string) {

fmt.Println("version =", Version)
fmt.Println("commit =", Commit)
fmt.Println("date =", Date)
os.Exit(0)
},
}
36 changes: 35 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
package main

import "github.com/appuio/image-cleanup/commands"
import (
"github.com/appuio/image-cleanup/commands"
"os"

log "github.com/sirupsen/logrus"
)

var (
version = "latest"
commit = "snapshot"
date = "unknown"
)

func main() {
commands.Version = version
commands.Commit = commit
commands.Date = date

ConfigureLogging()
commands.Execute()
}

func ConfigureLogging() {

log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})

log.SetOutput(os.Stdout)

//TODO: To make this configurable via flag
level, err := log.ParseLevel("debug")
if err != nil {
log.WithField("error", err).Warn("Using info level.")
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(level)
}
}

0 comments on commit 57e5500

Please sign in to comment.