initial commit
This commit is contained in:
136
cmd/tpstate/init.go
Normal file
136
cmd/tpstate/init.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/logutils"
|
||||
)
|
||||
|
||||
func getEnvFloat64(env string, def float64) (val float64) {
|
||||
osVal := os.Getenv(env)
|
||||
|
||||
if osVal == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
var err error
|
||||
if val, err = strconv.ParseFloat(osVal, 64); err != nil {
|
||||
log.Fatalf("[ERROR] Unable to parse floating point number from environment variable (%s): %v", env, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getEnvInt(env string, def int) (ret int) {
|
||||
val := os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
ret, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Environment variable is not of numeric type: %v\n", env)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getEnvString(env, def string) (val string) {
|
||||
val = os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getEnvBool(env string, def bool) (val bool) {
|
||||
osVal := os.Getenv(env)
|
||||
|
||||
if osVal == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
var err error
|
||||
if val, err = strconv.ParseBool(osVal); err != nil {
|
||||
log.Fatalf("[ERROR] Environment variable is not of boolean type: %v\n", env)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func setLogLevel(l int) {
|
||||
switch {
|
||||
case l <= 20:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("ERROR"))
|
||||
case l > 20 && l <= 40:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("WARNING"))
|
||||
case l > 40 && l <= 60:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("INFO"))
|
||||
case l > 60 && l <= 80:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("DEBUG"))
|
||||
case l > 80:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("TRACE"))
|
||||
}
|
||||
}
|
||||
|
||||
func initialize() {
|
||||
var (
|
||||
tz string
|
||||
dt string
|
||||
err error
|
||||
)
|
||||
|
||||
flag.IntVar(&config.LogLevel,
|
||||
"log",
|
||||
getEnvInt("LOG_LEVEL", 0),
|
||||
"(LOG_LEVEL) Set the log verbosity.",
|
||||
)
|
||||
flag.Float64Var(&config.SunRiseSet.Latitude,
|
||||
"latitude",
|
||||
getEnvFloat64("LATITUDE", 38.749020),
|
||||
"(LATITUDE) Latitude for the sunset/sunrise calculation.",
|
||||
)
|
||||
flag.Float64Var(&config.SunRiseSet.Longitude,
|
||||
"longitude",
|
||||
getEnvFloat64("LONGITUDE", -90.521360),
|
||||
"(LONGITUDE) Longitude for the sunrise/sunset calculation.",
|
||||
)
|
||||
flag.StringVar(&tz,
|
||||
"timezone",
|
||||
getEnvString("TIMEZONE", "America/Chicago"),
|
||||
"(TIMEZONE) Timezone for the sunrise/sunset calculation.",
|
||||
)
|
||||
flag.StringVar(&dt,
|
||||
"date",
|
||||
getEnvString("DATE", time.Now().Format(config.TimeFormat)),
|
||||
"(DATE) Date to use for sunrise/sunset calculation.",
|
||||
)
|
||||
flag.BoolVar(&config.NextSunriseSunset,
|
||||
"next",
|
||||
getEnvBool("NEXT", true),
|
||||
"(NEXT) Determine and calculate the next occuring sunrise/sunset date & time.",
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
setLogLevel(config.LogLevel)
|
||||
log.SetOutput(config.Log)
|
||||
|
||||
config.CalculateDate, err = time.Parse(config.TimeFormat, dt)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Unable to parse time: %v\n", err)
|
||||
}
|
||||
|
||||
config.TimeZone, err = time.LoadLocation(tz)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Unable to parse time zone: %v\n", err)
|
||||
}
|
||||
|
||||
_, tzOffset := config.CalculateDate.In(config.TimeZone).Zone()
|
||||
config.SunRiseSet.UtcOffset = float64(tzOffset / 60 / 60)
|
||||
}
|
Reference in New Issue
Block a user