Vectrum
Sign In
42 lines · 814 Bytes · Shell
#!/usr/bin/env bash
set -euo pipefail
cat > conf.go <<'GO'
package conf

import (
	"encoding/json"
	"fmt"
	"os"
)

// Settings is the on-disk configuration.
type Settings struct {
	Name    string `json:"name"`
	Retries int    `json:"retries"`
}

// Load reads settings from path.
func Load(path string) (Settings, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return Settings{}, fmt.Errorf("conf: reading %s: %w", path, err)
	}
	var s Settings
	if err := json.Unmarshal(data, &s); err != nil {
		return Settings{}, fmt.Errorf("conf: parsing %s: %w", path, err)
	}
	return s, nil
}
GO
cat > main_helper.go <<'GO'
package conf

// Describe summarises the settings at path.
func Describe(path string) (string, error) {
	s, err := Load(path)
	if err != nil {
		return "", err
	}
	return s.Name, nil
}
GO