Skip to content

Commit

Permalink
try to unsquash cue errors
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed Apr 7, 2020
1 parent edf052a commit 8423960
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
7 changes: 5 additions & 2 deletions lib/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package lib
import (
"fmt"
"time"

"github.com/hofstadter-io/hof/lib/util"
)

func Gen(entrypoints, expressions []string, mode string) (error) {
Expand All @@ -15,15 +17,16 @@ func Gen(entrypoints, expressions []string, mode string) (error) {
errs = R.LoadCue()
if len(errs) > 0 {
for _, e := range errs {
fmt.Println(e)
util.PrintCueError(e)
}
return fmt.Errorf("\nErrors while loading cue files\n")
}

errs = R.LoadGenerators()
if len(errs) > 0 {
for _, e := range errs {
fmt.Println(e)
util.PrintCueError(e)
// fmt.Println(e)
}
return fmt.Errorf("\nErrors while loading generators\n")
}
Expand Down
32 changes: 1 addition & 31 deletions lib/gen/loadcue.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
package gen

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"time"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"golang.org/x/text/language"
"golang.org/x/text/message"

"github.com/hofstadter-io/hof/lib/templates"
)

func getLang() language.Tag {
loc := os.Getenv("LC_ALL")
if loc == "" {
loc = os.Getenv("LANG")
}
loc = strings.Split(loc, ".")[0]
return language.Make(loc)
}

func (G *Generator) LoadCue() ([]error) {
// fmt.Println("Gen Load:", G.Name)

Expand All @@ -34,21 +18,6 @@ func (G *Generator) LoadCue() ([]error) {
// Decode the value into a temporary "generator" with timing
err := G.CueValue.Decode(&gen)
if err != nil {

p := message.NewPrinter(getLang())
format := func(w io.Writer, format string, args ...interface{}) {
p.Fprintf(w, format, args...)
}
cwd, _ := os.Getwd()
w := &bytes.Buffer{}
errors.Print(w, err, &errors.Config{
Format: format,
Cwd: cwd,
ToSlash: false,
})
s := w.String()
fmt.Println(s)

return []error{err}
}

Expand Down Expand Up @@ -241,3 +210,4 @@ func (G *Generator) decodeFile(i int, file map[string]interface{}) (*File, error

return F, nil
}

41 changes: 41 additions & 0 deletions lib/util/cue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package util

import (
"bytes"
"fmt"
"io"
"os"
"strings"

"cuelang.org/go/cue/errors"
"golang.org/x/text/language"
"golang.org/x/text/message"
)

func getLang() language.Tag {
loc := os.Getenv("LC_ALL")
if loc == "" {
loc = os.Getenv("LANG")
}
loc = strings.Split(loc, ".")[0]
return language.Make(loc)
}

func PrintCueError(err error) {

p := message.NewPrinter(getLang())
format := func(w io.Writer, format string, args ...interface{}) {
p.Fprintf(w, format, args...)
}
cwd, _ := os.Getwd()
w := &bytes.Buffer{}
errors.Print(w, err, &errors.Config{
Format: format,
Cwd: cwd,
ToSlash: false,
})
s := w.String()
fmt.Println(s)

}

0 comments on commit 8423960

Please sign in to comment.