forked from adamm/autogits
47 lines
967 B
Go
47 lines
967 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/tailscale/hujson"
|
|
)
|
|
|
|
type Config struct {
|
|
ForgeEndpoint string `json:"forge_url"`
|
|
Keys []string `json:"keys"`
|
|
}
|
|
|
|
type contextKey string
|
|
|
|
const configKey contextKey = "config"
|
|
|
|
func ReadConfig(reader io.Reader) (*Config, error) {
|
|
data, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading config data: %w", err)
|
|
}
|
|
config := Config{}
|
|
data, err = hujson.Standardize(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse json: %w", err)
|
|
}
|
|
if err := json.Unmarshal(data, &config); err != nil {
|
|
return nil, fmt.Errorf("error parsing json to api keys and target url: %w", err)
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
func ReadConfigFile(filename string) (*Config, error) {
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot open config file for reading. err: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
return ReadConfig(file)
|
|
}
|