forked from PlakarKorp/plakar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdconfig.go
More file actions
47 lines (38 loc) · 1.09 KB
/
oldconfig.go
File metadata and controls
47 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package utils
import (
"fmt"
"maps"
"os"
"go.yaml.in/yaml/v3"
"github.com/PlakarKorp/kloset/config"
)
type OldConfig struct {
DefaultRepository string `yaml:"default-repo"`
Repositories map[string]config.RepositoryConfig `yaml:"repositories"`
Remotes map[string]config.SourceConfig `yaml:"remotes"`
}
func LoadOldConfigIfExists(configFile string) (*config.Config, error) {
cfg := config.NewConfig()
f, err := os.Open(configFile)
if err != nil {
if os.IsNotExist(err) {
return cfg, nil
}
return nil, fmt.Errorf("error reading old config file: %w", err)
}
defer f.Close()
var old OldConfig
if err := yaml.NewDecoder(f).Decode(&old); err != nil {
return nil, fmt.Errorf("failed to parse old config file: %w", err)
}
cfg.DefaultRepository = old.DefaultRepository
cfg.Repositories = old.Repositories
cfg.Sources = old.Remotes
cfg.Destinations = make(map[string]config.DestinationConfig)
for key, val := range cfg.Sources {
res := make(map[string]string)
maps.Copy(res, val)
cfg.Destinations[key] = res
}
return cfg, nil
}