2023-12-19 21:26:11 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-12-21 20:20:43 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tylertravisty/go-utils/random"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CIDLen = 8
|
|
|
|
DefaultInterval = 10
|
2023-12-19 21:26:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Channel struct {
|
2023-12-21 20:20:43 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
ApiUrl string `json:"api_url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Interval time.Duration `json:"interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) NewChannel(url string, name string) (string, error) {
|
|
|
|
for {
|
|
|
|
id, err := random.String(CIDLen)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("config: error generating ID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := a.Channels[id]; !exists {
|
|
|
|
a.Channels[id] = Channel{id, url, name, DefaultInterval}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 21:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type App struct {
|
2023-12-21 20:20:43 +00:00
|
|
|
Channels map[string]Channel `json:"channels"`
|
2023-12-19 21:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load(filepath string) (*App, error) {
|
|
|
|
f, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("config: error opening file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var app App
|
|
|
|
decoder := json.NewDecoder(f)
|
|
|
|
err = decoder.Decode(&app)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("config: error decoding file into json: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Save(filepath string) error {
|
|
|
|
b, err := json.MarshalIndent(app, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("config: error encoding config into json: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(filepath, b, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("config: error writing config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|