Skip to content

Commit

Permalink
Add cleaninput for bad json
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Nov 29, 2017
1 parent 8ebbf97 commit 0425089
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var config struct {
Password string
RequestPerHour int
}
Root string
PauseOnError bool
Root string
auth.Config
}

Expand Down
21 changes: 20 additions & 1 deletion tent/cmd/transifex_download.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"bufio"
"encoding/json"
"log"
"os"
"os/signal"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -48,6 +50,8 @@ func downloadRun(cmd *cobra.Command, args []string) {

var count int

pause := bufio.NewReader(os.Stdin)

green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()

Expand Down Expand Up @@ -98,8 +102,12 @@ func downloadRun(cmd *cobra.Command, args []string) {
continue
}
var m []map[string]string
if err := json.NewDecoder(strings.NewReader(target)).Decode(&m); err != nil {

if err := json.NewDecoder(strings.NewReader(cleanInput(target))).Decode(&m); err != nil {
log.Printf("%s (%s) %s\n%s", resource.Slug, t, err, target)
if config.PauseOnError {
pause.ReadBytes('\n')
}
continue
}
resource.Content = m
Expand Down Expand Up @@ -145,3 +153,14 @@ func downloadRun(cmd *cobra.Command, args []string) {
}

}

var (
unescapedNewlines = regexp.MustCompile(`"body":"(\\"|[^"])*\n(\\"|[^"])*"`)
)

func cleanInput(s string) string {
s = unescapedNewlines.ReplaceAllStringFunc(s, func(v string) string {
return strings.Replace(v, "\n", "\\n", -1)
})
return s
}

0 comments on commit 0425089

Please sign in to comment.