Skip to content

Commit

Permalink
fix: only execute network requests when invoking cli commands requiri…
Browse files Browse the repository at this point in the history
…ng remote data
  • Loading branch information
achannarasappa committed May 31, 2024
1 parent 69ef96b commit f1ec587
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 119 deletions.
24 changes: 17 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ var (
configPath string
dep c.Dependencies
ctx c.Context
config c.Config
options cli.Options
optionsPrint print.Options
err error
rootCmd = &cobra.Command{
Version: Version,
Use: "ticker",
Short: "Terminal stock ticker and stock gain/loss tracker",
Args: cli.Validate(&ctx, &options, &err),
PreRun: initContext,
Args: cli.Validate(&config, &options, &err),
Run: cli.Run(ui.Start(&dep, &ctx)),
}
printCmd = &cobra.Command{
Use: "print",
Short: "Prints holdings",
Args: cli.Validate(&ctx, &options, &err),
Run: print.Run(&dep, &ctx, &optionsPrint),
Use: "print",
Short: "Prints holdings",
PreRun: initContext,
Args: cli.Validate(&config, &options, &err),
Run: print.Run(&dep, &ctx, &optionsPrint),
}
)

Expand Down Expand Up @@ -63,14 +66,21 @@ func init() { //nolint: gochecknoinits
}

func initConfig() {
dep, err = cli.GetDependencies()

dep = cli.GetDependencies()

config, err = cli.GetConfig(dep, configPath, options)

if err != nil {
fmt.Println(err)
os.Exit(1)
}

ctx, err = cli.GetContext(dep, options, configPath)
}

func initContext(_ *cobra.Command, _ []string) {

ctx, err = cli.GetContext(dep, config)

if err != nil {
fmt.Println(err)
Expand Down
37 changes: 17 additions & 20 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,56 +50,47 @@ func Run(uiStartFn func() error) func(*cobra.Command, []string) {
}

// Validate checks whether config is valid and returns an error if invalid or if an error was generated earlier
func Validate(ctx *c.Context, options *Options, prevErr *error) func(*cobra.Command, []string) error {
func Validate(config *c.Config, options *Options, prevErr *error) func(*cobra.Command, []string) error {
return func(_ *cobra.Command, _ []string) error {

if prevErr != nil && *prevErr != nil {
return *prevErr
}

if len(ctx.Config.Watchlist) == 0 && len(options.Watchlist) == 0 && len(ctx.Config.Lots) == 0 && len(ctx.Config.AssetGroup) == 0 {
if len(config.Watchlist) == 0 && len(options.Watchlist) == 0 && len(config.Lots) == 0 && len(config.AssetGroup) == 0 {
return errors.New("invalid config: No watchlist provided") //nolint:goerr113
}

return nil
}
}

func GetDependencies() (c.Dependencies, error) {

client := yahooClient.New(resty.New(), resty.New())
err := yahooClient.RefreshSession(client, resty.New())

if err != nil {
return c.Dependencies{}, err
}
func GetDependencies() c.Dependencies {

return c.Dependencies{
Fs: afero.NewOsFs(),
HttpClients: c.DependenciesHttpClients{
Default: resty.New(),
Yahoo: client,
Yahoo: yahooClient.New(resty.New(), resty.New()),
},
}, nil
}

}

// GetContext builds the context from the config and reference data
func GetContext(d c.Dependencies, options Options, configPath string) (c.Context, error) {
func GetContext(d c.Dependencies, config c.Config) (c.Context, error) {
var (
reference c.Reference
config c.Config
groups []c.AssetGroup
err error
)

config, err = readConfig(d.Fs, configPath)
err = yahooClient.RefreshSession(d.HttpClients.Yahoo, resty.New())

if err != nil {
return c.Context{}, err
}

config = getConfig(config, options, &d.HttpClients)
groups, err = getGroups(config, *d.HttpClients.Default)

if err != nil {
Expand Down Expand Up @@ -152,15 +143,21 @@ func getReference(config c.Config, assetGroups []c.AssetGroup, client resty.Clie

}

func getConfig(config c.Config, options Options, httpClients *c.DependenciesHttpClients) c.Config {
func GetConfig(dep c.Dependencies, configPath string, options Options) (c.Config, error) {

config, err := readConfig(dep.Fs, configPath)

if err != nil {
return c.Config{}, err
}

if len(options.Watchlist) != 0 {
config.Watchlist = strings.Split(strings.ReplaceAll(options.Watchlist, " ", ""), ",")
}

if len(config.Proxy) > 0 {
httpClients.Default.SetProxy(config.Proxy)
httpClients.Yahoo.SetProxy(config.Proxy)
dep.HttpClients.Default.SetProxy(config.Proxy)
dep.HttpClients.Yahoo.SetProxy(config.Proxy)
}

config.RefreshInterval = getRefreshInterval(options.RefreshInterval, config.RefreshInterval)
Expand All @@ -172,7 +169,7 @@ func getConfig(config c.Config, options Options, httpClients *c.DependenciesHttp
config.Proxy = getStringOption(options.Proxy, config.Proxy)
config.Sort = getStringOption(options.Sort, config.Sort)

return config
return config, nil
}

func getConfigPath(fs afero.Fs, configPathOption string) (string, error) {
Expand Down
Loading

0 comments on commit f1ec587

Please sign in to comment.