-
Notifications
You must be signed in to change notification settings - Fork 1
/
ardi_config.go
217 lines (186 loc) · 5.26 KB
/
ardi_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package core
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/robgonnella/ardi/v2/rpc"
"github.com/robgonnella/ardi/v2/types"
"github.com/robgonnella/ardi/v2/util"
log "github.com/sirupsen/logrus"
)
// ArdiConfig represents core module for ardi.json manipulation
type ArdiConfig struct {
config types.ArdiConfig
confPath string
logger *log.Logger
}
// NewArdiConfig returns core json module for handling ardi.json config
func NewArdiConfig(confPath string, initialConfig types.ArdiConfig, logger *log.Logger) *ArdiConfig {
return &ArdiConfig{
config: initialConfig,
confPath: confPath,
logger: logger,
}
}
// AddBuild to ardi.json
func (a *ArdiConfig) AddBuild(name, sketch, fqbn string, buildProps []string) error {
project, err := util.ProcessSketch(sketch)
if err != nil {
return err
}
newBuild := types.ArdiBuild{
Directory: project.Directory,
Sketch: project.Sketch,
Baud: project.Baud,
FQBN: fqbn,
}
props := util.GeneratePropsMap(buildProps)
newBuild.Props = props
a.logger.Infof("Addding build: %s", name)
a.printBuild(name, newBuild)
a.config.Builds[name] = newBuild
return a.write()
}
// GetCompileOpts returns appropriate compile options for an ardi build
func (a *ArdiConfig) GetCompileOpts(buildName string) (*rpc.CompileOpts, error) {
build, ok := a.config.Builds[buildName]
if !ok {
return nil, fmt.Errorf("No builds found for %s", buildName)
}
buildProps := util.GeneratePropsArray(build.Props)
compileOpts := &rpc.CompileOpts{
FQBN: build.FQBN,
SketchDir: build.Directory,
SketchPath: build.Sketch,
ExportName: buildName,
BuildProps: buildProps,
}
return compileOpts, nil
}
// RemoveBuild removes specified build from ardi.json
func (a *ArdiConfig) RemoveBuild(build string) error {
delete(a.config.Builds, build)
return a.write()
}
// ListBuilds lists build specifications in ardi.json
func (a *ArdiConfig) ListBuilds(builds []string) {
a.logger.Println("")
if len(builds) > 0 {
for _, name := range builds {
if b, ok := a.config.Builds[name]; ok {
a.printBuild(name, b)
}
}
}
for name, build := range a.config.Builds {
a.printBuild(name, build)
}
}
// GetBuilds returns builds specified in config
func (a *ArdiConfig) GetBuilds() map[string]types.ArdiBuild {
return a.config.Builds
}
// AddLibrary to ardi.json
func (a *ArdiConfig) AddLibrary(name, version string) error {
a.config.Libraries[name] = version
return a.write()
}
// RemoveLibrary from ardi.json
func (a *ArdiConfig) RemoveLibrary(name string) error {
delete(a.config.Libraries, name)
return a.write()
}
// ListLibraries lists installed libraries
func (a *ArdiConfig) ListLibraries() {
a.logger.Println("")
for library, version := range a.config.Libraries {
a.logger.Printf("%s: %s\n", library, version)
}
a.logger.Println("")
}
// GetLibraries returns libraries specired in config
func (a *ArdiConfig) GetLibraries() map[string]string {
return a.config.Libraries
}
// AddPlatform to ardi.json
func (a *ArdiConfig) AddPlatform(platform, version string) error {
a.config.Platforms[platform] = version
return a.write()
}
// RemovePlatform from ardi.json
func (a *ArdiConfig) RemovePlatform(platform string) error {
delete(a.config.Platforms, platform)
return a.write()
}
// ListPlatforms lists all board urls in config
func (a *ArdiConfig) ListPlatforms() {
a.logger.Println("")
for platform, vers := range a.config.Platforms {
a.logger.Infof("%s: %s", platform, vers)
}
a.logger.Println("")
}
// GetPlatforms returns platforms specified in config
func (a *ArdiConfig) GetPlatforms() map[string]string {
return a.config.Platforms
}
// AddBoardURL to ardi.json
func (a *ArdiConfig) AddBoardURL(url string) error {
if !util.ArrayContains(a.config.BoardURLS, url) {
a.logger.Infof("Adding board url: %s", url)
a.config.BoardURLS = append(a.config.BoardURLS, url)
return a.write()
}
a.logger.Infof("board url already added: %s", url)
return nil
}
// RemoveBoardURL from ardi.json
func (a *ArdiConfig) RemoveBoardURL(url string) error {
if util.ArrayContains(a.config.BoardURLS, url) {
newList := []string{}
for _, u := range a.config.BoardURLS {
if u != url {
newList = append(newList, u)
}
}
a.config.BoardURLS = newList
return a.write()
}
a.logger.Infof("board url not in config: %s", url)
return nil
}
// ListBoardURLS lists all board urls in config
func (a *ArdiConfig) ListBoardURLS() {
a.logger.Info("Board URLS")
for _, url := range a.config.BoardURLS {
a.logger.Info(url)
}
}
// GetBoardURLS returns board urls specified in config
func (a *ArdiConfig) GetBoardURLS() []string {
return a.config.BoardURLS
}
func (a *ArdiConfig) write() error {
newData, err := json.MarshalIndent(a.config, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(a.confPath, newData, 0644); err != nil {
return err
}
return nil
}
// private
func (a *ArdiConfig) printBuild(name string, b types.ArdiBuild) {
a.logger.Println("")
a.logger.Printf("%s:\n", name)
a.logger.Printf(" Directory: %s\n", b.Directory)
a.logger.Printf(" Sketch: %s\n", b.Sketch)
a.logger.Printf(" Baud: %d\n", b.Baud)
a.logger.Printf(" FQBN: %s\n", b.FQBN)
a.logger.Printf(" Props:\n")
for prop, instruction := range b.Props {
a.logger.Printf(" %s: %s\n", prop, instruction)
}
a.logger.Println("")
}