-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.go
145 lines (124 loc) · 2.88 KB
/
flags.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/Rhymond/go-money"
"github.com/spf13/pflag"
"github.com/talbx/csfloat/types"
"os"
)
func ParseFlags(flagset *pflag.FlagSet) (*types.SearchConfig, error) {
config := types.SearchConfig{}
keyfile, _ := flagset.GetString("keyfile")
keyFileMsg := "A key file with your CSFloat API key is required.\nEither provide a file called \"key\" in the same dir from where you run float, or provide the path to your key file like \"--keyfile ../path/to/my/keyfile\""
if keyfile == "" {
_, err := os.ReadFile("key")
if err != nil {
return nil, errors.New(keyFileMsg)
}
config.Keyfile = "key"
} else {
_, err := os.ReadFile(keyfile)
if err != nil {
return nil, errors.New(keyFileMsg)
}
config.Keyfile = keyfile
}
maxi, err := flagset.GetInt("max")
if err != nil {
return nil, err
}
config.MaxPrice = maxi
discount, err := flagset.GetFloat64("discount")
if err != nil {
return nil, err
}
config.MinDiscountPercentage = discount
mini, err := flagset.GetInt("min")
if err != nil {
return nil, err
}
config.MinPrice = mini
cat, err := flagset.GetInt("category")
if err != nil {
return nil, err
}
config.Category = cat
keyword, err := flagset.GetString("keyword")
if err != nil {
return nil, err
}
config.Keyword = keyword
stickers, err := flagset.GetBool("stickers")
if err != nil {
return nil, err
}
config.Stickers = stickers
top, err := flagset.GetInt("top")
if err != nil {
return nil, err
}
config.Top = top
cron, err := flagset.GetBool("cron")
if err != nil {
return nil, err
}
config.Cron = cron
auctions, err := flagset.GetBool("auctions")
if err != nil {
return nil, err
}
config.Auctions = auctions
if config.MinPrice == 0 {
config.MinPrice = maxi / 10
}
fmt.Println(prettyPrint(config))
return &config, nil
}
func prettyPrint(cfg types.SearchConfig) string {
var cat string
if cfg.Category == 1 {
cat = "1 (Normal)"
} else {
cat = "2 (Normal)"
}
var auctions string
if cfg.Auctions {
auctions = "Yes"
} else {
auctions = "No"
}
var stickers string
if cfg.Stickers {
stickers = "Yes"
} else {
stickers = "No"
}
var cron string
if cfg.Cron {
cron = "Yes"
} else {
cron = "No"
}
var keyword string
if cfg.Keyword != "" {
keyword = cfg.Keyword
} else {
keyword = "n/a"
}
printCfg := types.PrintConfig{
MaxPrice: money.New(int64(cfg.MaxPrice), money.USD).Display(),
MinPrice: money.New(int64(cfg.MinPrice), money.USD).Display(),
Category: cat,
Keyfile: cfg.Keyfile,
MinDiscountPercentage: fmt.Sprintf("%v%v", cfg.MinDiscountPercentage, "%"),
Stickers: stickers,
Top: cfg.Top,
Keyword: keyword,
Cron: cron,
Auctions: auctions,
}
s, _ := json.MarshalIndent(printCfg, "", "\t")
return string(s)
}