...
1
18
19 package config
20
21 import (
22 "encoding/json"
23 "io/ioutil"
24 "os"
25 "scheduler/log"
26 "scheduler/types"
27 )
28
29 func GetConfigFilePath() string {
30 return GetDataPath() + "/" + ConfigurationFileName
31 }
32
33 func GetConfigSchedulerFilePath() string {
34 return GetDataPath() + "/" + ConfigurationSchedulerFileName
35 }
36
37 func SaveConfigurationDynamicToConfigFile() error {
38
39 err := CreateDataFolder()
40 if err != nil {
41 log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
42 return err
43 }
44
45
46 configJson, err := json.MarshalIndent(configurationDynamic, "", " ")
47 err = os.WriteFile(GetConfigFilePath(), configJson, 0644)
48 if err != nil {
49 log.Log.Errorf("Cannot save configuration to file %s: %s", GetConfigFilePath(), err.Error())
50 return err
51 }
52
53 return nil
54 }
55
56 func SaveConfigurationSchedulerToConfigFile(descriptor *types.SchedulerDescriptor) error {
57
58 err := CreateDataFolder()
59 if err != nil {
60 log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
61 return err
62 }
63
64
65 configJson, err := json.MarshalIndent(descriptor, "", " ")
66 err = ioutil.WriteFile(GetConfigSchedulerFilePath(), configJson, 0644)
67 if err != nil {
68 log.Log.Errorf("Cannot save configuration to file %s: %s", GetConfigSchedulerFilePath(), err.Error())
69 return err
70 }
71
72 return nil
73 }
74
75 func CreateDataFolder() error {
76
77 if _, err := os.Stat(GetDataPath()); !os.IsNotExist(err) {
78 return nil
79 }
80
81
82 err := os.Mkdir(GetDataPath(), 0664)
83 if err != nil {
84 log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
85 return err
86 }
87
88 return nil
89 }
90
View as plain text