-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
103 lines (87 loc) · 2.09 KB
/
command.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
package main
import (
"fmt"
"os"
"sort"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
var (
usage = `Specify a command to execute:
- get: display stock price in a table
- get-all: display watchlist in a table
- list: editable list of all tickers in watchlist
- add: add ticker to watchlist`
)
func executeCommand(command string, args []string) error {
switch command {
case "get":
tickers := args[0]
getTickersPrice(tickers)
return nil
case "get-all":
getWatchlistPrice()
return nil
case "list":
displayWatchlist()
return nil
case "add":
tickers := args[0]
for _, ticker := range strings.Split(tickers, ",") {
addTickerToWatchlist(ticker)
}
getWatchlistPrice()
return nil
default:
return fmt.Errorf("invalid command: '%s'\n\n%s\n", command, usage)
}
}
func getTickersPrice(tickers string) {
tickersArray := strings.Split(tickers, ",")
var stocks []ChartResponse
for _, ticker := range tickersArray {
res, err := fetchPrice(ticker)
if err != nil {
fmt.Printf("Error fetching")
}
stocks = append(stocks, res)
}
getTable(stocks)
}
func getWatchlistPrice() {
var watchList = getWatchList()
var stocks []ChartResponse
for _, ticker := range watchList {
res, err := fetchPrice(ticker)
if err != nil {
fmt.Printf("Error fetching")
}
stocks = append(stocks, res)
}
getTable(stocks)
}
func displayWatchlist() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}
func addTickerToWatchlist(ticker string) bool {
var watchList = getWatchList()
var found = strings.Contains(strings.Join(watchList, ","), ticker)
if found {
fmt.Printf("%s is already in the watchlist\n", ticker)
return false
}
watchList = append(watchList, ticker)
sort.Strings(watchList)
var fileContent = strings.Join(watchList, ",")
var operation = updateWatchList(fileContent)
if !operation {
fmt.Printf("Something went wrong. %s has not been added to the watchlist !\n", ticker)
return false
}
fmt.Printf("%s has been added to the watchlist !\n", ticker)
return true
}