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 FROM scratch
COPY --from=builder /source/timetable-mgr /bin/timetable-mgr COPY --from=builder /source/timetable-mgr /bin/timetable-mgr
USER 20400
CMD [ "/bin/timetable-mgr" ] CMD [ "/bin/timetable-mgr" ]

View File

@ -32,7 +32,7 @@ func CheckCif(cfg *helpers.Configuration) {
log.Info("Full CIF download required") log.Info("Full CIF download required")
err := runCifFullDownload(cfg) err := runCifFullDownload(cfg)
if err != nil { 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 return
} }
@ -61,6 +61,6 @@ func CheckCif(cfg *helpers.Configuration) {
log.Info("CIF Update required", zap.Any("days to update", daysToUpdate)) log.Info("CIF Update required", zap.Any("days to update", daysToUpdate))
err = runCifUpdateDownload(cfg, metadata, daysToUpdate) err = runCifUpdateDownload(cfg, metadata, daysToUpdate)
if err != nil { 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) dataStream, err := nrod.NrodStream(url, cfg)
if err != nil { if err != nil {
log.Error("Error downloading CIF data", zap.Error(err)) log.Error("Error downloading CIF data", zap.Error(err))
return err
} }
// Parse CIF file // 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 // Provides a method to print the configuration struct. Only when the DEBUG env is set to true
func (c *Configuration) PrintConfig() { func (c *Configuration) PrintConfig() {
if os.Getenv("DEBUG") == "true" { if os.Getenv("debug") == "true" {
fmt.Println("Configuration:") fmt.Println("Configuration:")
fmt.Println("VstpOn: ", c.VstpOn) fmt.Println("VstpOn: ", c.VstpOn)
fmt.Println("NrodUser: ", c.NrodUser) fmt.Println("NrodUser: ", c.NrodUser)

13
main.go
View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"os/user"
"syscall" "syscall"
"time" "time"
_ "time/tzdata" _ "time/tzdata"
@ -31,7 +32,9 @@ func init() {
printStartupBanner() printStartupBanner()
fmt.Printf("%sVersion %s \n\n%s", bold+blue, helpers.Version, reset) 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() { func main() {
@ -126,6 +129,14 @@ func printStartupBanner() {
func checkRunAsRoot() { func checkRunAsRoot() {
uid := os.Getuid() uid := os.Getuid()
if uid != 0 { 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 return
} }

View File

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