|
| 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 | +} |
0 commit comments