Skip to content

Commit

Permalink
simplify config validation
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya committed Dec 11, 2024
1 parent 7c8ae20 commit 328d3d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
41 changes: 3 additions & 38 deletions cmd/proplet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ import (

const registryTimeout = 30 * time.Second

var (
wasmFilePath string
wasmBinary []byte
logLevel slog.Level
)
var logLevel slog.Level

func main() {
flag.StringVar(&wasmFilePath, "file", "", "Path to the WASM file")
flag.Parse()

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -43,9 +38,7 @@ func main() {
cancel()
}()

hasWASMFile := wasmFilePath != ""

cfg, err := proplet.LoadConfig("proplet/config.json", hasWASMFile)
cfg, err := proplet.LoadConfig("proplet/config.json")
if err != nil {
logger.Error("Failed to load configuration", slog.String("path", "proplet/config.json"), slog.Any("error", err))
os.Exit(1)

Check failure on line 44 in cmd/proplet/main.go

View workflow job for this annotation

GitHub Actions / Lint and Build

exitAfterDefer: os.Exit will exit, and `defer cancel()` will not run (gocritic)
Expand All @@ -59,21 +52,7 @@ func main() {
logger.Info("Registry connectivity verified", slog.String("url", cfg.RegistryURL))
}

if hasWASMFile {
wasmBinary, err = loadWASMFile(wasmFilePath, logger)
if err != nil {
logger.Error("Failed to load WASM file", slog.String("wasm_file_path", wasmFilePath), slog.Any("error", err))
os.Exit(1)
}
logger.Info("WASM binary loaded at startup", slog.Int("size_bytes", len(wasmBinary)))
}

if cfg.RegistryURL == "" && wasmBinary == nil {
logger.Error("Neither a registry URL nor a WASM binary file was provided")
os.Exit(1)
}

service, err := proplet.NewService(ctx, cfg, wasmBinary, logger)
service, err := proplet.NewService(ctx, cfg, nil, logger)
if err != nil {
logger.Error("Error initializing service", slog.Any("error", err))
os.Exit(1)
Expand All @@ -97,16 +76,6 @@ func configureLogger(level string) *slog.Logger {
return slog.New(logHandler)
}

func loadWASMFile(path string, logger *slog.Logger) ([]byte, error) {
logger.Info("Loading WASM file", slog.String("path", path))
wasmBytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read WASM file: %w", err)
}

return wasmBytes, nil
}

func checkRegistryConnectivity(registryURL string, logger *slog.Logger) error {
ctx, cancel := context.WithTimeout(context.Background(), registryTimeout)
defer cancel()
Expand All @@ -118,25 +87,21 @@ func checkRegistryConnectivity(registryURL string, logger *slog.Logger) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, registryURL, nil)

Check failure on line 87 in cmd/proplet/main.go

View workflow job for this annotation

GitHub Actions / Lint and Build

httpNoBody: http.NoBody should be preferred to the nil request body (gocritic)
if err != nil {
logger.Error("Failed to create HTTP request", slog.String("url", registryURL), slog.Any("error", err))

return fmt.Errorf("failed to create HTTP request: %w", err)

Check failure on line 90 in cmd/proplet/main.go

View workflow job for this annotation

GitHub Actions / Lint and Build

return with no blank line before (nlreturn)
}

resp, err := client.Do(req)
if err != nil {
logger.Error("Failed to connect to registry", slog.String("url", registryURL), slog.Any("error", err))

return fmt.Errorf("failed to connect to registry URL '%s': %w", registryURL, err)

Check failure on line 96 in cmd/proplet/main.go

View workflow job for this annotation

GitHub Actions / Lint and Build

return with no blank line before (nlreturn)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
logger.Error("Registry returned unexpected status", slog.String("url", registryURL), slog.Int("status_code", resp.StatusCode))

return fmt.Errorf("registry URL '%s' returned status: %s", registryURL, resp.Status)

Check failure on line 102 in cmd/proplet/main.go

View workflow job for this annotation

GitHub Actions / Lint and Build

return with no blank line before (nlreturn)
}

logger.Info("Registry connectivity verified", slog.String("url", registryURL))

return nil

Check failure on line 106 in cmd/proplet/main.go

View workflow job for this annotation

GitHub Actions / Lint and Build

return with no blank line before (nlreturn)
}
6 changes: 3 additions & 3 deletions proplet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Config struct {
RegistryToken string `json:"registry_token"`
}

func LoadConfig(filepath string, hasWASMFile bool) (Config, error) {
func LoadConfig(filepath string) (Config, error) {
file, err := os.Open(filepath)
if err != nil {
return Config{}, fmt.Errorf("unable to open configuration file '%s': %w", filepath, err)
Expand All @@ -30,14 +30,14 @@ func LoadConfig(filepath string, hasWASMFile bool) (Config, error) {
return Config{}, fmt.Errorf("failed to parse configuration file '%s': %w", filepath, err)
}

if err := config.Validate(hasWASMFile); err != nil {
if err := config.Validate(); err != nil {
return Config{}, fmt.Errorf("configuration validation failed: %w", err)
}

return config, nil
}

func (c Config) Validate(hasWASMFile bool) error {
func (c Config) Validate() error {
if err := validateField(c.BrokerURL, "broker_url"); err != nil {
return err
}
Expand Down

0 comments on commit 328d3d6

Please sign in to comment.