@@ -45,14 +45,44 @@ import (
4545 "github.com/codeclysm/extract"
4646)
4747
48+ // Tool is a program needed to program a board
4849type Tool struct {
4950 Name string
5051 Version string
5152 Packager string
5253 Path string
5354}
5455
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 {
5686 opts = opts .fill ()
5787
5888 // Download
@@ -75,10 +105,10 @@ func (t *Tool) Download(url, signature string, opts *Opts) error {
75105 h := sha256 .New ()
76106 h .Write (body )
77107 sum := h .Sum (nil )
78- signature = strings .Split (signature , ":" )[1 ]
108+ checksum = strings .Split (checksum , ":" )[1 ]
79109
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" )
82112 }
83113
84114 // Remove folder
@@ -107,34 +137,6 @@ func (t *Tool) Download(url, signature string, opts *Opts) error {
107137 return nil
108138}
109139
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-
138140// Installed returns a list of the installed tools
139141func Installed (opts * Opts ) ([]Tool , error ) {
140142 opts = opts .fill ()
0 commit comments