-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
130 lines (117 loc) · 2.76 KB
/
config.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/fairdatasociety/fairdrive-desktop-app/pkg/api"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
const (
settings = ".fda.yaml"
)
type conf struct {
fc *api.FairOSConfig
mountPoint string
autoMount bool
mountedPods []string
}
func (c *conf) SetupConfig(beeEndpoint, beeBatch, network, rpc, mountPoint string) error {
config := viper.New()
config.Set("bee.endpoint", beeEndpoint)
config.Set("bee.batch", beeBatch)
config.Set("network", network)
config.Set("rpc", rpc)
home, err := homedir.Dir()
if err != nil {
return err
}
if mountPoint == "" {
mountPoint = home
}
config.Set("mountPoint", mountPoint)
cfgFile := filepath.Join(home, settings)
return config.WriteConfigAs(cfgFile)
}
func (c *conf) ReadConfig() error {
config := viper.New()
home, err := homedir.Dir()
if err != nil {
return err
}
cfgFile := filepath.Join(home, settings)
if _, err := os.Stat(cfgFile); err != nil {
return fmt.Errorf("config not found")
} else {
config.SetConfigFile(cfgFile)
if err := config.ReadInConfig(); err != nil {
return err
}
}
c.fc = &api.FairOSConfig{
Bee: config.GetString("bee.endpoint"),
Batch: config.GetString("bee.batch"),
RPC: config.GetString("rpc"),
Network: config.GetString("network"),
}
c.mountPoint = config.GetString("mountPoint")
c.autoMount = config.GetBool("autoMount")
c.mountedPods = config.GetStringSlice("mountedPods")
return nil
}
func (c *conf) GetConfig() *api.FairOSConfig {
return c.fc
}
func (c *conf) GetMountPoint() string {
return c.mountPoint
}
func (c *conf) GetAutoMount() bool {
return c.autoMount
}
func (c *conf) GetMountedPods() []string {
return c.mountedPods
}
func (c *conf) setMountedPods(pods []string) error {
config := viper.New()
home, err := homedir.Dir()
if err != nil {
return err
}
cfgFile := filepath.Join(home, settings)
if _, err := os.Stat(cfgFile); err != nil {
return fmt.Errorf("config not found")
} else {
config.SetConfigFile(cfgFile)
if err := config.ReadInConfig(); err != nil {
return err
}
}
if c.autoMount {
c.mountedPods = pods
config.Set("mountedPods", c.mountedPods)
return config.WriteConfig()
}
return nil
}
//func (c *conf) setAutomount(isChecked bool) error {
// config := viper.New()
// home, err := homedir.Dir()
// if err != nil {
// return err
// }
// cfgFile := filepath.Join(home, settings)
// if _, err := os.Stat(cfgFile); err != nil {
// return fmt.Errorf("config not found")
// } else {
// config.SetConfigFile(cfgFile)
// if err := config.ReadInConfig(); err != nil {
// return err
// }
// }
// c.autoMount = isChecked
// config.Set("autoMount", c.autoMount)
// return config.WriteConfig()
//}
func (c *conf) IsSet() bool {
return c.fc != nil
}