|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
1 | 16 | package repertory |
2 | 17 |
|
3 | 18 | import ( |
| 19 | + "path/filepath" |
| 20 | + |
4 | 21 | "github.com/arduino/arduino-cli/cli/feedback" |
5 | 22 | "github.com/arduino/arduino-cli/configuration" |
6 | 23 | "github.com/gofrs/uuid" |
7 | 24 | "github.com/spf13/viper" |
8 | | - "os" |
9 | 25 | ) |
10 | 26 |
|
11 | 27 | // Store is the Read Only config storage |
12 | 28 | var Store = viper.New() |
13 | 29 |
|
| 30 | +var ( |
| 31 | + Type = "yaml" |
| 32 | + Name = "repertory" + "." + Type |
| 33 | +) |
| 34 | + |
14 | 35 | // Configure configures the Read Only config storage |
15 | 36 | func Init() { |
16 | 37 | configPath := configuration.GetDefaultArduinoDataDir() |
17 | | - Store.SetConfigType("yaml") |
18 | | - Store.SetConfigName("repertory.yaml") |
| 38 | + Store.SetConfigName(Name) |
| 39 | + Store.SetConfigType(Type) |
19 | 40 | Store.AddConfigPath(configPath) |
| 41 | + configFilePath := filepath.Join(configPath, Name) |
20 | 42 | // Attempt to read config file |
21 | 43 | if err := Store.ReadInConfig(); err != nil { |
22 | 44 | // ConfigFileNotFoundError is acceptable, anything else |
23 | 45 | // should be reported to the user |
24 | 46 | if _, ok := err.(viper.ConfigFileNotFoundError); ok { |
25 | | - // FIXME: how should I treat this error? |
26 | | - installationID, _ := uuid.NewV4() |
27 | | - Store.SetDefault("installation.id", installationID.String()) |
28 | | - installationSecret, _ := uuid.NewV4() |
29 | | - Store.SetDefault("installation.secret", installationSecret.String()) |
30 | | - if err = Store.SafeWriteConfigAs(configPath); err != nil { |
31 | | - if os.IsNotExist(err) { |
32 | | - err = Store.WriteConfigAs(configPath) |
33 | | - } |
| 47 | + generateInstallationData() |
| 48 | + err := Store.WriteConfigAs(configFilePath) |
| 49 | + if err != nil { |
| 50 | + feedback.Errorf("Error writing repertory file: %v", err) |
34 | 51 | } |
35 | 52 | } else { |
36 | 53 | feedback.Errorf("Error reading repertory file: %v", err) |
37 | | - |
38 | 54 | } |
39 | 55 | } |
40 | 56 | } |
| 57 | + |
| 58 | +func generateInstallationData() { |
| 59 | + |
| 60 | + installationID, err := uuid.NewV4() |
| 61 | + if err != nil { |
| 62 | + feedback.Errorf("Error generating installation.id: %v", err) |
| 63 | + } |
| 64 | + Store.Set("installation.id", installationID.String()) |
| 65 | + |
| 66 | + installationSecret, err := uuid.NewV4() |
| 67 | + if err != nil { |
| 68 | + feedback.Errorf("Error generating installation.secret: %v", err) |
| 69 | + } |
| 70 | + Store.Set("installation.secret", installationSecret.String()) |
| 71 | +} |
0 commit comments