errors_windows.go 1.1 KB

123456789101112131415161718192021222324252627
  1. package instructions
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "regexp"
  6. "strings"
  7. )
  8. func errNotJSON(command, original string) error {
  9. // For Windows users, give a hint if it looks like it might contain
  10. // a path which hasn't been escaped such as ["c:\windows\system32\prog.exe", "-param"],
  11. // as JSON must be escaped. Unfortunate...
  12. //
  13. // Specifically looking for quote-driveletter-colon-backslash, there's no
  14. // double backslash and a [] pair. No, this is not perfect, but it doesn't
  15. // have to be. It's simply a hint to make life a little easier.
  16. extra := ""
  17. original = filepath.FromSlash(strings.ToLower(strings.Replace(strings.ToLower(original), strings.ToLower(command)+" ", "", -1)))
  18. if len(regexp.MustCompile(`"[a-z]:\\.*`).FindStringSubmatch(original)) > 0 &&
  19. !strings.Contains(original, `\\`) &&
  20. strings.Contains(original, "[") &&
  21. strings.Contains(original, "]") {
  22. extra = fmt.Sprintf(`. It looks like '%s' includes a file path without an escaped back-slash. JSON requires back-slashes to be escaped such as ["c:\\path\\to\\file.exe", "/parameter"]`, original)
  23. }
  24. return fmt.Errorf("%s requires the arguments to be in JSON form%s", command, extra)
  25. }