enable passing of config file on the command line

release/release-0.2.0
parent e997c4b883
commit c7ce3ba4a2

@ -11,9 +11,10 @@ import (
)
type CheckCommand struct {
Servers ServersCommand `command:"servers" description:"checks on servers"`
Snapshots SnapshotsCommand `command:"snapshots" description:"checks on snapshots"`
Storages StoragesCommand `command:"storages" description:"checks on storages"`
ConfigFile string `short:"f" long:"config-file" description:"Name of config file" value-name:"FILE"`
Servers ServersCommand `command:"servers" description:"checks on servers"`
Snapshots SnapshotsCommand `command:"snapshots" description:"checks on snapshots"`
Storages StoragesCommand `command:"storages" description:"checks on storages"`
}
var Checker CheckCommand
@ -25,6 +26,23 @@ type CheckConfig struct {
Token string
}
func config(configFile string) {
if configFile == "" {
return
}
viper.SetConfigFile(configFile)
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Config file not found or error parsing: %v", err)
}
Config.UserId = viper.GetString("gridscale.userid")
Config.Token = viper.GetString("gridscale.token")
}
func main() {
var parser = flags.NewParser(&Checker, flags.Default)
@ -33,17 +51,9 @@ func main() {
Config.UserId = os.Getenv("GRIDSCALE_USER")
Config.Token = os.Getenv("GRIDSCALE_TOKEN")
} else {
// look in check.toml for the config
viper.SetConfigName("check")
viper.AddConfigPath("config")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Config file not found or error parsing: %v", err)
if checkFileExistance("config/check.toml") {
config("config/check.toml")
}
Config.UserId = viper.GetString("gridscale.userid")
Config.Token = viper.GetString("gridscale.token")
}
if _, err := parser.Parse(); err != nil {
@ -57,22 +67,42 @@ func main() {
Unknown("no subcommands specificed.")
}
func PrintMsg(msg string) {
if msg != "" {
fmt.Println(" - " + msg)
}
}
func Ok(msg string) {
fmt.Println("OK - " + msg)
fmt.Print("OK")
PrintMsg(msg)
os.Exit(0)
}
func Warning(msg string) {
fmt.Println("WARNING - " + msg)
fmt.Print("WARNING")
PrintMsg(msg)
os.Exit(1)
}
func Critical(msg string) {
fmt.Println("CRITICAL - " + msg)
fmt.Print("CRITICAL")
PrintMsg(msg)
os.Exit(2)
}
func Unknown(msg string) {
fmt.Println("UNKNOWN - " + msg)
fmt.Print("UNKNOWN")
PrintMsg(msg)
os.Exit(3)
}
func checkFileExistance(fileName string) bool {
if _, err := os.Stat(fileName); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}

@ -10,5 +10,7 @@ type ServersCommand struct {
var serversCommand StoragesCommand
func (x *ServersCommand) Execute(args []string) error {
config(Checker.ConfigFile)
return nil
}

@ -14,6 +14,8 @@ var listServersCommand ListServersCommand
func (x *ListServersCommand) Execute(args []string) error {
config(Checker.ConfigFile)
serversResponse, error := gridscale.GetServers(Config.UserId, Config.Token)
if error != nil {

@ -22,6 +22,8 @@ var serversStatusCommand ServersStatusCommand
func (x *ServersStatusCommand) Execute(args []string) error {
config(Checker.ConfigFile)
if x.Type == "server.status" {
server, err := gridscale.GetServer(Config.UserId, Config.Token, x.ServerUUID)
if err != nil {

@ -18,6 +18,9 @@ type SnapshotsAgeCommand struct {
var snapshotsAgeCommand SnapshotsAgeCommand
func (x *SnapshotsAgeCommand) Execute(args []string) error {
config(Checker.ConfigFile)
storage, err := gridscale.GetStorageByUUID(Config.UserId, Config.Token, x.StorageUUID)
if err != nil {

@ -16,6 +16,9 @@ type SnapshotsCountCommand struct {
var snapshotsCountCommand SnapshotsCountCommand
func (x *SnapshotsCountCommand) Execute(args []string) error {
config(Checker.ConfigFile)
storage, err := gridscale.GetStorageByUUID(Config.UserId, Config.Token, x.StorageUUID)
if err != nil {

@ -14,6 +14,9 @@ type StoragesCommand struct {
var storagesCommand StoragesCommand
func (x *StoragesCommand) Execute(args []string) error {
config(Checker.ConfigFile)
fmt.Println("storages")
return nil
}

@ -14,6 +14,8 @@ var storageListCommand StorageListCommand
func (x *StorageListCommand) Execute(args []string) error {
config(Checker.ConfigFile)
storages, error := gridscale.GetStorages(Config.UserId, Config.Token)
if error != nil {

Loading…
Cancel
Save