Skip to content

Commit a1bb303

Browse files
committed
add packager to coreInfo, cleanup & fixes
1 parent c2c32d0 commit a1bb303

File tree

4 files changed

+24
-58
lines changed

4 files changed

+24
-58
lines changed

cmd/compile.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cmd
66

77
import (
88
"encoding/json"
9+
"os"
910
"os/exec"
1011
"strings"
1112

@@ -34,8 +35,9 @@ type BuilderResult struct {
3435

3536
// Info contains information regarding the library or the core used during the compile process
3637
type Info struct {
37-
Name string `json:"name"`
38-
Version string `json:"version"`
38+
Packager string `json:"packager,omitempty"`
39+
Name string `json:"name"`
40+
Version string `json:"version"`
3941
}
4042

4143
// returnJson contains information regarding the core and libraries used during the compile process
@@ -48,11 +50,11 @@ type ReturnJson struct {
4850
var compileCmd = &cobra.Command{
4951
Use: "compile",
5052
Short: "Compiles Arduino sketches.",
51-
Long: `Compiles Arduino sketches outputting an object file and a json file
52-
The json contains information regarding libraries and core to use to build the full sketch`,
53-
// Example: , // TODO
54-
Args: cobra.ExactArgs(1), // the path of the sketch to build
55-
Run: compileSketch,
53+
Long: `Compiles Arduino sketches outputting an object file and a json file in a build directory
54+
The json contains information regarding libraries and core to use in order to build the sketch`,
55+
Example: os.Args[0] + `compile -b arduino:avr:uno /home/umberto/Arduino/Blink`,
56+
Args: cobra.ExactArgs(1), // the path of the sketch to build
57+
Run: compileSketch,
5658
}
5759

5860
func init() {
@@ -115,15 +117,15 @@ func compileSketch(cmd *cobra.Command, args []string) {
115117
}
116118

117119
// parseOutput function takes cmdOutToParse as argument,
118-
// cmdOutToParse is the output captured from the command run
119-
// the function extract the paths of the .o files and
120-
// a ReturnJson object
120+
// cmdOutToParse is the json output captured from the command run
121+
// the function extracts and returns the paths of the .o files
122+
// (generated during the compile phase) and a ReturnJson object
121123
func parseOutput(cmdOutToParse []byte) ([]*paths.Path, *ReturnJson) {
122124
err := json.Unmarshal(cmdOutToParse, &compileOutput)
123125
if err != nil {
124126
logrus.Fatal(err)
125127
} else if !compileOutput.Success {
126-
logrus.Fatalf("sketch compile was not successful %s", compileOutput.CompilerErr)
128+
logrus.Fatalf("sketch compile was not successful: %s", compileOutput.CompilerErr)
127129
}
128130

129131
compilerOutLines := strings.Split(compileOutput.CompilerOut, "\n")
@@ -144,7 +146,7 @@ func parseOutput(cmdOutToParse []byte) ([]*paths.Path, *ReturnJson) {
144146
sketchFilesPaths, err := sketchDir.ReadDir()
145147
if err != nil {
146148
logrus.Fatal(err)
147-
} else if sketchFilesPaths == nil {
149+
} else if len(sketchFilesPaths) == 0 {
148150
logrus.Fatalf("empty directory: %s", sketchDir)
149151
}
150152
var returnObjectFilesList []*paths.Path
@@ -159,25 +161,23 @@ func parseOutput(cmdOutToParse []byte) ([]*paths.Path, *ReturnJson) {
159161
LibsInfo: compileOutput.BuilderResult.UsedLibraries,
160162
}
161163

162-
// TODO add also the packager -> maybe ParseReference could be used from the cli
163-
// TODO core could be calculated from fqbn
164-
165164
return returnObjectFilesList, &returnJson
166165
}
167166

168-
// parseCoreLine takes the line containig info regarding the core and
169-
// returns a coreInfo object
167+
// parseCoreLine takes the line containing info regarding the core and
168+
// returns a Info object
170169
func parseCoreLine(coreLine string) *Info {
171170
words := strings.Split(coreLine, " ")
172171
strCorePath := words[len(words)-1] // last string has the path of the core
173-
// maybe check if the path is legit before and logrus.Fatal if not
174172
corePath := paths.New(strCorePath)
175-
version := corePath.Base()
176-
name := corePath.Parent().Base()
177-
logrus.Debugf("core name: %s, core version: %s", name, version)
173+
if !corePath.Exist() {
174+
logrus.Fatalf("the path of the core does not exists: %s", corePath)
175+
}
176+
corePathParents := corePath.Parents()
178177
coreInfo := &Info{
179-
Name: name,
180-
Version: version,
178+
Packager: corePathParents[3].Base(),
179+
Name: corePathParents[1].Base(),
180+
Version: corePathParents[0].Base(),
181181
}
182182
return coreInfo
183183
}

cmd/root.go

+1-26
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,10 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
14-
1513
// rootCmd represents the base command when called without any subcommands
1614
var rootCmd = &cobra.Command{
1715
Use: "cslt-tool",
18-
Short: "A brief description of your application",
19-
Long: `A longer description that spans multiple lines and likely contains
20-
examples and usage of using your application. For example:
21-
22-
Cobra is a CLI library for Go that empowers applications.
23-
This application is a tool to generate the needed files
24-
to quickly create a Cobra application.`,
25-
// Uncomment the following line if your bare application
26-
// has an action associated with it:
27-
// Run: func(cmd *cobra.Command, args []string) { },
16+
Short: "cslt-tool is a command-line tool that uses the Arduino CLI to generate objectfiles and a json file with info regarding core and libraries used",
2817
}
2918

3019
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -35,17 +24,3 @@ func Execute() {
3524
os.Exit(1)
3625
}
3726
}
38-
39-
func init() {
40-
// Here you will define your flags and configuration settings.
41-
// Cobra supports persistent flags, which, if defined here,
42-
// will be global for your application.
43-
44-
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cslt-tool.yaml)")
45-
46-
// Cobra also supports local flags, which will only run
47-
// when this action is called directly.
48-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
49-
}
50-
51-

go.mod

-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@ require (
88
)
99

1010
require (
11-
github.com/davecgh/go-spew v1.1.1 // indirect
12-
github.com/kr/pretty v0.2.1 // indirect
1311
github.com/pkg/errors v0.9.1 // indirect
14-
github.com/pmezard/go-difflib v1.0.0 // indirect
1512
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
16-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1713
)
1814

1915
require (
2016
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2117
github.com/sirupsen/logrus v1.8.1
2218
github.com/spf13/pflag v1.0.5 // indirect
23-
github.com/stretchr/testify v1.7.0
2419
)

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,7 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
241241
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
242242
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
243243
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
244-
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
245-
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
246244
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
247-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
248245
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
249246
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
250247
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
@@ -748,7 +745,6 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
748745
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
749746
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
750747
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
751-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
752748
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
753749
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
754750
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

0 commit comments

Comments
 (0)