Skip to content

Commit

Permalink
Handle arguments better - check if valid file
Browse files Browse the repository at this point in the history
Signed-off-by: aswinkarthik <[email protected]>
  • Loading branch information
aswinkarthik committed Feb 16, 2019
1 parent 74b2bed commit aca0154
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package cmd

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -53,7 +52,15 @@ Most suitable for csv files created from database tables`,

// Validate args
if len(args) != 2 {
return errors.New("Pass 2 files. Usage: csvdiff <base-csv> <delta-csv>")
return fmt.Errorf("Pass 2 files. Usage: csvdiff <base-csv> <delta-csv>")
}

if err := isValidFile(args[0]); err != nil {
return err
}

if err := isValidFile(args[1]); err != nil {
return err
}

// Validate flags
Expand Down Expand Up @@ -90,6 +97,24 @@ Most suitable for csv files created from database tables`,
},
}

func isValidFile(path string) error {
fileInfo, err := os.Stat(path)

if os.IsNotExist(err) {
return fmt.Errorf("%s does not exist", path)
}

if fileInfo.IsDir() {
return fmt.Errorf("%s is a directory. Please pass a file", path)
}

if err != nil {
return fmt.Errorf("error reading path: %v", err)
}

return nil
}

// 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() {
Expand Down

0 comments on commit aca0154

Please sign in to comment.