Skip to content

Commit

Permalink
Merge pull request #27 from bxrne/housekeeping
Browse files Browse the repository at this point in the history
Housekeeping
  • Loading branch information
bxrne authored Jan 14, 2025
2 parents af5eeaa + 37f5f32 commit 5f5a38a
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 91 deletions.
4 changes: 2 additions & 2 deletions cmd/launchrail/launchrail.go → cmd/launchrail/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package launchrail
package main

import (
"github.com/bxrne/launchrail/internal/config"
Expand All @@ -9,7 +9,7 @@ import (
"github.com/bxrne/launchrail/pkg/thrustcurves"
)

func Root() {
func main() {
log := logger.GetLogger()
log.Debug("Starting...")

Expand Down
7 changes: 3 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ options:
openrocket_file: "./testdata/openrocket/l1.ork"
launchrail:
length: 3.0
angle: 0.0
orientation: 0.0
angle: 2.0
orientation: 1.0
launchsite:
latitude: 37.7749
longitude: -122.4194
altitude: 0.0

altitude: 1.0
44 changes: 38 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
package config

import (
"errors"
"fmt"
"os"

"github.com/spf13/viper"
)

// GetConfig returns the singleton instance of the configuration.
var (
cfg *Config
)

// GetConfig returns the application configuration as a singleton
func GetConfig() (*Config, error) {
var cfg *Config

v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")

if err := v.ReadInConfig(); err != nil {
return nil, errors.New("failed to read config file")
return nil, fmt.Errorf("failed to read config file: %s", err)
}

if err := v.Unmarshal(&cfg); err != nil {
return nil, errors.New("failed to unmarshal config")
return nil, fmt.Errorf("failed to unmarshal config: %s", err)
}

if err := cfg.Validate(); err != nil {
return nil, err
return nil, fmt.Errorf("failed to validate config: %s", err)
}

return cfg, nil
}

// Validate checks the config to error on empty field
func (cfg *Config) Validate() error {
if cfg.App.Name == "" {
return fmt.Errorf("app.name is required")
Expand All @@ -44,6 +48,10 @@ func (cfg *Config) Validate() error {
return fmt.Errorf("logging.level is required")
}

if cfg.External.OpenRocketVersion == "" {
return fmt.Errorf("external.openrocket_version is required")
}

if cfg.Options.MotorDesignation == "" {
return fmt.Errorf("options.motor_designation is required")
}
Expand All @@ -56,5 +64,29 @@ func (cfg *Config) Validate() error {
return fmt.Errorf("options.openrocket_file is invalid: %s", err)
}

if cfg.Options.Launchrail.Length == 0 {
return fmt.Errorf("options.launchrail.length is required")
}

if cfg.Options.Launchrail.Angle == 0 {
return fmt.Errorf("options.launchrail.angle is required")
}

if cfg.Options.Launchrail.Orientation == 0 {
return fmt.Errorf("options.launchrail.orientation is required")
}

if cfg.Options.Launchsite.Latitude == 0 {
return fmt.Errorf("options.launchsite.latitude is required")
}

if cfg.Options.Launchsite.Longitude == 0 {
return fmt.Errorf("options.launchsite.longitude is required")
}

if cfg.Options.Launchsite.Altitude == 0 {
return fmt.Errorf("options.launchsite.altitude is required")
}

return nil
}
Loading

0 comments on commit 5f5a38a

Please sign in to comment.