Skip to content

Commit

Permalink
Merge pull request #46 from ysim/testing
Browse files Browse the repository at this point in the history
Fix tests and improve logging
  • Loading branch information
ysim committed Jul 30, 2020
2 parents c13c777 + b2b8269 commit 438c26b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
18 changes: 8 additions & 10 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"strings"
"text/template"
Expand Down Expand Up @@ -55,8 +56,8 @@ func validateFields(fieldFlags []string) (map[string]interface{}, error) {

func validateNewRecipe(filename string) string {
if filename == "" {
fmt.Println("You must provide at least a filename in order to create a new recipe.")
os.Exit(1)
errMsg := "You must provide at least a filename in order to create a new recipe."
log.Fatal(errMsg)
}

if strings.HasSuffix(filename, suffix) {
Expand All @@ -70,8 +71,8 @@ func validateNewRecipe(filename string) string {
// error if the file exists, so os.IsExist would receive a nil value for err.
// So, an os.IsExist(err) block would never execute for a file that exists.
if !os.IsNotExist(err) {
fmt.Printf("There already exists a file at the path: %s\n", filepath)
os.Exit(1)
errMsg := fmt.Sprintf("There already exists a file at the path: %s\n", filepath)
log.Fatal(errMsg)
}
return filepath
}
Expand All @@ -95,13 +96,11 @@ func writeNewRecipeFile(filepath string, name string, fields map[string]interfac

tpl, err := template.New("newrecipe").Parse(newRecipeTemplate)
if err != nil {
fmt.Println("Error parsing recipe template.")
os.Exit(1)
log.Fatal("Error parsing recipe template.")
}
err = tpl.Execute(f, recipeVariables)
if err != nil {
fmt.Println("Error executing template.")
os.Exit(1)
log.Fatal("Error executing template.")
}
}

Expand All @@ -113,8 +112,7 @@ func CreateNewRecipe(filename string, name string, fieldFlags []string) {
if len(fieldFlags) > 0 {
validatedFields, validateFieldsErr = validateFields(fieldFlags)
if validateFieldsErr != nil {
fmt.Println("An error occurred while validating the new recipe fields.")
os.Exit(1)
log.Fatal("An error occurred while validating the new recipe fields.")
}
}

Expand Down
22 changes: 12 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func GetBasenameWithoutExt(fullFilepath string) string {
func DisplayRecipe(fullFilepath string) {
recipeFile, err := ParseFile(fullFilepath)
if err != nil {
fmt.Printf("Unable to read file: %s\n", fullFilepath)
os.Exit(1)
errorMsg := fmt.Sprintf("Unable to read file: %s\n", fullFilepath)
log.Fatal(errorMsg)
}
RenderMarkdown(recipeFile.Markdown)
}
Expand Down Expand Up @@ -168,20 +168,22 @@ func init() {
prefix = fmt.Sprintf("%s/.drinks", homeDir)
}
default:
fmt.Println("Not a valid binary (must be one of `cook` or `concoct`).")
os.Exit(1)
// Doesn't matter what the prefix is if the binary is neither `cook`
// nor `concoct` as long as the the path exists; this likely means
// that we're in test mode.
prefix = homeDir
}

fileInfo, err := os.Lstat(prefix)
if err != nil {
fmt.Printf("There was an error getting file info for: %s\n", prefix)
os.Exit(1)
errorMsg := fmt.Sprintf("There was an error getting file info for the prefix: %s\n", prefix)
log.Fatal(errorMsg)
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
prefix, err = os.Readlink(prefix)
if err != nil {
fmt.Printf("Unable to read symlink: %s\n", prefix)
os.Exit(1)
errorMsg := fmt.Sprintf("Unable to read symlink: %s\n", prefix)
log.Fatal(errorMsg)
}
}
suffix = os.Getenv("COOK_RECIPES_EXT")
Expand All @@ -190,9 +192,9 @@ func init() {
}

// Log levels
log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{PadLevelText: true})
log.SetOutput(os.Stdout)
log.SetLevel(log.WarnLevel)
log.SetLevel(log.FatalLevel)
}

type flagArray []string
Expand Down
4 changes: 2 additions & 2 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func ParseSearchQuery(args []string) (map[string]Constraint, error) {
func Search(args []string) {
parsedQuery, parseErr := ParseSearchQuery(args)
if parseErr != nil {
fmt.Printf("Invalid search query: %s\n", parseErr.Error())
os.Exit(1)
errMsg := fmt.Sprintf("Invalid search query: %s\n", parseErr.Error())
log.Fatal(errMsg)
}

w := walk{prefix: prefix, searchArgs: parsedQuery}
Expand Down

0 comments on commit 438c26b

Please sign in to comment.