initial commit
This commit is contained in:
41
cmd/tpstate/config.go
Normal file
41
cmd/tpstate/config.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/logutils"
|
||||
"github.com/kelvins/sunrisesunset"
|
||||
)
|
||||
|
||||
type configStructure struct {
|
||||
// time configuration
|
||||
TimeFormat string `json:"time_format"`
|
||||
TimeZone *time.Location `json:"time_zone"`
|
||||
|
||||
// logging
|
||||
LogLevel int `json:"log_level"`
|
||||
Log *logutils.LevelFilter `json:"log_level_fileter"`
|
||||
|
||||
//sunriseset configuration
|
||||
SunRiseSet *sunrisesunset.Parameters `json:"sun_rise_set_configuration"`
|
||||
|
||||
// mode of operation
|
||||
CalculateDate time.Time `json:"sunrise_sunset_calculation_date"`
|
||||
SecondsUntil bool `json:"seconds_until"`
|
||||
NextSunriseSunset bool `json:"next_sunrise_sunset"`
|
||||
}
|
||||
|
||||
// Set Defaults
|
||||
var config = configStructure{
|
||||
LogLevel: 50,
|
||||
TimeFormat: "2006-01-02 15:04:05",
|
||||
Log: &logutils.LevelFilter{
|
||||
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARNING", "ERROR"},
|
||||
Writer: os.Stderr,
|
||||
},
|
||||
SunRiseSet: &sunrisesunset.Parameters{
|
||||
Latitude: 38.749020,
|
||||
Longitude: -90.521360,
|
||||
},
|
||||
}
|
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)
|
||||
}
|
14
cmd/tpstate/main.go
Normal file
14
cmd/tpstate/main.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
initialize()
|
||||
|
||||
_, _, err := nextSunriseSunsetTime(config.CalculateDate)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Unable to calculate sunrise/sunset: %v\n", err)
|
||||
}
|
||||
}
|
44
cmd/tpstate/sunCalculations.go
Normal file
44
cmd/tpstate/sunCalculations.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func nextSunriseSunsetTime(t time.Time) (time.Time, time.Time, error) {
|
||||
s := config.SunRiseSet
|
||||
|
||||
s.Date = t
|
||||
currentSR, currentSS, err := s.GetSunriseSunset()
|
||||
if err != nil {
|
||||
return time.Time{}, time.Time{}, err
|
||||
}
|
||||
|
||||
s.Date = t.Add(24 * time.Hour)
|
||||
nextSR, nextSS, err := s.GetSunriseSunset()
|
||||
if err != nil {
|
||||
return time.Time{}, time.Time{}, err
|
||||
}
|
||||
|
||||
var (
|
||||
nSR time.Time
|
||||
nSS time.Time
|
||||
)
|
||||
|
||||
if currentSR.After(t) {
|
||||
nSR = currentSR
|
||||
} else {
|
||||
nSR = nextSR
|
||||
}
|
||||
|
||||
if currentSS.After(t) {
|
||||
nSS = currentSS
|
||||
} else {
|
||||
nSS = nextSS
|
||||
}
|
||||
|
||||
log.Printf("[TRACE] Next calculated sunrise: %s\n", nSR.Format("2006-01-02 15:04:05"))
|
||||
log.Printf("[TRACE] Next calculated sunset : %s\n", nSS.Format("2006-01-02 15:04:05"))
|
||||
|
||||
return nSR, nSS, nil
|
||||
}
|
Reference in New Issue
Block a user