in cmd/check-approvals/main.go [38:83]
func approval() int {
cwd, _ := os.Getwd()
receivedFiles := findFiles(cwd, approvaltest.ReceivedSuffix)
for _, rf := range receivedFiles {
af := strings.TrimSuffix(rf, approvaltest.ReceivedSuffix) + approvaltest.ApprovedSuffix
var approved, received interface{}
if err := decodeJSONFile(rf, &received); err != nil {
fmt.Println("Could not create diff ", err)
return 3
}
if err := decodeJSONFile(af, &approved); err != nil && !os.IsNotExist(err) {
fmt.Println("Could not create diff ", err)
return 3
}
diff := cmp.Diff(approved, received)
added := color.New(color.FgBlack, color.BgGreen).SprintFunc()
deleted := color.New(color.FgBlack, color.BgRed).SprintFunc()
scanner := bufio.NewScanner(strings.NewReader(diff))
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 {
switch line[0] {
case '-':
line = deleted(line)
case '+':
line = added(line)
}
}
fmt.Println(line)
}
fmt.Println(rf)
fmt.Println("\nApprove Changes? (y/n)")
reader := bufio.NewReader(os.Stdin)
input, _, _ := reader.ReadRune()
switch input {
case 'y':
approvedPath := strings.Replace(rf, approvaltest.ReceivedSuffix, approvaltest.ApprovedSuffix, 1)
os.Rename(rf, approvedPath)
}
}
return 0
}