Skip to content

Commit 12b2eb8

Browse files
sanialescmaglie
authored andcommitted
starting sync and tuning login feature
1 parent f359ac2 commit 12b2eb8

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

cmd/arduino.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,17 +436,21 @@ func executeLoginCommand(cmd *cobra.Command, args []string) {
436436
token, err := authConf.Token(usr, pwd)
437437
if err != nil {
438438
formatter.PrintError(err)
439+
return
439440
}
440441

441-
NetRC.NewMachine("arduino.cc", usr, token.Access, "arduino-cli")
442+
NetRC.RemoveMachine("arduino.cc")
443+
NetRC.NewMachine("arduino.cc", usr, token.Access, token.Refresh)
442444
content, err := NetRC.MarshalText()
443445
if err != nil {
444446
formatter.PrintError(err)
447+
return
445448
}
446449

447450
err = ioutil.WriteFile(netRCFile, content, 0666)
448451
if err != nil {
449452
formatter.PrintError(err)
453+
return
450454
}
451455

452456
formatter.PrintResult(`Successfully logged into the system

cmd/arduino_sketch.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ package cmd
3232
import (
3333
"context"
3434
"encoding/base64"
35-
"encoding/json"
3635
"errors"
3736
"fmt"
3837
"io/ioutil"
@@ -96,21 +95,19 @@ func executeSketchSyncCommand(cmd *cobra.Command, args []string) error {
9695

9796
client := createClient.New(nil)
9897
tok := "Bearer " + bearerToken
99-
resp, err := client.SearchSketches(context.Background(), "", nil, nil, &tok)
98+
resp, err := client.SearchSketches(context.Background(), createClient.SearchSketchesPath(), nil, nil, &tok)
10099
if err != nil {
100+
//formatter.Print(err)
101101
formatter.PrintErrorMessage("Cannot get create sketches, sync failed")
102102
return nil
103103
}
104104
defer resp.Body.Close()
105-
content, err := ioutil.ReadAll(resp.Body)
106-
if err != nil {
107-
formatter.PrintErrorMessage("Cannot get create sketches, sync failed")
108-
return nil
109-
}
110105

111-
var onlineSketches createClient.ArduinoCreateSketches
112-
err = json.Unmarshal(content, resp)
106+
onlineSketches, err := client.DecodeArduinoCreateSketches(resp)
113107
if err != nil {
108+
content, _ := ioutil.ReadAll(resp.Body)
109+
formatter.PrintResult(content)
110+
formatter.Print(err)
114111
formatter.PrintErrorMessage("Cannot unmarshal response from create, sync failed")
115112
return nil
116113
}
@@ -293,7 +290,11 @@ func login() (string, error) {
293290
}
294291

295292
netRCFile := filepath.Join(home, ".netrc")
296-
NetRC, err := netrc.ParseFile(netRCFile)
293+
file, err := os.OpenFile(netRCFile, os.O_CREATE|os.O_RDONLY, 0666)
294+
if err != nil {
295+
return "", err
296+
}
297+
NetRC, err := netrc.Parse(file)
297298
if err != nil {
298299
return "", err
299300
}
@@ -308,7 +309,15 @@ func login() (string, error) {
308309
return "", err
309310
}
310311

311-
arduinoMachine.UpdatePassword(newToken.Access)
312+
var token string
313+
if newToken.TTL != 0 { //we haven't recently requested a valid token, which is in .netrc under "account", so we have to update it
314+
arduinoMachine.UpdatePassword(newToken.Refresh)
315+
arduinoMachine.UpdateAccount(newToken.Access)
316+
token = newToken.Access
317+
} else {
318+
token = arduinoMachine.Account
319+
}
320+
312321
content, err := NetRC.MarshalText()
313322
if err == nil { //serialize new info
314323
err := ioutil.WriteFile(netRCFile, content, 0666)
@@ -318,5 +327,5 @@ func login() (string, error) {
318327
} else if GlobalFlags.Verbose > 0 {
319328
formatter.Print(err.Error())
320329
}
321-
return newToken.Access, nil
330+
return token, nil
322331
}

create_client_helpers/sketches.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ func CreateSketchesPath() string {
4242
return fmt.Sprintf("/create/v1/sketches")
4343
}
4444

45+
const (
46+
devURL = "api-dev.arduino.cc"
47+
prodURL = "api.arduino.cc"
48+
prod = false
49+
)
50+
4551
// CreateSketches Adds a new sketch.
4652
func (c *Client) CreateSketches(ctx context.Context, path string, payload *Sketch, authorization string) (*http.Response, error) {
4753
req, err := c.NewCreateSketchesRequest(ctx, path, payload, authorization)
@@ -62,6 +68,11 @@ func (c *Client) NewCreateSketchesRequest(ctx context.Context, path string, payl
6268
if scheme == "" {
6369
scheme = "http"
6470
}
71+
if prod {
72+
path = prodURL + path
73+
} else {
74+
path = devURL + path
75+
}
6576
u := url.URL{Host: c.Host, Scheme: scheme, Path: path}
6677
req, err := http.NewRequest("PUT", u.String(), &body)
6778
if err != nil {
@@ -95,6 +106,11 @@ func (c *Client) NewDeleteSketchesRequest(ctx context.Context, path string, auth
95106
if scheme == "" {
96107
scheme = "http"
97108
}
109+
if prod {
110+
path = prodURL + path
111+
} else {
112+
path = devURL + path
113+
}
98114
u := url.URL{Host: c.Host, Scheme: scheme, Path: path}
99115
req, err := http.NewRequest("DELETE", u.String(), nil)
100116
if err != nil {
@@ -135,6 +151,11 @@ func (c *Client) NewEditSketchesRequest(ctx context.Context, path string, payloa
135151
if scheme == "" {
136152
scheme = "http"
137153
}
154+
if prod {
155+
path = prodURL + path
156+
} else {
157+
path = devURL + path
158+
}
138159
u := url.URL{Host: c.Host, Scheme: scheme, Path: path}
139160
req, err := http.NewRequest("POST", u.String(), &body)
140161
if err != nil {
@@ -168,6 +189,11 @@ func (c *Client) NewSearchSketchesRequest(ctx context.Context, path string, offs
168189
if scheme == "" {
169190
scheme = "http"
170191
}
192+
if prod {
193+
path = prodURL + path
194+
} else {
195+
path = devURL + path
196+
}
171197
u := url.URL{Host: c.Host, Scheme: scheme, Path: path}
172198
values := u.Query()
173199
if offset != nil {
@@ -211,6 +237,11 @@ func (c *Client) NewShowSketchesRequest(ctx context.Context, path string, author
211237
if scheme == "" {
212238
scheme = "http"
213239
}
240+
if prod {
241+
path = prodURL + path
242+
} else {
243+
path = devURL + path
244+
}
214245
u := url.URL{Host: c.Host, Scheme: scheme, Path: path}
215246
req, err := http.NewRequest("GET", u.String(), nil)
216247
if err != nil {

create_client_helpers/user_types.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
package createClient
3131

3232
import (
33+
"encoding/base64"
34+
"fmt"
35+
3336
"github.com/bcmi-labs/arduino-modules/sketches"
3437
)
3538

@@ -63,7 +66,9 @@ type Sketch struct {
6366
}
6467

6568
func ConvertFrom(sketch sketches.Sketch) *Sketch {
66-
ino := string(sketch.Ino.Data)
69+
var temp []byte
70+
base64.StdEncoding.Encode(temp, sketch.Ino.Data)
71+
ino := string(temp)
6772
ret := Sketch{
6873
Name: sketch.Name,
6974
Folder: &sketch.Path,
@@ -78,12 +83,15 @@ func ConvertFrom(sketch sketches.Sketch) *Sketch {
7883
}
7984
ret.Files = make([]*File, len(sketch.Files))
8085
for i, f := range sketch.Files {
81-
data := string(f.Data)
86+
base64.StdEncoding.Encode(temp, f.Data)
87+
data := string(temp)
8288
ret.Files[i] = &File{
8389
Data: &data,
8490
Name: f.Name,
8591
}
8692
}
93+
fmt.Println(sketch.Metadata.CPU.Fqbn)
94+
fmt.Println(*ret.Metadata.CPU.Fqbn)
8795
return &ret
8896
}
8997

0 commit comments

Comments
 (0)