-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrackpkg.go
166 lines (127 loc) · 3.45 KB
/
trackpkg.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
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli"
"os"
"strings"
)
const trackingRepo = "~/.trackingrepo"
var trackingRepoPath, _ = homedir.Expand(trackingRepo)
var supportedCarriers = []Carrier{Fedex{}, Ups{}, Usps{}}
var repository = ShipmentsRepository{Path: trackingRepoPath}
var commands = []cli.Command{
{
Name: "add",
Usage: "adds new tracking number",
ArgsUsage: "<tracking_number> <description>",
Action: Add,
BashComplete: TrackpkgAutocomplete,
},
{
Name: "remove",
Usage: "removes tracking number",
ArgsUsage: "<tracking_number> | <item_number>",
Action: Remove,
BashComplete: TrackpkgAutocomplete,
},
{
Name: "update",
Usage: "updates status of all tracked numbers",
Action: Update,
BashComplete: TrackpkgAutocomplete,
},
{
Name: "list",
Usage: "lists status of a given or all tracked number",
ArgsUsage: "[<tracking_number> | <item_number>]",
Action: List,
BashComplete: TrackpkgAutocomplete,
},
{
Name: "detail",
Usage: "prints detail tracking status of a given or all tracked numbers",
ArgsUsage: "[<tracking_number> | <item_number>]",
Action: Detail,
BashComplete: TrackpkgAutocomplete,
},
{
Name: "clean",
Usage: "removes delivered packages",
Action: Clean,
BashComplete: TrackpkgAutocomplete,
},
}
// Add adds new tracking number
func Add(context *cli.Context) error {
trackingNumber := context.Args().First()
description := strings.Join(context.Args().Tail(), " ")
if trackingNumber == "" {
return nil
}
shipments, _ := repository.load()
shipment := Shipment{TrackingNumber: trackingNumber, Description: description, Delivered: false}
shipments.addItem(shipment)
err := repository.save(shipments)
return err
}
// Remove removes tracking number
func Remove(context *cli.Context) error {
trackingNumber := context.Args().First()
if trackingNumber == "" {
return nil
}
shipments, _ := repository.load()
shipments.removeItem(trackingNumber)
err := repository.save(shipments)
return err
}
// Update updates status of all tracked numbers
func Update(context *cli.Context) error {
shipments, err := repository.load()
shipments.updateTracking(supportedCarriers)
err = repository.save(shipments)
return err
}
// List lists status of a given or all tracked number
func List(context *cli.Context) error {
shipments, err := repository.load()
shipments.list("", false)
return err
}
// Detail prints detail tracking status of a given or all tracked numbers
func Detail(context *cli.Context) error {
trackingNumber := context.Args().First()
shipments, err := repository.load()
shipments.list(trackingNumber, true)
return err
}
// Clean removes delivered packages
func Clean(context *cli.Context) error {
shipments, err := repository.load()
shipments.removeDelivered()
err = repository.save(shipments)
return err
}
// TrackpkgAutocomplete bash autocomplete
func TrackpkgAutocomplete(context *cli.Context) {
}
func main() {
app := cli.NewApp()
app.Name = "Trackpkg"
app.Usage = "Track Shipment Packages"
app.Version = "0.1"
// app.Authors = []cli.Author{
// cli.Author{
// Name: "Jwalanta Shrestha",
// Email: "[email protected]",
// },
// }
app.EnableBashCompletion = true
app.Commands = commands
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}