Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Housekeeping #27

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading