@@ -45,14 +45,44 @@ import (
45
45
"github.com/codeclysm/extract"
46
46
)
47
47
48
+ // Tool is a program needed to program a board
48
49
type Tool struct {
49
50
Name string
50
51
Version string
51
52
Packager string
52
53
Path string
53
54
}
54
55
55
- func (t * Tool ) Download (url , signature string , opts * Opts ) error {
56
+ // Opts contain options to pass to the Download function
57
+ type Opts struct {
58
+ Location string
59
+ Client * http.Client
60
+ }
61
+
62
+ // fill fills itself with default values
63
+ func (o * Opts ) fill () * Opts {
64
+ if o == nil {
65
+ o = & Opts {}
66
+ }
67
+
68
+ if o .Location == "" {
69
+ usr , _ := user .Current ()
70
+ o .Location = filepath .Join (usr .HomeDir , ".arduino-create" )
71
+ }
72
+
73
+ if o .Client == nil {
74
+ o .Client = & http.Client {
75
+ Timeout : 10 * time .Second ,
76
+ }
77
+ }
78
+
79
+ fmt .Println (o )
80
+
81
+ return o
82
+ }
83
+
84
+ // Download unpacks a tool on the system, checking that the checksum matches
85
+ func (t * Tool ) Download (url , checksum string , opts * Opts ) error {
56
86
opts = opts .fill ()
57
87
58
88
// Download
@@ -75,10 +105,10 @@ func (t *Tool) Download(url, signature string, opts *Opts) error {
75
105
h := sha256 .New ()
76
106
h .Write (body )
77
107
sum := h .Sum (nil )
78
- signature = strings .Split (signature , ":" )[1 ]
108
+ checksum = strings .Split (checksum , ":" )[1 ]
79
109
80
- if string (hex .EncodeToString (sum )) != signature {
81
- return errors .New ("signature doesn't match" )
110
+ if string (hex .EncodeToString (sum )) != checksum {
111
+ return errors .New ("checksum doesn't match" )
82
112
}
83
113
84
114
// Remove folder
@@ -107,34 +137,6 @@ func (t *Tool) Download(url, signature string, opts *Opts) error {
107
137
return nil
108
138
}
109
139
110
- // Opts contain options to pass to the Download function
111
- type Opts struct {
112
- Location string
113
- Client * http.Client
114
- }
115
-
116
- // fill fills itself with default values
117
- func (o * Opts ) fill () * Opts {
118
- if o == nil {
119
- o = & Opts {}
120
- }
121
-
122
- if o .Location == "" {
123
- usr , _ := user .Current ()
124
- o .Location = filepath .Join (usr .HomeDir , ".arduino-create" )
125
- }
126
-
127
- if o .Client == nil {
128
- o .Client = & http.Client {
129
- Timeout : 10 * time .Second ,
130
- }
131
- }
132
-
133
- fmt .Println (o )
134
-
135
- return o
136
- }
137
-
138
140
// Installed returns a list of the installed tools
139
141
func Installed (opts * Opts ) ([]Tool , error ) {
140
142
opts = opts .fill ()
0 commit comments