-
Notifications
You must be signed in to change notification settings - Fork 5
/
parcel_info_cmd.go
157 lines (141 loc) · 4.21 KB
/
parcel_info_cmd.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/alufers/inpost-cli/swagger"
"github.com/antihax/optional"
"github.com/mdp/qrterminal/v3"
"github.com/urfave/cli/v2"
"rsc.io/qr"
terminal "github.com/wayneashleyberry/terminal-dimensions"
)
var ParcelInfoCmd = &cli.Command{
Name: "parcel-info",
Aliases: []string{"qr", "info"},
Description: "shows detailed information about a parcel, including a QR code",
Usage: "[--format=text|json] [--qr H|M|L] <SHIPMENT_NUMBER> -- show info about a package and a QR code",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
DefaultText: "text",
Value: "text",
Usage: "The output format: json or text (with a QR code)",
},
&cli.StringFlag{
Name: "qr",
Aliases: []string{"q"},
DefaultText: "H",
Value: "H",
Usage: "QR code quality settings: H, M, L, none (no QR code)",
},
},
Action: func(c *cli.Context) error {
level, err := stringToQRLevel(c.String("qr"))
if err != nil {
return err
}
if err := RefreshAllTokens(c); err != nil {
return fmt.Errorf("failed to refresh token: %v", err)
}
shipmentNumber, apiClient, err := resolveShipmentNumber(c, c.Args().Get(0))
if err != nil {
return fmt.Errorf("failed to resolve shipment number: %w", err)
}
parcels, _, err := apiClient.DefaultApi.V1ParcelsGet(c.Context, &swagger.DefaultApiV1ParcelsGetOpts{
ShipmentNumbers: optional.NewString(shipmentNumber),
})
if err != nil {
return fmt.Errorf("failed to get parcels: %v", err)
}
if len(parcels) <= 0 {
return fmt.Errorf("no such parcel found")
}
p := parcels[0]
infoBuf := &bytes.Buffer{}
switch c.String("format") {
case "text":
fmt.Fprintf(infoBuf, "Shipment number: %v\n", p.ShipmentNumber)
fmt.Fprintf(infoBuf, "Sender name: %v\n", p.SenderName)
fmt.Fprintf(infoBuf, "Status: %v\n", p.Status)
fmt.Fprintf(infoBuf, "Recipient phone number: %v\n", p.PhoneNumber)
if p.OpenCode != "" {
fmt.Fprintf(infoBuf, "Open code: %v\n", p.OpenCode)
}
if !p.ExpiryDate.IsZero() {
fmt.Fprintf(infoBuf, "Expiry time: %v\n", formatDuration(time.Until(p.ExpiryDate)))
}
if p.PickupPoint != nil {
fmt.Fprintf(infoBuf, "Pickup point:\n")
if p.PickupPoint.AddressDetails != nil {
fmt.Fprintf(infoBuf, " Address: %v%v%v,\n",
p.PickupPoint.AddressDetails.Street,
prependSpaceIfNotEmpty(p.PickupPoint.AddressDetails.BuildingNumber),
prependSpaceIfNotEmpty(p.PickupPoint.AddressDetails.FlatNumber),
)
fmt.Fprintf(infoBuf, " City: %v, %v\n",
p.PickupPoint.AddressDetails.City,
p.PickupPoint.AddressDetails.Province,
)
fmt.Fprintf(infoBuf, " Location description: %v\n", p.PickupPoint.LocationDescription)
}
}
if p.OpenCode != "" {
fmt.Fprintf(infoBuf, "QR code URL: https://inpost.pl/%v/code/%v\n", p.PhoneNumber, p.OpenCode)
}
qrBuf := &bytes.Buffer{}
var qrWidth int
if p.QrCode != "" && level != nil {
qrterminal.GenerateWithConfig(p.QrCode, qrterminal.Config{
Level: *level,
Writer: qrBuf,
BlackChar: qrterminal.BLACK,
WhiteChar: qrterminal.WHITE,
QuietZone: 2,
}) // inpost uses HIGH error correction
code, err := qr.Encode(p.QrCode, *level)
if err == nil {
qrWidth = code.Size
}
}
qrLines := strings.Split(qrBuf.String(), "\n")
infoLines := strings.Split(infoBuf.String(), "\n")
var infoWidth int
for _, l := range infoLines {
if len(l) > infoWidth {
infoWidth = len(l)
}
}
termWidth, _ := terminal.Width()
if infoWidth+qrWidth+2 > int(termWidth) {
// print vertically
for _, line := range qrLines {
fmt.Println(line)
}
for _, line := range infoLines {
fmt.Println(line)
}
} else {
for i := 0; i < len(qrLines) || i < len(infoLines); i++ {
if i < len(qrLines) {
fmt.Print(qrLines[i])
}
if i < len(qrLines) && i < len(infoLines) {
fmt.Print(" ")
}
if i < len(infoLines) {
fmt.Print(infoLines[i])
}
fmt.Print("\n")
}
}
case "json":
s, _ := json.MarshalIndent(p, "", " ")
fmt.Print(string(s))
}
return nil
},
}