Skip to content

Commit 157fc5f

Browse files
committed
configs: moved Arduino-IDE related methods to dedicated file
1 parent 3b3a4f0 commit 157fc5f

File tree

2 files changed

+94
-57
lines changed

2 files changed

+94
-57
lines changed

configs/arduino_ide.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* arduino-cli is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2018 ARDUINO AG (http://www.arduino.cc/)
28+
*/
29+
30+
package configs
31+
32+
import (
33+
"errors"
34+
"path/filepath"
35+
36+
"github.com/arduino/go-properties-map"
37+
"github.com/sirupsen/logrus"
38+
"github.com/spf13/viper"
39+
)
40+
41+
// UnserializeFromIDEPreferences loads the config from an IDE
42+
// preferences.txt file, by Updating the specified otherConfigs
43+
func UnserializeFromIDEPreferences(otherConfigs *Configs) error {
44+
logrus.Info("Unserializing from IDE preferences")
45+
props, err := properties.Load(filepath.Join(otherConfigs.ArduinoDataFolder, "preferences.txt"))
46+
if err != nil {
47+
logrus.WithError(err).Warn("Error during unserialize from IDE preferences")
48+
return err
49+
}
50+
err = proxyConfigsFromIDEPrefs(otherConfigs, props)
51+
if err != nil {
52+
logrus.WithError(err).Warn("Error during unserialize from IDE preferences")
53+
return err
54+
}
55+
sketchbookPath, exists := props.SubTree("sketchbook")["path"]
56+
if exists {
57+
otherConfigs.SketchbookPath = sketchbookPath
58+
}
59+
return nil
60+
}
61+
62+
func proxyConfigsFromIDEPrefs(otherConfigs *Configs, props properties.Map) error {
63+
proxy := props.SubTree("proxy")
64+
switch proxy["type"] {
65+
case "auto":
66+
// Automatic proxy
67+
viper.Set("proxy.type", "auto")
68+
break
69+
case "manual":
70+
// Manual proxy configuration
71+
manualConfig := proxy.SubTree("manual")
72+
hostname, exists := manualConfig["hostname"]
73+
if !exists {
74+
return errors.New("Proxy hostname not found in preferences.txt")
75+
}
76+
username := manualConfig["username"]
77+
password := manualConfig["password"]
78+
79+
otherConfigs.ProxyType = "manual"
80+
otherConfigs.ProxyManualConfig = &ProxyConfigs{
81+
Hostname: hostname,
82+
Username: username,
83+
Password: password,
84+
}
85+
viper.Set("proxy", otherConfigs.ProxyManualConfig)
86+
break
87+
case "none":
88+
// No proxy
89+
break
90+
default:
91+
return errors.New("Unsupported proxy config")
92+
}
93+
return nil
94+
}

configs/configs.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
package configs
3535

3636
import (
37-
"errors"
3837
"io/ioutil"
3938
"os"
4039
"path/filepath"
@@ -43,7 +42,6 @@ import (
4342

4443
"github.com/spf13/viper"
4544

46-
properties "github.com/arduino/go-properties-map"
4745
"github.com/bcmi-labs/arduino-cli/common"
4846

4947
"gopkg.in/yaml.v2"
@@ -109,61 +107,6 @@ func Default() Configs {
109107
return defaultConfig
110108
}
111109

112-
// UnserializeFromIDEPreferences loads the config from an IDE preferences.txt file, by Updating the specified otherConfigs.
113-
func UnserializeFromIDEPreferences(otherConfigs *Configs) error {
114-
logrus.Info("Unserializing from IDE preferences")
115-
props, err := properties.Load(filepath.Join(otherConfigs.ArduinoDataFolder, "preferences.txt"))
116-
if err != nil {
117-
logrus.WithError(err).Warn("Error during unserialize from IDE preferences")
118-
return err
119-
}
120-
err = proxyConfigsFromIDEPrefs(otherConfigs, props)
121-
if err != nil {
122-
logrus.WithError(err).Warn("Error during unserialize from IDE preferences")
123-
return err
124-
}
125-
sketchbookPath, exists := props.SubTree("sketchbook")["path"]
126-
if exists {
127-
otherConfigs.SketchbookPath = sketchbookPath
128-
}
129-
return nil
130-
}
131-
132-
func proxyConfigsFromIDEPrefs(otherConfigs *Configs, props properties.Map) error {
133-
proxy := props.SubTree("proxy")
134-
typo := proxy["type"]
135-
switch typo {
136-
case "auto":
137-
//automatic proxy
138-
viper.Set("proxy.type", "auto")
139-
break
140-
case "manual":
141-
//manual proxy configuration
142-
manualConfig := proxy.SubTree("manual")
143-
hostname, exists := manualConfig["hostname"]
144-
if !exists {
145-
return errors.New("Proxy ostname not found in preferences.txt")
146-
}
147-
username := manualConfig["username"]
148-
password := manualConfig["password"]
149-
150-
otherConfigs.ProxyType = "manual"
151-
otherConfigs.ProxyManualConfig = &ProxyConfigs{
152-
Hostname: hostname,
153-
Username: username,
154-
Password: password,
155-
}
156-
viper.Set("proxy", *otherConfigs.ProxyManualConfig)
157-
break
158-
case "none":
159-
//No proxy
160-
break
161-
default:
162-
return errors.New("Unsupported config type")
163-
}
164-
return nil
165-
}
166-
167110
// Unserialize loads the configs from a yaml file.
168111
// Returns the default configuration if there is an
169112
// error.

0 commit comments

Comments
 (0)