diff --git a/Gopkg.lock b/Gopkg.lock index b7d9cfb..f407cb5 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -13,6 +13,12 @@ packages = ["."] revision = "b586327df9d5a5e5023fa1338f44941745136d68" +[[projects]] + branch = "master" + name = "github.com/Xe/gluanetrc" + packages = ["."] + revision = "af26c7928995089c19896bcc5d0f8ba48a7930a9" + [[projects]] branch = "master" name = "github.com/Xe/gopreload" @@ -965,6 +971,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "9eaa07e1d6ac7d535a05586369d4609d3930210539508423bac7cc4db97795d9" + inputs-digest = "04d2f396a00556d6fd4651d3af551eb832ea69ff3c2ba78a5e345a494d5c2bf0" solver-name = "gps-cdcl" solver-version = 1 diff --git a/cmd/construct/main.go b/cmd/construct/main.go index 77813d8..e36e316 100644 --- a/cmd/construct/main.go +++ b/cmd/construct/main.go @@ -8,7 +8,9 @@ import ( "os" "path/filepath" + "git.xeserv.us/xena/route/internal/gluaroute" "github.com/Xe/eclier" + "github.com/Xe/gluanetrc" "github.com/Xe/x/tools/glue/libs/gluaexpect" "github.com/Xe/x/tools/glue/libs/gluasimplebox" "github.com/ailncode/gluaxmlpath" @@ -31,6 +33,7 @@ import ( var hDir string var cfgHome *string var netrcFile *string +var defaultServer *string func init() { dir, err := homedir.Dir() @@ -42,6 +45,7 @@ func init() { cfgHome = flag.String("home", filepath.Join(hDir, ".construct"), "construct's home directory") netrcFile = flag.String("netrc", filepath.Join(hDir, ".netrc"), "location of netrc file to use for authentication") + defaultServer = flag.String("default-server", "https://api.route.xeserv.us:7268", "api server to connect to") } func main() { @@ -55,18 +59,22 @@ func main() { os.MkdirAll(pluginLoc, 0755) os.MkdirAll(scriptsLoc, 0755) - fout, err := os.Create(*netrcFile) - if err != nil { - log.Fatal(err) + + if _, err := os.Stat(*netrcFile); err != nil { + log.Printf("creating netrc file...") + fout, err := os.Create(*netrcFile) + if err != nil { + log.Fatal(err) + } + fout.Close() } - fout.Close() opts := []eclier.RouterOption{ eclier.WithGluaCreationHook(preload), eclier.WithScriptHome(scriptsLoc), } - err = filepath.Walk(pluginLoc, func(path string, info os.FileInfo, err error) error { + err := filepath.Walk(pluginLoc, func(path string, info os.FileInfo, err error) error { if info.IsDir() { opts = append(opts, eclier.WithScriptHome(info.Name())) } @@ -101,4 +109,6 @@ func preload(L *lua.LState) { gluasimplebox.Preload(L) gluaxmlpath.Preload(L) json.Preload(L) + gluanetrc.Preload(L) + gluaroute.Preload(L) } diff --git a/internal/gluaroute/route.go b/internal/gluaroute/route.go new file mode 100644 index 0000000..1f1962e --- /dev/null +++ b/internal/gluaroute/route.go @@ -0,0 +1,49 @@ +package gluaroute + +import ( + "net/http" + + "git.xeserv.us/xena/route/proto" + "git.xeserv.us/xena/route/proto/route" + lua "github.com/yuin/gopher-lua" + luar "layeh.com/gopher-luar" +) + +var exports = map[string]lua.LGFunction{ + "new": newClient, +} + +func newClient(L *lua.LState) int { + routedURL := L.ToString(1) + token := L.ToString(2) + + L.Push(luar.New(L, route.New(routedURL, token, &http.Client{}))) + + return 1 +} + +type client struct { + *route.Client +} + +type backends struct { + proto.Backends +} + +type routes struct { + proto.Routes +} + +type tokens struct { + proto.Tokens +} + +func Preload(L *lua.LState) { + L.PreloadModule("svc", Loader) +} + +func Loader(L *lua.LState) int { + mod := L.SetFuncs(L.NewTable(), exports) + L.Push(mod) + return 1 +} diff --git a/mage.go b/mage.go index 3eba98d..676646f 100644 --- a/mage.go +++ b/mage.go @@ -177,8 +177,6 @@ func Tools(ctx context.Context) { func Generate(ctx context.Context) { dir := filepath.Join(wd, "proto") - Tools(ctx) - shouldWork(ctx, nil, dir, "sh", "./regen.sh") } diff --git a/proto/eclier/route_twirp_eclier_backends_kill.lua b/proto/eclier/route_twirp_eclier_backends_kill.lua index a200080..e4029f0 100644 --- a/proto/eclier/route_twirp_eclier_backends_kill.lua +++ b/proto/eclier/route_twirp_eclier_backends_kill.lua @@ -26,6 +26,6 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.backends.kill(flags) + local resp = svc.new(apiURL, token):backends():kill(flags) end diff --git a/proto/eclier/route_twirp_eclier_backends_list.lua b/proto/eclier/route_twirp_eclier_backends_list.lua index 9e8d8bd..fc4cf50 100644 --- a/proto/eclier/route_twirp_eclier_backends_list.lua +++ b/proto/eclier/route_twirp_eclier_backends_list.lua @@ -27,7 +27,7 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.backends.list(flags) + local resp = svc.new(apiURL, token):backends():list(flags) print("bs:\t\t" .. tostring(resp.bs)) print("backends:\t\t" .. tostring(resp.backends)) diff --git a/proto/eclier/route_twirp_eclier_routes_delete.lua b/proto/eclier/route_twirp_eclier_routes_delete.lua index f48f561..7e417c9 100644 --- a/proto/eclier/route_twirp_eclier_routes_delete.lua +++ b/proto/eclier/route_twirp_eclier_routes_delete.lua @@ -28,6 +28,6 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.routes.delete(flags) + local resp = svc.new(apiURL, token):routes():delete(flags) end diff --git a/proto/eclier/route_twirp_eclier_routes_get.lua b/proto/eclier/route_twirp_eclier_routes_get.lua index 1bfdac9..454fe06 100644 --- a/proto/eclier/route_twirp_eclier_routes_get.lua +++ b/proto/eclier/route_twirp_eclier_routes_get.lua @@ -27,7 +27,7 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.routes.get(flags) + local resp = svc.new(apiURL, token):routes():get(flags) print("id:\t\t" .. tostring(resp.id)) print("creator:\t\t" .. tostring(resp.creator)) diff --git a/proto/eclier/route_twirp_eclier_routes_get_all.lua b/proto/eclier/route_twirp_eclier_routes_get_all.lua index 3f2be77..a29d64f 100644 --- a/proto/eclier/route_twirp_eclier_routes_get_all.lua +++ b/proto/eclier/route_twirp_eclier_routes_get_all.lua @@ -25,7 +25,7 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.routes.get_all(flags) + local resp = svc.new(apiURL, token):routes():get_all(flags) print("routes:\t\t" .. tostring(resp.routes)) end diff --git a/proto/eclier/route_twirp_eclier_routes_put.lua b/proto/eclier/route_twirp_eclier_routes_put.lua index baad522..5e17fcf 100644 --- a/proto/eclier/route_twirp_eclier_routes_put.lua +++ b/proto/eclier/route_twirp_eclier_routes_put.lua @@ -28,7 +28,7 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.routes.put(flags) + local resp = svc.new(apiURL, token):routes():put(flags) print("id:\t\t" .. tostring(resp.id)) print("creator:\t\t" .. tostring(resp.creator)) diff --git a/proto/eclier/route_twirp_eclier_tokens_deactivate.lua b/proto/eclier/route_twirp_eclier_tokens_deactivate.lua index 06cd8a0..7cdde7e 100644 --- a/proto/eclier/route_twirp_eclier_tokens_deactivate.lua +++ b/proto/eclier/route_twirp_eclier_tokens_deactivate.lua @@ -13,10 +13,10 @@ local svc = require "svc" local fs = flag.new() -- flags for Token -fs:string("id", "", "value for message arg id") fs:string("body", "", "value for message arg body") -fs:strings("scopes", "", "value for message arg scopes") -fs:bool("active", "", "value for message arg active") +fs:strings("scopes", "value for message arg scopes") +fs:bool("active", false, "value for message arg active") +fs:string("id", "", "value for message arg id") script.usage = fs:usage() @@ -29,6 +29,6 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.tokens.deactivate(flags) + local resp = svc.new(apiURL, token):tokens():deactivate(flags) end diff --git a/proto/eclier/route_twirp_eclier_tokens_delete.lua b/proto/eclier/route_twirp_eclier_tokens_delete.lua index 70a30e4..d2cb974 100644 --- a/proto/eclier/route_twirp_eclier_tokens_delete.lua +++ b/proto/eclier/route_twirp_eclier_tokens_delete.lua @@ -13,10 +13,10 @@ local svc = require "svc" local fs = flag.new() -- flags for Token +fs:strings("scopes", "value for message arg scopes") +fs:bool("active", false, "value for message arg active") fs:string("id", "", "value for message arg id") fs:string("body", "", "value for message arg body") -fs:strings("scopes", "", "value for message arg scopes") -fs:bool("active", "", "value for message arg active") script.usage = fs:usage() @@ -29,6 +29,6 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.tokens.delete(flags) + local resp = svc.new(apiURL, token):tokens():delete(flags) end diff --git a/proto/eclier/route_twirp_eclier_tokens_get.lua b/proto/eclier/route_twirp_eclier_tokens_get.lua index e4e242a..b8e6642 100644 --- a/proto/eclier/route_twirp_eclier_tokens_get.lua +++ b/proto/eclier/route_twirp_eclier_tokens_get.lua @@ -27,7 +27,7 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.tokens.get(flags) + local resp = svc.new(apiURL, token):tokens():get(flags) print("id:\t\t" .. tostring(resp.id)) print("body:\t\t" .. tostring(resp.body)) diff --git a/proto/eclier/route_twirp_eclier_tokens_get_all.lua b/proto/eclier/route_twirp_eclier_tokens_get_all.lua index e039709..37ef46d 100644 --- a/proto/eclier/route_twirp_eclier_tokens_get_all.lua +++ b/proto/eclier/route_twirp_eclier_tokens_get_all.lua @@ -25,7 +25,7 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.tokens.get_all(flags) + local resp = svc.new(apiURL, token):tokens():get_all(flags) print("tokens:\t\t" .. tostring(resp.tokens)) end diff --git a/proto/eclier/route_twirp_eclier_tokens_put.lua b/proto/eclier/route_twirp_eclier_tokens_put.lua index 50838b9..5c1f145 100644 --- a/proto/eclier/route_twirp_eclier_tokens_put.lua +++ b/proto/eclier/route_twirp_eclier_tokens_put.lua @@ -13,10 +13,10 @@ local svc = require "svc" local fs = flag.new() -- flags for Token +fs:bool("active", false, "value for message arg active") fs:string("id", "", "value for message arg id") fs:string("body", "", "value for message arg body") -fs:strings("scopes", "", "value for message arg scopes") -fs:bool("active", "", "value for message arg active") +fs:strings("scopes", "value for message arg scopes") script.usage = fs:usage() @@ -29,7 +29,7 @@ function run(arg) arg[0] = script.verb local flags = fs:parse(arg) - local resp = svc.tokens.put(flags) + local resp = svc.new(apiURL, token):tokens():put(flags) print("id:\t\t" .. tostring(resp.id)) print("body:\t\t" .. tostring(resp.body)) diff --git a/vendor/github.com/Xe/gluanetrc/LICENSE b/vendor/github.com/Xe/gluanetrc/LICENSE new file mode 100644 index 0000000..82248fe --- /dev/null +++ b/vendor/github.com/Xe/gluanetrc/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2017 Christine Dodrill + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/vendor/github.com/Xe/gluanetrc/config.ld b/vendor/github.com/Xe/gluanetrc/config.ld new file mode 100644 index 0000000..50dfad6 --- /dev/null +++ b/vendor/github.com/Xe/gluanetrc/config.ld @@ -0,0 +1,7 @@ +file = { + "./netrc.lua", +} + +title = "gluanetrc" +project = "Xe/gluanetrc" +description = "netrc offers a simple interface to a user's netrc file in their home directory" diff --git a/vendor/github.com/Xe/gluanetrc/netrc.go b/vendor/github.com/Xe/gluanetrc/netrc.go new file mode 100644 index 0000000..c2dcd07 --- /dev/null +++ b/vendor/github.com/Xe/gluanetrc/netrc.go @@ -0,0 +1,81 @@ +package gluanetrc + +import ( + "os" + "path/filepath" + + "github.com/dickeyxxx/netrc" + lua "github.com/yuin/gopher-lua" + luar "layeh.com/gopher-luar" +) + +var n *netrc.Netrc + +func init() { + var err error + + fname := filepath.Join(os.Getenv("HOME"), ".netrc") + + fout, err := os.Create(fname) + if err != nil { + panic(err) + } + fout.Close() + + n, err = netrc.Parse(filepath.Join(fname)) + if err != nil { + panic(err) + } +} + +var exports = map[string]lua.LGFunction{ + "machine": machine, + "save": save, + "remove_machine": removeMachine, + "add_machine": addMachine, +} + +func addMachine(L *lua.LState) int { + name := L.ToString(1) + login := L.ToString(2) + password := L.ToString(3) + + n.AddMachine(name, login, password) + + L.Push(luar.New(L, n.Machine(name))) + return 1 +} + +func removeMachine(L *lua.LState) int { + name := L.ToString(1) + + n.RemoveMachine(name) + + return 0 +} + +func machine(L *lua.LState) int { + name := L.ToString(1) + + m := n.Machine(string(name)) + + L.Push(luar.New(L, m)) + return 1 +} + +func save(L *lua.LState) int { + n.Save() + return 0 +} + +// Preload loads netrc into a gopher-lua's LState module registry. +func Preload(L *lua.LState) { + L.PreloadModule("netrc", Loader) +} + +// Loader loads the netrc modules. +func Loader(L *lua.LState) int { + mod := L.SetFuncs(L.NewTable(), exports) + L.Push(mod) + return 1 +} diff --git a/vendor/github.com/Xe/gluanetrc/netrc.lua b/vendor/github.com/Xe/gluanetrc/netrc.lua new file mode 100644 index 0000000..18139b2 --- /dev/null +++ b/vendor/github.com/Xe/gluanetrc/netrc.lua @@ -0,0 +1,59 @@ +--- Module netrc offers a simple interface to a user's netrc file in their home directory. +-- @module netrc + +local netrc = {} + +--- add_machine adds a machine to the netrc manifest with a username and password. +-- @param name string the domain name of the machine +-- @param login string the user name to log in as +-- @param password string the password or similar secret for the machine +-- @return Machine +function netrc.add_machine(name, login, password) +end + +--- machine loads netrc data for a given machine by domain name. +-- Any changes made with the `set` method of a machine will be saved to the disk +-- when the module's `save` function is called. If the given machine does not +-- exist in the netrc file, this function will return nil. +-- @param name string +-- @return Machine +-- @usage local creds = netrc.machine("api.foobar.com") +-- @usage print(creds:get("username"), creds:get("password")) +function netrc.machine(name) + return nil +end + +--- remove_machine removes a single machine from the netrc manifest by name. +-- @param name string the name of the machine to remove from the netrc manifest +-- @usage netrc.remove_machine("api.digg.com") +function netrc.remove_machine(name) +end + +--- save writes all changes made in machine `set` methods to the disk at $HOME/.netrc. +-- This function will raise a lua error if the save fails. This function should +-- not fail in the course of normal operation. +-- @usage netrc.save() +function netrc.save() +end + +--- Machine is a userdata wrapper around the go netrc.Machine type. +-- https://godoc.org/github.com/dickeyxxx/netrc#Machine +-- @type Machine + +local Machine = {} + +--- get gets a Machine value by key. +-- @param key the netrc key to get +-- @return string the value from the netrc +-- @usage local cli = api.new(m:get("login"), m:get("password")) +function Machine:get(key) +end + +--- set updates information in this Machine by a key, value pair. +-- @param key the netrc key to set +-- @param value the value to set the above key to +-- @usage m:set("password", "hunter2") +function Machine:set(key, value) +end + +return netrc