2024-05-11 00:37:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-05-13 16:15:42 +00:00
|
|
|
"log/slog"
|
2024-05-11 00:37:40 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2024-05-13 16:15:42 +00:00
|
|
|
|
|
|
|
"github.com/gookit/config/v2"
|
2024-05-11 00:37:40 +00:00
|
|
|
)
|
|
|
|
|
2024-05-13 20:12:45 +00:00
|
|
|
type ConfigFile struct {
|
2024-05-11 00:37:40 +00:00
|
|
|
SecretKey string `json:"secret_key"`
|
|
|
|
OneUptimeURL string `json:"oneuptime_url"`
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:12:45 +00:00
|
|
|
func newConfigFile() *ConfigFile {
|
|
|
|
return &ConfigFile{
|
2024-05-11 00:37:40 +00:00
|
|
|
SecretKey: "",
|
|
|
|
OneUptimeURL: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:12:45 +00:00
|
|
|
func (c *ConfigFile) loadConfig() error {
|
|
|
|
cfg := &ConfigFile{}
|
2024-05-11 00:37:40 +00:00
|
|
|
err := config.LoadFiles(c.configPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.BindStruct("", cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.SecretKey = cfg.SecretKey
|
|
|
|
c.OneUptimeURL = cfg.OneUptimeURL
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:12:45 +00:00
|
|
|
func (c *ConfigFile) save(secretKey string, url string) error {
|
2024-05-11 00:37:40 +00:00
|
|
|
err := c.loadConfig()
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.Set("secret_key", secretKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = config.Set("oneuptime_url", url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Open the file with os.Create, which truncates the file if it already exists,
|
|
|
|
// and creates it if it doesn't.
|
|
|
|
file, err := os.Create(c.configPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
// Create a JSON encoder that writes to the file, and use Encode method
|
|
|
|
// which will write the map to the file in JSON format.
|
|
|
|
encoder := json.NewEncoder(file)
|
|
|
|
encoder.SetIndent("", " ") // Optional: makes the output more readable
|
|
|
|
return encoder.Encode(config.Data())
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeConfigFile deletes the configuration file.
|
2024-05-13 20:12:45 +00:00
|
|
|
func (c *ConfigFile) removeConfigFile() error {
|
2024-05-11 00:37:40 +00:00
|
|
|
|
|
|
|
// Check if the file exists before attempting to remove it.
|
|
|
|
if _, err := os.Stat(c.configPath()); os.IsNotExist(err) {
|
|
|
|
// File does not exist, return an error or handle it accordingly.
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the file.
|
|
|
|
err := os.Remove(c.configPath())
|
|
|
|
if err != nil {
|
|
|
|
// Handle potential errors in deleting the file.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureDir checks if a directory exists and makes it if it does not.
|
2024-05-13 20:12:45 +00:00
|
|
|
func (c *ConfigFile) ensureDir(dirName string) error {
|
2024-05-11 00:37:40 +00:00
|
|
|
// Check if the directory exists
|
|
|
|
info, err := os.Stat(dirName)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Directory does not exist, create it
|
|
|
|
return os.MkdirAll(dirName, 0755)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
// Exists but is not a directory
|
|
|
|
return os.ErrExist
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// configPath returns the full path to the configuration file,
|
|
|
|
// ensuring the directory exists or creating it if it does not.
|
2024-05-13 20:12:45 +00:00
|
|
|
func (c *ConfigFile) configPath() string {
|
2024-05-11 00:37:40 +00:00
|
|
|
var basePath string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
basePath = os.Getenv("PROGRAMDATA")
|
|
|
|
if basePath == "" {
|
|
|
|
basePath = fmt.Sprintf("C:%sProgramData", string(filepath.Separator))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
basePath = fmt.Sprintf("%setc", string(filepath.Separator))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the directory path where the configuration file will be stored.
|
2024-05-14 09:02:48 +00:00
|
|
|
configDirectory := filepath.Join(basePath, "oneuptime-infrastructure-agent")
|
2024-05-11 00:37:40 +00:00
|
|
|
|
|
|
|
// Ensure the directory exists.
|
|
|
|
err := c.ensureDir(configDirectory)
|
|
|
|
if err != nil {
|
2024-05-13 16:15:42 +00:00
|
|
|
slog.Error("Failed to create config directory: %v", err)
|
2024-05-11 00:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the full path to the configuration file.
|
|
|
|
return filepath.Join(configDirectory, "config.json")
|
|
|
|
}
|