From ba569d4aabef5d98628be3b5c400053112e0f5e2 Mon Sep 17 00:00:00 2001 From: Vladimir Serov Date: Sun, 29 Dec 2019 18:27:37 +0300 Subject: [PATCH] [feat] proper config fetching (not yet used) --- appvm.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/appvm.go b/appvm.go index 4a9e8cd..eef7a5b 100644 --- a/appvm.go +++ b/appvm.go @@ -98,6 +98,43 @@ var xmlTmpl = ` ` +func evalNix(expr string) (s string) { + command := exec.Command("nix", "eval", expr) + bytes, _ := command.Output() + s = string(bytes) + return +} + +// Gets an expression returning AppVM config path +func getAppVMExpressionPath(name string) string { + paths := strings.Split(os.Getenv("APPVM_CONFIGS"), ":") + for _, a := range paths { + searchpath := a + "/nix" + log.Print("Searching " + searchpath + " for expressions") + if _, err := os.Stat(searchpath); os.IsExist(err) { + exprpath := searchpath + "/" + name + ".nix" + + if os.Stat(exprpath); os.IsExist(err) { + return exprpath + } + + } + log.Print("Local repo " + searchpath + " doesn't have a nix expression for " + name) + } + log.Print("Trying to use remote repo config") + + fetchFormat := "(builtins.fetchurl \"raw.githubusercontent.com/%[1]s/%[2]s/master/nix/%[3]s.nix\" )" + splitString := strings.Split(name, "/") + + if len(splitString) != 3 { + // nope, not a repo format + return evalNix(fmt.Sprintf(fetchFormat, "jollheef", "appvm", name)) + } + + return evalNix(fmt.Sprintf(fetchFormat, splitString[0], splitString[1], splitString[2])) + +} + func generateXML(name, vmNixPath, reginfo, img, sharedDir string) string { // TODO: Define XML in go return fmt.Sprintf(xmlTmpl, "appvm_"+name, vmNixPath, vmNixPath, vmNixPath,