Skip to content

Commit

Permalink
refactor: updated function signature of validate
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Feb 14, 2021
1 parent 20ec805 commit 708cb9d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
rootCmd = &cobra.Command{
Use: "ticker",
Short: "Terminal stock ticker and stock gain/loss tracker",
Args: cli.Validate(dep, &ctx, &options, err),
Args: cli.Validate(&ctx, &options, &err),
Run: cli.Run(ui.Start(&dep, &ctx)),
}
)
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func Run(uiStartFn func() error) func(*cobra.Command, []string) {
}
}

func Validate(dep Dependencies, ctx *Context, options *Options, prevErr error) func(*cobra.Command, []string) error {
func Validate(ctx *Context, options *Options, prevErr *error) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {

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

if len(ctx.Config.Watchlist) == 0 && len(options.Watchlist) == 0 && len(ctx.Config.Lots) == 0 {
Expand Down
7 changes: 4 additions & 3 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ var _ = Describe("Cli", func() {

When("a deferred error is passed in", func() {
It("validation fails", func() {
outputErr := Validate(c.Dependencies{}, &c.Context{}, &cli.Options{}, errors.New("some config error"))(&cobra.Command{}, []string{})
inputErr := errors.New("some config error")
outputErr := Validate(&c.Context{}, &cli.Options{}, &inputErr)(&cobra.Command{}, []string{})
Expect(outputErr).To(MatchError("some config error"))
})
})
Expand All @@ -393,7 +394,7 @@ var _ = Describe("Cli", func() {
When("there is no watchlist in the config file and no watchlist cli argument", func() {
It("should return an error", func() {
options.Watchlist = ""
outputErr := Validate(dep, &ctx, &options, nil)(&cobra.Command{}, []string{})
outputErr := Validate(&ctx, &options, nil)(&cobra.Command{}, []string{})
Expect(outputErr).To(MatchError("Invalid config: No watchlist provided"))

})
Expand All @@ -410,7 +411,7 @@ var _ = Describe("Cli", func() {
},
},
}
outputErr := Validate(dep, &ctx, &options, nil)(&cobra.Command{}, []string{})
outputErr := Validate(&ctx, &options, nil)(&cobra.Command{}, []string{})
Expect(outputErr).NotTo(HaveOccurred())
})
})
Expand Down

0 comments on commit 708cb9d

Please sign in to comment.