This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
148 lines (119 loc) · 4.9 KB
/
root.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
/*
Copyright 2022
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"github.com/penny-vault/import-sa-quant-rank/backblaze"
"github.com/penny-vault/import-sa-quant-rank/sa"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var cfgFile string
var test bool
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "import-sa-quant-rank",
Short: "Import JSON ratings downloaded from Seeking Alpha's stock screener",
// Long: ``,
Run: func(cmd *cobra.Command, args []string) {
log.Info().Bool("Test", test).Msg("Download SeekingAlpha ratings")
ratings, err := sa.Download()
if err != nil {
log.Error().Err(err).Msg("error downloading ticker metrics")
os.Exit(1)
}
sa.ValidateRatings(ratings)
if !test {
sa.EnrichWithFigi(ratings)
sa.SaveToDB(ratings)
}
// Save data as parquet to a temporary directory
tmpdir, err := os.MkdirTemp(os.TempDir(), "import-sa")
if err != nil {
log.Error().Err(err).Msg("could not create tempdir")
os.Exit(1)
}
parquetFn := fmt.Sprintf("%s/sa-%s.parquet", tmpdir, ratings[0].Date.Format("20060102"))
log.Info().Str("FileName", parquetFn).Msg("writing seeking alpha ratings data to parquet")
sa.SaveToParquet(ratings, parquetFn)
// Upload to backblaze
if !test {
backblaze.UploadToBackBlaze(parquetFn, viper.GetString("backblaze.bucket"), ratings[0].Date.Format("2006"))
}
// Cleanup after ourselves
os.RemoveAll(tmpdir)
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
log.Error().Msg(err.Error())
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(initLog)
// Persistent flags that are global to application
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.import-sa-quant-rank.toml)")
rootCmd.PersistentFlags().Bool("log-json", false, "print logs as json to stderr")
viper.BindPFlag("log.json", rootCmd.PersistentFlags().Lookup("log-json"))
rootCmd.PersistentFlags().Bool("hide-progress", false, "hide progress bar")
viper.BindPFlag("display.hide_progress", rootCmd.PersistentFlags().Lookup("hide-progress"))
rootCmd.Flags().BoolVarP(&test, "test", "t", false, "run in test mode and do not save results to database or upload to backblaze")
rootCmd.PersistentFlags().String("state_file", "state.json", "state file")
viper.BindPFlag("playwright.state_file", rootCmd.PersistentFlags().Lookup("state_file"))
// Add flags
rootCmd.Flags().StringP("database_url", "d", "host=localhost port=5432", "DSN for database connection")
viper.BindPFlag("database.url", rootCmd.Flags().Lookup("database_url"))
rootCmd.Flags().Uint32P("limit", "l", 0, "limit results to N")
viper.BindPFlag("limit", rootCmd.Flags().Lookup("limit"))
rootCmd.Flags().StringP("backblaze_bucket", "b", "seeking-alpha", "Backblaze bucket name")
viper.BindPFlag("backblaze.bucket", rootCmd.Flags().Lookup("backblaze_bucket"))
rootCmd.Flags().String("backblaze_application_id", "<not-set>", "Backblaze application id")
viper.BindPFlag("backblaze.application_id", rootCmd.Flags().Lookup("backblaze_application_id"))
rootCmd.Flags().String("backblaze_application_key", "<not-set>", "Backblaze application key")
viper.BindPFlag("backblaze.application_key", rootCmd.Flags().Lookup("backblaze_application_key"))
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".import-sa-quant-rank" (without extension).
viper.AddConfigPath("/etc") // path to look for the config file in
viper.AddConfigPath(fmt.Sprintf("%s/.config", home))
viper.AddConfigPath(".")
viper.SetConfigType("toml")
viper.SetConfigName("import-sa-quant-rank")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
func initLog() {
if !viper.GetBool("log.json") {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
}