Skip to content

Commit c625756

Browse files
committed
refactoring, added folder functions for packages, cores, tools and moved flags of commands to a separate file
1 parent deaa893 commit c625756

File tree

5 files changed

+98
-141
lines changed

5 files changed

+98
-141
lines changed

cmd/arduino.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,6 @@ const (
7171

7272
var versions = make(map[string]string)
7373

74-
// GlobalFlags represents flags available in all the program.
75-
var GlobalFlags struct {
76-
Verbose int // More time verbose flag is written, the more the Verbose count increases. Represents verbosity level.
77-
Format string // The Output format (e.g. text, json).
78-
}
79-
80-
// rootCmdFlags represent flags available to the root command.
81-
var rootCmdFlags struct {
82-
ConfigFile string // The path of the config file provided by the omonym flag.
83-
}
84-
8574
// arduinoCmd represents the base command when called without any subcommands
8675
var arduinoCmd = &cobra.Command{
8776
Use: "arduino",

cmd/arduino_core.go

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,9 @@
3030
package cmd
3131

3232
import (
33-
"fmt"
34-
"io/ioutil"
35-
"os"
36-
"path/filepath"
37-
"strings"
38-
39-
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
40-
"github.com/bcmi-labs/arduino-cli/cmd/output"
4133
"github.com/bcmi-labs/arduino-cli/cmd/pretty_print"
4234
"github.com/bcmi-labs/arduino-cli/common"
4335
"github.com/spf13/cobra"
44-
"github.com/zieckey/goini"
4536
)
4637

4738
var arduinoCoreCmd = &cobra.Command{
@@ -59,10 +50,6 @@ With -v tag (up to 2 times) can provide more verbose output.`,
5950
Run: executeCoreListCommand,
6051
}
6152

62-
var arduinoCoreFlags struct {
63-
updateIndex bool
64-
}
65-
6653
func init() {
6754
arduinoCmd.AddCommand(arduinoCoreCmd)
6855
arduinoCoreCmd.AddCommand(arduinoCoreListCmd)
@@ -79,89 +66,5 @@ func executeCoreCommand(cmd *cobra.Command, args []string) {
7966
}
8067

8168
func executeCoreListCommand(cmd *cobra.Command, args []string) {
82-
libHome, err := common.GetDefaultLibFolder()
83-
if err != nil {
84-
formatter.PrintErrorMessage("Cannot get libraries folder")
85-
return
86-
}
87-
88-
//prettyPrints.LibStatus(status)
89-
dir, err := os.Open(libHome)
90-
if err != nil {
91-
formatter.PrintErrorMessage("Cannot open libraries folder")
92-
return
93-
}
94-
95-
dirFiles, err := dir.Readdir(0)
96-
if err != nil {
97-
formatter.PrintErrorMessage("Cannot read into libraries folder")
98-
return
99-
}
100-
101-
libs := make(map[string]interface{}, 10)
10269

103-
//TODO: optimize this algorithm
104-
// time complexity O(libraries_to_install(from RAM) *
105-
// library_folder_number(from DISK) *
106-
// library_folder_file_number (from DISK))
107-
//TODO : remove only one version
108-
for _, file := range dirFiles {
109-
if file.IsDir() {
110-
indexFile := filepath.Join(libHome, file.Name(), "library.properties")
111-
_, err = os.Stat(indexFile)
112-
if os.IsNotExist(err) {
113-
fileName := file.Name()
114-
//replacing underscore in foldernames with spaces.
115-
fileName = strings.Replace(fileName, "_", " ", -1)
116-
fileName = strings.Replace(fileName, "-", " v. ", -1)
117-
//I use folder name
118-
libs[fileName] = "Unknown Version"
119-
} else {
120-
// I use library.properties file
121-
content, err := ioutil.ReadFile(indexFile)
122-
if err != nil {
123-
fileName := file.Name()
124-
//replacing underscore in foldernames with spaces.
125-
fileName = strings.Replace(fileName, "_", " ", -1)
126-
fileName = strings.Replace(fileName, "-", " v. ", -1)
127-
//I use folder name
128-
libs[fileName] = "Unknown Version"
129-
continue
130-
}
131-
132-
ini := goini.New()
133-
err = ini.Parse(content, "\n", "=")
134-
if err != nil {
135-
formatter.Print(err)
136-
}
137-
Name, ok := ini.Get("name")
138-
if !ok {
139-
fileName := file.Name()
140-
//replacing underscore in foldernames with spaces.
141-
fileName = strings.Replace(fileName, "_", " ", -1)
142-
fileName = strings.Replace(fileName, "-", " v. ", -1)
143-
//I use folder name
144-
libs[fileName] = "Unknown Version"
145-
continue
146-
}
147-
Version, ok := ini.Get("version")
148-
if !ok {
149-
fileName := file.Name()
150-
//replacing underscore in foldernames with spaces.
151-
fileName = strings.Replace(fileName, "_", " ", -1)
152-
fileName = strings.Replace(fileName, "-", " v. ", -1)
153-
//I use folder name
154-
libs[fileName] = "Unknown Version"
155-
continue
156-
}
157-
libs[Name] = fmt.Sprintf("v.%s", Version)
158-
}
159-
}
160-
}
161-
162-
if len(libs) < 1 {
163-
formatter.PrintErrorMessage("No library installed")
164-
} else {
165-
formatter.Print(output.LibResultsFromMap(libs))
166-
}
16770
}

cmd/arduino_lib.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ var arduinoLibCmd = &cobra.Command{
6262
Run: executeLibCommand,
6363
}
6464

65-
// arduinoLibFlags represents `arduino lib` flags.
66-
var arduinoLibFlags struct {
67-
updateIndex bool
68-
}
69-
7065
// arduinoLibInstallCmd represents the lib install command.
7166
var arduinoLibInstallCmd = &cobra.Command{
7267
Use: "install LIBRARY[@VERSION_NUMBER](S)",

cmd/flags.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
// GlobalFlags represents flags available in all the program.
33+
var GlobalFlags struct {
34+
Verbose int // More time verbose flag is written, the more the Verbose count increases. Represents verbosity level.
35+
Format string // The Output format (e.g. text, json).
36+
}
37+
38+
// rootCmdFlags represent flags available to the root command.
39+
var rootCmdFlags struct {
40+
ConfigFile string // The path of the config file provided by the omonym flag.
41+
}
42+
43+
// arduinoLibFlags represents `arduino lib` flags.
44+
var arduinoLibFlags struct {
45+
updateIndex bool
46+
}
47+
48+
// arduinoCoreFlags represents `arduino core` flags.
49+
var arduinoCoreFlags struct {
50+
updateIndex bool
51+
}

common/paths.go

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ import (
4040
"github.com/bcmi-labs/arduino-cli/task"
4141
)
4242

43+
// GetFolder gets a folder on a path, and creates it if not found.
44+
func GetFolder(folder string, label string) (string, error) {
45+
_, err := os.Stat(folder)
46+
if os.IsNotExist(err) {
47+
formatter.Print(fmt.Sprintf("Cannot find default %s folder, attemping to create it ...", label))
48+
err = os.MkdirAll(folder, 0755)
49+
if err != nil {
50+
formatter.Print("ERROR")
51+
formatter.PrintErrorMessage(fmt.Sprintf("Cannot create %s folder\n", label))
52+
return "", err
53+
}
54+
formatter.Print("OK")
55+
}
56+
return folder, nil
57+
}
58+
4359
// GetDefaultArduinoFolder returns the default data folder for Arduino platform
4460
func GetDefaultArduinoFolder() (string, error) {
4561
var folder string
@@ -55,22 +71,11 @@ func GetDefaultArduinoFolder() (string, error) {
5571
case "darwin":
5672
folder = filepath.Join(usr.HomeDir, "Library", "arduino15")
5773
default:
58-
return "", fmt.Errorf("Unsupported OS: %s", runtime.GOOS)
74+
return folder, fmt.Errorf("Unsupported OS: %s", runtime.GOOS)
5975
}
6076
return GetFolder(folder, "default arduino")
6177
}
6278

63-
// GetDefaultLibFolder get the default folder of downloaded libraries.
64-
func GetDefaultLibFolder() (string, error) {
65-
baseFolder, err := GetDefaultArduinoHomeFolder()
66-
if err != nil {
67-
return "", err
68-
}
69-
70-
libFolder := filepath.Join(baseFolder, "libraries")
71-
return GetFolder(libFolder, "libraries")
72-
}
73-
7479
// GetDefaultArduinoHomeFolder gets the home directory for arduino CLI.
7580
func GetDefaultArduinoHomeFolder() (string, error) {
7681
usr, err := user.Current()
@@ -81,25 +86,34 @@ func GetDefaultArduinoHomeFolder() (string, error) {
8186
return GetFolder(homeFolder, "Arduino home")
8287
}
8388

84-
// GetFolder gets a folder on a path, and creates it if not found.
85-
func GetFolder(folder string, messageName string) (string, error) {
86-
_, err := os.Stat(folder)
87-
if os.IsNotExist(err) {
88-
formatter.Print(fmt.Sprintf("Cannot find default %s folder, attemping to create it ...", messageName))
89-
err = os.MkdirAll(folder, 0755)
90-
if err != nil {
91-
formatter.Print("ERROR")
92-
formatter.PrintErrorMessage(fmt.Sprintf("Cannot create %s folder\n", messageName))
93-
return "", err
94-
}
95-
formatter.Print("OK")
89+
// getDefaultFolder returns the default folder with specified name and label.
90+
func getDefaultFolder(baseFolderFunc func() (string, error), folderName string, folderLabel string) (string, error) {
91+
baseFolder, err := baseFolderFunc()
92+
if err != nil {
93+
return "", err
9694
}
97-
return folder, nil
95+
destFolder := filepath.Join(baseFolder, folderName)
96+
return GetFolder(destFolder, folderLabel)
9897
}
9998

100-
// ExecUpdateIndex is a generic procedure to update an index file.
101-
func ExecUpdateIndex(wrapper task.Wrapper, verbosity int) {
102-
wrapper.Execute(verbosity)
99+
// GetDefaultLibFolder gets the default folder of downloaded libraries.
100+
func GetDefaultLibFolder() (string, error) {
101+
return getDefaultFolder(GetDefaultArduinoHomeFolder, "libraries", "libraries")
102+
}
103+
104+
// GetDefaultPkgFolder gets the default folder of downloaded packages.
105+
func GetDefaultPkgFolder() (string, error) {
106+
return getDefaultFolder(GetDefaultArduinoFolder, "packages", "packages")
107+
}
108+
109+
// GetDefaultCoresFolder gets the default folder of downloaded cores.
110+
func GetDefaultCoresFolder() (string, error) {
111+
return getDefaultFolder(GetDefaultPkgFolder, "hardware", "cores")
112+
}
113+
114+
// GetDefaultToolsFolder gets the default folder of downloaded packages.
115+
func GetDefaultToolsFolder() (string, error) {
116+
return getDefaultFolder(GetDefaultPkgFolder, "tools", "tools")
103117
}
104118

105119
// GetDownloadCacheFolder gets a generic cache folder for downloads.
@@ -113,6 +127,11 @@ func GetDownloadCacheFolder(item string) (string, error) {
113127
return GetFolder(stagingFolder, fmt.Sprint(item, "cache"))
114128
}
115129

130+
// ExecUpdateIndex is a generic procedure to update an index file.
131+
func ExecUpdateIndex(wrapper task.Wrapper, verbosity int) {
132+
wrapper.Execute(verbosity)
133+
}
134+
116135
// IndexPath returns the path of the specified index file.
117136
func IndexPath(fileName string) (string, error) {
118137
baseFolder, err := GetDefaultArduinoFolder()

0 commit comments

Comments
 (0)