Skip to content

Commit

Permalink
moved main to cmd, fixed config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bxrne committed Jan 14, 2025
1 parent af5eeaa commit 40811fb
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 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
31 changes: 30 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/viper"
)

// GetConfig returns the singleton instance of the configuration.
// GetConfig returns the application configuration
func GetConfig() (*Config, error) {
var cfg *Config
v := viper.New()
Expand All @@ -31,6 +31,7 @@ func GetConfig() (*Config, error) {
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 +45,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 +61,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
}
5 changes: 3 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestValidateConfigMissingLoggingLevel(t *testing.T) {
})
}

// TEST: GIVEN a configuration file with missing external.openrocket_version WHEN GetConfig is called THEN no error is returned
// TEST: GIVEN a configuration file with missing external.openrocket_version WHEN GetConfig is called THEN error is returned
func TestValidateConfigMissingOpenRocketVersion(t *testing.T) {
withWorkingDir(t, "../..", func() {
cfg, err := config.GetConfig()
Expand All @@ -147,7 +147,8 @@ func TestValidateConfigMissingOpenRocketVersion(t *testing.T) {

cfg.External.OpenRocketVersion = ""
err = cfg.Validate()
if err != nil {
expected := "external.openrocket_version is required"
if err == nil && err.Error() != expected {
t.Errorf("Expected no error, got: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Config struct {
} `mapstructure:"options"`
}

// Marshal to map structure for logging.
// String returns the configuration as a map of strings, useful for testing.
func (c *Config) String() map[string]string {
marshalled := make(map[string]string)
marshalled["app.name"] = c.App.Name
Expand Down
9 changes: 0 additions & 9 deletions main.go

This file was deleted.

0 comments on commit 40811fb

Please sign in to comment.