2022-05-05 13:25:50 +00:00
|
|
|
// Copyright 2022 Cloudbase Solutions SRL
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
|
// a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
|
|
|
|
|
2022-05-02 17:55:29 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-16 19:31:58 +00:00
|
|
|
"errors"
|
2022-06-17 10:58:35 +00:00
|
|
|
"fmt"
|
2022-05-02 17:55:29 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2022-06-17 10:58:35 +00:00
|
|
|
"sync"
|
2022-05-02 17:55:29 +00:00
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
|
|
2023-07-22 22:26:47 +00:00
|
|
|
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
2022-05-02 17:55:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2022-05-04 11:44:10 +00:00
|
|
|
DefaultAppFolder = "garm-cli"
|
2022-05-02 17:55:29 +00:00
|
|
|
DefaultConfigFileName = "config.toml"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func getConfigFilePath() (string, error) {
|
|
|
|
|
configDir, err := getHomeDir()
|
|
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return "", fmt.Errorf("error fetching home folder: %w", err)
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := ensureHomeDir(configDir); err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return "", fmt.Errorf("error ensuring config dir: %w", err)
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfgFile := filepath.Join(configDir, DefaultConfigFileName)
|
|
|
|
|
return cfgFile, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LoadConfig() (*Config, error) {
|
|
|
|
|
cfgFile, err := getConfigFilePath()
|
|
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error fetching config: %w", err)
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:40:59 +00:00
|
|
|
if _, err := os.Stat(cfgFile); err != nil {
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
// return empty config
|
|
|
|
|
return &Config{}, nil
|
|
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error accessing config file: %w", err)
|
2022-05-03 12:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:55:29 +00:00
|
|
|
var config Config
|
|
|
|
|
if _, err := toml.DecodeFile(cfgFile, &config); err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error decoding toml: %w", err)
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
2022-06-17 10:58:35 +00:00
|
|
|
mux sync.Mutex
|
2022-05-02 17:55:29 +00:00
|
|
|
Managers []Manager `toml:"manager"`
|
|
|
|
|
ActiveManager string `toml:"active_manager"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Config) HasManager(mgr string) bool {
|
2022-06-17 10:58:35 +00:00
|
|
|
c.mux.Lock()
|
|
|
|
|
defer c.mux.Unlock()
|
2022-05-02 17:55:29 +00:00
|
|
|
if mgr == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for _, val := range c.Managers {
|
|
|
|
|
if val.Name == mgr {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 10:58:35 +00:00
|
|
|
func (c *Config) SetManagerToken(name, token string) error {
|
|
|
|
|
c.mux.Lock()
|
|
|
|
|
defer c.mux.Unlock()
|
|
|
|
|
found := false
|
|
|
|
|
newManagerList := []Manager{}
|
|
|
|
|
for _, mgr := range c.Managers {
|
|
|
|
|
newMgr := Manager{
|
|
|
|
|
Name: mgr.Name,
|
|
|
|
|
BaseURL: mgr.BaseURL,
|
|
|
|
|
Token: mgr.Token,
|
|
|
|
|
}
|
|
|
|
|
if mgr.Name == name {
|
|
|
|
|
found = true
|
|
|
|
|
newMgr.Token = token
|
|
|
|
|
}
|
|
|
|
|
newManagerList = append(newManagerList, newMgr)
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
return fmt.Errorf("profile %s not found", name)
|
|
|
|
|
}
|
|
|
|
|
c.Managers = newManagerList
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Config) DeleteProfile(name string) error {
|
|
|
|
|
c.mux.Lock()
|
|
|
|
|
defer c.mux.Unlock()
|
|
|
|
|
newManagers := []Manager{}
|
|
|
|
|
for _, val := range c.Managers {
|
|
|
|
|
if val.Name == name {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
newManagers = append(newManagers, Manager{
|
|
|
|
|
Name: val.Name,
|
|
|
|
|
BaseURL: val.BaseURL,
|
|
|
|
|
Token: val.Token,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
c.Managers = newManagers
|
|
|
|
|
if c.ActiveManager == name {
|
|
|
|
|
if len(c.Managers) > 0 {
|
|
|
|
|
c.ActiveManager = c.Managers[0].Name
|
|
|
|
|
} else {
|
|
|
|
|
c.ActiveManager = ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 17:55:29 +00:00
|
|
|
func (c *Config) GetActiveConfig() (Manager, error) {
|
2022-06-17 10:58:35 +00:00
|
|
|
c.mux.Lock()
|
|
|
|
|
defer c.mux.Unlock()
|
2022-05-02 17:55:29 +00:00
|
|
|
if c.ActiveManager == "" {
|
|
|
|
|
return Manager{}, runnerErrors.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, val := range c.Managers {
|
|
|
|
|
if val.Name == c.ActiveManager {
|
|
|
|
|
return val, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Manager{}, runnerErrors.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Config) SaveConfig() error {
|
2022-06-17 10:58:35 +00:00
|
|
|
c.mux.Lock()
|
|
|
|
|
defer c.mux.Unlock()
|
2022-05-02 17:55:29 +00:00
|
|
|
cfgFile, err := getConfigFilePath()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error getting config: %w", err)
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cfgHandle, err := os.Create(cfgFile)
|
|
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error getting file handle: %w", err)
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
encoder := toml.NewEncoder(cfgHandle)
|
|
|
|
|
if err := encoder.Encode(c); err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error saving config: %w", err)
|
2022-05-02 17:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
|
Name string `toml:"name"`
|
|
|
|
|
BaseURL string `toml:"base_url"`
|
|
|
|
|
Token string `toml:"bearer_token"`
|
|
|
|
|
}
|