-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new cue runtime operational, ready to rock and roll
- Loading branch information
Showing
29 changed files
with
595 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package cuefig | ||
|
||
// Name: config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"cuelang.org/go/cue" | ||
// "cuelang.org/go/cue/build" | ||
"cuelang.org/go/cue/errors" | ||
"cuelang.org/go/cue/load" | ||
|
||
"github.com/hofstadter-io/hof/lib/util" | ||
) | ||
|
||
const ( | ||
ConfigEntrypoint = ".hofcfg.cue" | ||
ConfigWorkpath = "" | ||
ConfigLocation = "local" | ||
) | ||
|
||
func LoadConfigDefault(cfg interface{}) (cue.Value, error) { | ||
return LoadConfigConfig(ConfigWorkpath, ConfigEntrypoint, cfg) | ||
} | ||
|
||
func LoadConfigConfig(workpath, entrypoint string, cfg interface{}) (val cue.Value, err error) { | ||
fmt.Println("Cuefig[Config].Load:", workpath, entrypoint) | ||
|
||
// Fallback order: local / user / global | ||
fpath := filepath.Join(workpath, entrypoint) | ||
|
||
// possibly, check for workpath | ||
if workpath != "" { | ||
_, err = os.Lstat(workpath) | ||
if err != nil { | ||
if _, ok := err.(*os.PathError); !ok && (strings.Contains(err.Error(), "file does not exist") || strings.Contains(err.Error(), "no such file")) { | ||
// error is worse than non-existant | ||
return val, err | ||
} | ||
// otherwise, does not exist, so we should init? | ||
// XXX want to let applications decide how to handle this | ||
fmt.Println("missing:", workpath) | ||
return val, err | ||
} | ||
} | ||
|
||
// check for entrypoint | ||
_, err = os.Lstat(fpath) | ||
if err != nil { | ||
if _, ok := err.(*os.PathError); !ok && (strings.Contains(err.Error(), "file does not exist") || strings.Contains(err.Error(), "no such file")) { | ||
// error is worse than non-existant | ||
return val, err | ||
} | ||
// otherwise, does not exist, so we should init? | ||
// XXX want to let applications decide how to handle this | ||
fmt.Println("missing:", fpath) | ||
return val, err | ||
} | ||
|
||
fmt.Println(" - found cue file:", fpath) | ||
|
||
var errs []error | ||
|
||
CueRT := &cue.Runtime{} | ||
BIS := load.Instances([]string{fpath}, nil) | ||
for _, bi := range BIS { | ||
|
||
if bi.Err != nil { | ||
// fmt.Println("BI ERR", bi.Err, bi.Incomplete, bi.DepsErrors) | ||
es := errors.Errors(bi.Err) | ||
for _, e := range es { | ||
errs = append(errs, e.(error)) | ||
} | ||
continue | ||
} | ||
|
||
// Build the Instance | ||
I, err := CueRT.Build(bi) | ||
if err != nil { | ||
es := errors.Errors(err) | ||
// fmt.Println("BUILD ERR", es, I) | ||
for _, e := range es { | ||
errs = append(errs, e.(error)) | ||
} | ||
continue | ||
} | ||
|
||
// Get top level value from cuelang | ||
V := I.Value() | ||
|
||
err = V.Decode(&cfg) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
val = V | ||
|
||
} | ||
|
||
if len(errs) > 0 { | ||
for _, e := range errs { | ||
util.PrintCueError(e) | ||
} | ||
return val, fmt.Errorf("Errors while reading Config file: %q", fpath) | ||
} | ||
|
||
return val, nil | ||
} |
Oops, something went wrong.