-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinfoCommand.go
88 lines (75 loc) · 2.09 KB
/
infoCommand.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"encoding/json"
"fmt"
"github.com/fluepke/esptool/esp32"
"os"
"strconv"
"strings"
)
type DeviceInfo struct {
ChipType string
Revision string
Features []string
MacAddress string
Partitions esp32.PartitionList
}
func (d *DeviceInfo) String() string {
builder := &strings.Builder{}
fmt.Fprint(builder, underline(bold(("Chip Information"))))
fmt.Fprint(builder, "\n")
fmt.Fprintf(builder, "%s: %s\n", bold("Chip Type"), d.ChipType)
fmt.Fprintf(builder, "%s: %s\n", bold("Revision"), d.Revision)
fmt.Fprintf(builder, "%s: %s\n", bold("MAC"), d.MacAddress)
fmt.Fprintf(builder, "%s: %s\n", bold("Features"), strings.Join(d.Features, ", "))
fmt.Fprintln(builder, bold("Partition Table"))
if d.Partitions != nil {
fmt.Fprint(builder, d.Partitions.String())
} else {
fmt.Fprint(builder, "** invalid **")
}
return builder.String()
}
func infoCommand(jsonOutput bool, esp32 *esp32.ESP32ROM) error {
macAddress, err := esp32.GetChipMAC()
if err != nil {
return fmt.Errorf("Could not retrieve MAC address: %s", err.Error())
}
description, err := esp32.GetChipDescription()
if err != nil {
return fmt.Errorf("Could not retrieve chip description: %s", err.Error())
}
features, err := esp32.GetFeatures()
if err != nil {
return fmt.Errorf("Could not retrieve chip features: %s", err.Error())
}
featureList := make([]string, 0)
for feature, status := range features {
if status {
featureList = append(featureList, feature.String())
}
}
deviceInfo := &DeviceInfo{
ChipType: description.ChipType.String(),
Revision: strconv.Itoa(int(description.Revision)),
Features: featureList,
MacAddress: macAddress,
}
partitionList, err := esp32.ReadPartitionList()
if err != nil {
fmt.Printf("Error: %v", err)
}
if err == nil {
deviceInfo.Partitions = partitionList
}
if jsonOutput {
prettyJson, err := json.MarshalIndent(deviceInfo, "", " ")
if err != nil {
return fmt.Errorf("Could not generate JSON outputs: %s", err.Error())
}
_, err = os.Stdout.Write(prettyJson)
return err
}
_, err = fmt.Println(deviceInfo.String())
return err
}