Tidying error handling and adding some fluff.
Go Test / test (push) Successful in 53s Details

This commit is contained in:
Fred Boniface 2024-04-28 10:25:41 +01:00
parent 01da611d26
commit b93d36dacd
6 changed files with 23 additions and 6 deletions

View File

@ -5,4 +5,5 @@ RUN go build .
FROM scratch
COPY --from=builder /source/timetable-mgr /bin/timetable-mgr
USER 20400
CMD [ "/bin/timetable-mgr" ]

View File

@ -32,7 +32,7 @@ func CheckCif(cfg *helpers.Configuration) {
log.Info("Full CIF download required")
err := runCifFullDownload(cfg)
if err != nil {
log.Error("Unable to run full CIF Update", zap.Error(err))
log.Warn("Unable to run full CIF Update", zap.Error(err))
}
return
}
@ -61,6 +61,6 @@ func CheckCif(cfg *helpers.Configuration) {
log.Info("CIF Update required", zap.Any("days to update", daysToUpdate))
err = runCifUpdateDownload(cfg, metadata, daysToUpdate)
if err != nil {
log.Error("Daily CIF update failed", zap.Error(err))
log.Warn("Daily CIF update failed", zap.Error(err))
}
}

View File

@ -24,6 +24,7 @@ func runCifFullDownload(cfg *helpers.Configuration) error {
dataStream, err := nrod.NrodStream(url, cfg)
if err != nil {
log.Error("Error downloading CIF data", zap.Error(err))
return err
}
// Parse CIF file

View File

@ -119,7 +119,7 @@ func (c *Configuration) setConfigValue(key, value string) {
// Provides a method to print the configuration struct. Only when the DEBUG env is set to true
func (c *Configuration) PrintConfig() {
if os.Getenv("DEBUG") == "true" {
if os.Getenv("debug") == "true" {
fmt.Println("Configuration:")
fmt.Println("VstpOn: ", c.VstpOn)
fmt.Println("NrodUser: ", c.NrodUser)

13
main.go
View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/signal"
"os/user"
"syscall"
"time"
_ "time/tzdata"
@ -31,7 +32,9 @@ func init() {
printStartupBanner()
fmt.Printf("%sVersion %s \n\n%s", bold+blue, helpers.Version, reset)
//checkRunAsRoot()
// Exits is being run as root
// not necessary and not secure
checkRunAsRoot()
}
func main() {
@ -126,6 +129,14 @@ func printStartupBanner() {
func checkRunAsRoot() {
uid := os.Getuid()
if uid != 0 {
currUser, err := user.Current()
var msg string
if err != nil {
msg = "Unable to determine which user is running the application, but is not being run by root"
} else {
msg = fmt.Sprintf("Running as user: %s, %s", currUser.Uid, currUser.Username)
}
fmt.Println(blue + msg + reset)
return
}

View File

@ -2,6 +2,7 @@ package nrod
import (
"compress/gzip"
"errors"
"fmt"
"io"
"net/http"
@ -22,7 +23,6 @@ func NrodStream(url string, cfg *helpers.Configuration) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Error("Error creating HTTP Request", zap.Error(err))
return nil, err
}
@ -34,9 +34,13 @@ func NrodStream(url string, cfg *helpers.Configuration) (io.ReadCloser, error) {
return nil, err
}
if resp == nil {
err = errors.New("http response error - response = nil")
return nil, err
}
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("unexpected status code: %d", resp.StatusCode)
log.Error("Non-successful status code", zap.Error(err))
return nil, err
}