-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrack_cmd.go
50 lines (43 loc) · 1.21 KB
/
track_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
package main
import (
"encoding/json"
"fmt"
"github.com/alufers/inpost-cli/trackingapi"
"github.com/urfave/cli/v2"
)
var TrackCmd = &cli.Command{
Name: "track",
Usage: "[--format=text|json] <SHIPMENT_NUMBER> -- show tracking",
Description: "Shows detailed tracking information about the parcel",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
DefaultText: "text",
Value: "text",
Usage: "The output format: json or text",
},
},
Action: func(c *cli.Context) error {
if err := RefreshAllTokens(c); err != nil {
return fmt.Errorf("failed to refresh token: %v", err)
}
shipmentNumber, _, err := resolveShipmentNumber(c, c.Args().Get(0))
if err != nil {
return fmt.Errorf("failed to resolve shipment number: %w", err)
}
data, err := trackingapi.GetTrackingData(shipmentNumber)
if err != nil {
return fmt.Errorf("failed to get tracking data: %w", err)
}
if c.String("format") == "json" {
s, _ := json.MarshalIndent(data, "", " ")
fmt.Print(string(s))
} else {
for _, p := range data.TrackingDetails {
fmt.Printf("%v %v (%v)\n", p.Datetime.String(), p.Status, p.OriginStatus)
}
}
return nil
},
}