-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (49 loc) · 1.03 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/piquette/finance-go/equity"
)
type fetchArgs struct {
Ticker string `json:"ticker,omitempty"`
}
func main() {
if len(os.Args) < 2 {
exit("please provide an action")
}
switch os.Args[1] {
case "lifecycle":
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
case "fetch":
if len(os.Args) < 3 {
exit("please provide args to fetch")
}
fetchArgs := &fetchArgs{}
if err := json.Unmarshal([]byte(os.Args[2]), &fetchArgs); err != nil {
exit("please provide valid args json")
}
if fetchArgs.Ticker == "" {
exit("please provide a ticker in the args json")
}
quote, err := equity.Get(fetchArgs.Ticker)
exitOn(err)
fmt.Println(quote.RegularMarketPrice)
default:
exit("please provide 'fetch' as the action")
}
}
func exit(reason string) {
exitOn(errors.New(reason))
}
func exitOn(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}