az2: work in progress, a wrapper like `az` but in Lua. Needs arg splicing
before I want to use it.
1 files changed, 101 insertions(+), 0 deletions(-)

A => bin/az2
A => bin/az2 +101 -0
@@ 0,0 1,101 @@ 
+#!/usr/bin/env lua5.1
+
+-- apt-get install lua-posix
+posix = require "posix"
+
+alias_infos = {
+    ["jobs"] = {
+        ["desc"] = "Show both command jobs and sweep jobs",
+        ["script"] = [[ az . commands
+                        az . sweeps ]],
+
+    },
+    ["help"] = {
+        ["desc"] = "Print available aliases",
+        ["func"] = nil, -- fill in below
+    },
+    ["sweeps"] = {
+        ["desc"] = "Show sweep jobs",
+        ["script"] = [[
+            az ml job list --query '[? type == `sweep_job`]'
+        ]],
+    },
+    ["commmands"] = {
+        ["desc"] = "Show command jobs",
+        ["script"] = [[
+            az ml job list --query '[? type != `sweep_job`].{
+                name: name, status: status, experiment_name: experiment_name, command: command,
+                type: type, description: description}'
+        ]]
+    },
+    ["computes"] = {
+        ["desc"] = "Show compute instances",
+        ["script"] = [[
+            az ml compute list --type amlcompute
+            az ml compute list --type computeinstance
+        ]],
+    },
+    ["sizes"] = {
+        ["desc"] = "Show sizes of compute instance types",
+        ["script"] = [[
+            az ml compute list-sizes --query 'value[].{name: name, vCpUs: vCpUs, memoryGb: join(``, [to_string(@.memoryGb), `" GB"`])}'
+        ]]
+    },
+}
+
+-- Alternative names for commands
+alias_infos.computers = alias_infos.computes
+alias_infos.computesizes = alias_infos.sizes
+
+-- Print description of the given function
+alias_infos.help.func = function (_alias_info, args)
+    -- `az help`
+    if #args == 0 then
+        for name, info in pairs(alias_infos) do
+            print(name .. ": " .. info.desc)
+        end
+        return
+    end
+
+    get_help_for = args[1]
+
+    -- `az help some_known_alias`
+    info = alias_infos[get_help_for]
+    if info then
+        print(get_help_for .. ": " .. info.desc)
+        return
+    else
+    -- `az help unknown_alias`
+        print("Unknown alias: `" .. get_help_for)
+    end
+end
+
+
+-- Print command, then run it.
+-- alias_info: one entry of the alias_infos table.
+-- args: a table of further arguments. Currently unused; in future, may be
+-- spliced into the script's placeholders.
+function run_script(alias_info, args)
+    print(alias_info.script)
+    os.execute(alias_info.script)
+end
+
+
+function main(args)
+    if args[1] ~= '.' then
+        az_command = { "/home/sietse/.local/bin/az", unpack(args, 1, #args) }
+        az_command = { "echo", unpack(args, 1, #args) }
+        posix.exec(
+            "/home/sietse/.local/bin/az",
+            { unpack(args, 1, #args) }
+        )
+    end
+
+    -- We're in the '.' namespace, let's look for an alias
+    alias = args[2] or "help"
+    alias_args = { unpack(args, 3, #args) }
+    f = alias_infos[alias]["func"] or run_script
+    f(alias_infos[alias], alias_args)
+end
+
+main(arg)