-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprinter.go
46 lines (41 loc) · 991 Bytes
/
printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"errors"
"fmt"
"strings"
)
type Printer struct {
IP string `yaml:"ip"`
ID string `yaml:"id"`
Model string `yaml:"model"`
Token string `yaml:"token"`
Sacp bool `yaml:"sacp"`
}
/*
NewPrinter create new printer from response
Snapmaker [email protected]|model:Snapmaker J1|status:IDLE|SACP:1
*/
func NewPrinter(resp []byte) (*Printer, error) {
msg := string(resp)
if !strings.Contains(msg, "|model:") || !strings.Contains(msg, "@") {
return nil, errors.New("invalid response")
}
var (
parts = strings.Split(msg, "|")
id = parts[0][:strings.LastIndex(parts[0], "@")]
ip = parts[0][strings.LastIndex(parts[0], "@")+1:]
model = parts[1][strings.Index(parts[1], ":")+1:]
sacp = strings.Contains(msg, "SACP:1")
)
return &Printer{
IP: ip,
ID: id,
Model: model,
Token: "",
Sacp: sacp,
}, nil
}
/* Name for promptui */
func (p *Printer) String() string {
return fmt.Sprintf("%s@%s - %s", p.ID, p.IP, p.Model)
}