Skip to content

Commit de01688

Browse files
committed
second draft of configs (arduino#65)
1 parent a0d9873 commit de01688

File tree

4 files changed

+116
-6
lines changed

4 files changed

+116
-6
lines changed

cmd/arduino.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
3838
"github.com/bcmi-labs/arduino-cli/cmd/output"
3939
"github.com/bcmi-labs/arduino-cli/common"
40+
"github.com/bcmi-labs/arduino-cli/configs"
4041
"github.com/spf13/cobra"
4142
)
4243

@@ -119,6 +120,9 @@ func InitFlags() {
119120
arduinoCoreListCmd.ResetFlags()
120121
arduinoCoreVersionCmd.ResetFlags()
121122

123+
arduinoConfigCmd.ResetFlags()
124+
arduinoConfigInitCmd.ResetFlags()
125+
122126
ArduinoCmd.PersistentFlags().CountVarP(&GlobalFlags.Verbose, "verbose", "v", "enables verbose output (use more times for a higher level)")
123127
ArduinoCmd.PersistentFlags().StringVar(&GlobalFlags.Format, "format", "invalid", "the output format, can be [text|json]")
124128
ArduinoCmd.PersistentFlags().StringVar(&GlobalFlags.Home, "home", "", "the custom home (if not specified $HOME will be used)")
@@ -128,21 +132,27 @@ func InitFlags() {
128132
arduinoLibCmd.Flags().BoolVar(&arduinoLibFlags.updateIndex, "update-index", false, "Updates the libraries index")
129133

130134
arduinoCoreCmd.Flags().BoolVar(&arduinoCoreFlags.updateIndex, "update-index", false, "Updates the index of cores to the latest version")
135+
136+
arduinoConfigInitCmd.Flags().BoolVar(&arduinoConfigInitFlags.Default, "default", false, "If omitted, ask questions to the user about setting configuration properties, otherwise use default configuration")
137+
arduinoConfigInitCmd.Flags().StringVar(&arduinoConfigInitFlags.Location, "location", configs.DefaultLocation)
131138
}
132139

133140
// InitCommands reinitialize commands (useful for testing too)
134141
func InitCommands() {
135142
ArduinoCmd.ResetCommands()
136143
arduinoLibCmd.ResetCommands()
137144
arduinoCoreCmd.ResetCommands()
145+
arduinoConfigCmd.ResetCommands()
138146

139-
ArduinoCmd.AddCommand(arduinoVersionCmd, arduinoLibCmd, arduinoCoreCmd)
147+
ArduinoCmd.AddCommand(arduinoVersionCmd, arduinoLibCmd, arduinoCoreCmd, arduinoConfigCmd)
140148

141149
arduinoLibCmd.AddCommand(arduinoLibInstallCmd, arduinoLibUninstallCmd, arduinoLibSearchCmd,
142150
arduinoLibVersionCmd, arduinoLibListCmd, arduinoLibDownloadCmd)
143151

144152
arduinoCoreCmd.AddCommand(arduinoCoreListCmd, arduinoCoreDownloadCmd, arduinoCoreVersionCmd,
145153
arduinoCoreInstallCmd)
154+
155+
arduinoConfigCmd.AddCommand(arduinoConfigInitCmd)
146156
}
147157

148158
func arduinoPreRun(cmd *cobra.Command, args []string) {

cmd/arduino_config.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 2017 BCMI LABS SA (http://www.arduino.cc/)
28+
*/
29+
30+
package cmd
31+
32+
import (
33+
"fmt"
34+
35+
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
36+
"github.com/bcmi-labs/arduino-cli/configs"
37+
"github.com/spf13/cobra"
38+
)
39+
40+
var arduinoConfigCmd = &cobra.Command{
41+
Use: `config`,
42+
Short: `Configuration Commands`,
43+
Long: `Configuration Commands`,
44+
Example: `arduino config init # Initializes a new config file into the default location`,
45+
}
46+
47+
var arduinoConfigInitCmd = &cobra.Command{
48+
Use: `init`,
49+
Short: `Initializes a new config file into the default location`,
50+
Long: `Initializes a new config file into the default location ($EXE_DIR/cli-config.yml)`,
51+
Example: `arduino config init # Creates a config file by asking questions to the user into the default location
52+
arduino config init --default # Creates a config file with default configuration into default location`,
53+
Run: executeConfigInitCommand,
54+
}
55+
56+
func executeConfigInitCommand(cmd *cobra.Command, args []string) {
57+
var conf configs.Configs
58+
if !arduinoConfigInitFlags.Default && formatter.IsCurrentFormat("text") {
59+
conf = ConfigsFromQuestions()
60+
} else {
61+
conf = configs.Default()
62+
}
63+
err := conf.Serialize(arduinoConfigInitFlags.Location)
64+
if err != nil {
65+
formatter.PrintErrorMessage(fmt.Sprint("Config file creation error: ", err))
66+
} else {
67+
formatter.PrintResult("success")
68+
}
69+
}
70+
71+
// ConfigsFromQuestions asks some questions to the user to properly initialize configs.
72+
// It does not have much sense to use it in JSON formatting, though.
73+
func ConfigsFromQuestions() configs.Configs {
74+
ret := configs.Default()
75+
//Set of questions here
76+
return ret
77+
}

cmd/flags.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@ var rootCmdFlags struct {
4343

4444
// arduinoLibFlags represents `arduino lib` flags.
4545
var arduinoLibFlags struct {
46-
updateIndex bool
46+
updateIndex bool // If true, update library index.
4747
}
4848

4949
// arduinoCoreFlags represents `arduino core` flags.
5050
var arduinoCoreFlags struct {
51-
updateIndex bool
51+
updateIndex bool // If true, update package index.
52+
}
53+
54+
// arduinoConfigInitFlags represents `arduino config init` flags.
55+
var arduinoConfigInitFlags struct {
56+
Default bool // If false, ask questions to the user about setting configuration properties, otherwise use default configuration.
57+
Location string // The custom location of the file to create.
5258
}

configs/configs.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,51 @@
2727
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
2828
*/
2929

30+
/*
31+
* Package configs contains all CLI configurations handling.
32+
* It is done via a YAML file which can be in a custom location,
33+
* but is defaulted to "$EXECUTABLE_DIR/cli-config.yaml"
34+
*/
3035
package configs
3136

3237
import (
3338
"io/ioutil"
3439
"os"
40+
"path/filepath"
3541

3642
"gopkg.in/yaml.v2"
3743
)
3844

45+
// DefaultLocation represents the default location of the config file (same directory as executable)
46+
var DefaultLocation string
47+
48+
func init() {
49+
DefaultLocation, err := os.Getwd()
50+
if err != nil {
51+
DefaultLocation = "."
52+
}
53+
DefaultLocation = filepath.Join(DefaultLocation, "cli-config.yaml")
54+
}
55+
3956
// Configs represents the possible configurations for the CLI.
4057
type Configs struct {
4158
HTTPProxy string `yaml:"HTTP_Proxy,omitempty"`
42-
SketchBookPath string `yaml:"HTTP_Proxy,omitempty"`
59+
SketchbookPath string `yaml:"sketchbook_path,omitempty"`
4360
LibrariesPath string `yaml:"HTTP_Proxy,omitempty"`
4461
PackagesPath string `yaml:"HTTP_Proxy,omitempty"`
4562
}
4663

4764
// defaultConfig represents the default configuration.
4865
var defaultConfig = Configs{
4966
HTTPProxy: os.Getenv("HTTP_PROXY"),
50-
SketchBookPath: "",
67+
SketchbookPath: "",
5168
LibrariesPath: "",
5269
PackagesPath: "",
5370
}
5471

5572
var envConfig = Configs{
5673
HTTPProxy: os.Getenv("HTTP_PROXY"),
57-
SketchBookPath: os.Getenv("SKETCHBOOK_FOLDER"),
74+
SketchbookPath: os.Getenv("SKETCHBOOK_FOLDER"),
5875
LibrariesPath: os.Getenv("LIBS_FOLDER"),
5976
PackagesPath: os.Getenv("PACKAGES_FOLDER"),
6077
}

0 commit comments

Comments
 (0)