Skip to content

Instantly share code, notes, and snippets.

@tomekowal
Last active July 19, 2022 08:29
Show Gist options
  • Save tomekowal/efca0cfff1de39879443aed920e6cf70 to your computer and use it in GitHub Desktop.
Save tomekowal/efca0cfff1de39879443aed920e6cf70 to your computer and use it in GitHub Desktop.
Mix.install([{:ratatouille, "~> 0.5.0"},
{:keymap, github: "tomekowal/keymap", ref: "master"}])
defmodule SwissArmyKnife do
defmodule Model do
defstruct [:bc_dev_domain, :menu]
end
@behaviour Ratatouille.App
@actions [{?o, "Open Service"}]
@tiers [{?c, "com"}, {?b, "biz"}, {?m, "mobi"}, {?l, "local"}, {?g, "GitHub"}]
@services [
{?e, "pos-example"},
{?c, "acquibase"},
{?p, "payments-admin"},
{?u, "simulator (acquiring)"},
{?b, "bridge"},
{?i, "simulator (issuing)"}
]
@simulators ["pos-example", "simulator (acquiring)", "simulator (issuing)"]
@bc_github "https://github.com/bluecodecom/"
import Ratatouille.View
def init(_context) do
bc_dev_domain = ConfigHelper.get_env_or_raise("BC_DEV_DOMAIN")
%Model{menu: main_menu(bc_dev_domain), bc_dev_domain: bc_dev_domain}
end
defp main_menu(bc_dev_domain) do
keymap =
@actions
|> Enum.reduce(Keymap.new(), fn {action_bindings, action_name}, keymap ->
keymap
|> Keymap.put_in(List.flatten([action_bindings]), action_name, Keymap.new)
end)
keymap =
(for a <- @actions, t <- @tiers, do: {a, t})
|> Enum.reduce(keymap, fn {{action_bindings, _}, {tier_binding, tier_name}}, keymap ->
keymap
|> Keymap.put_in(List.flatten([action_bindings, tier_binding]), tier_name, Keymap.new())
end)
keymap =
(for a <- @actions, t <- @tiers, s <- [{?a, "all"} | @services], filter_simulators(t, s), do: {a, t, s})
|> Enum.reduce(keymap, fn {{action_bindings, _}, {tier_binding, tier_name}, {service_binding, service_name}}, keymap ->
keymap
|> Keymap.put_in(List.flatten([action_bindings, tier_binding, service_binding]), service_name, fn -> open_service(tier_name, service_name, bc_dev_domain) end)
end)
keymap
end
defp filter_simulators({_, "com"}, {_, service}), do: service not in @simulators
defp filter_simulators(_, _), do: true
def update(model, msg) do
case msg do
{:event, %{ch: character}} ->
case model.menu[character] || &noop/0 do
function when is_function(function) ->
function.()
%Model{model | menu: main_menu(model.bc_dev_domain)}
new_opts when is_map(new_opts) ->
%Model{model | menu: new_opts}
end
_ ->
%Model{model | menu: main_menu(model.bc_dev_domain)}
end
end
def render(model) do
view do
panel height: :fill, title: "What do you want to do?" do
for {shortcut, item} <- model.menu do
label(content: "#{[shortcut]} -> #{item.name}")
end
end
end
end
def open_service("GitHub", service, bc_dev_domain) do
urls =
case service do
"all" -> for {_, service} <- @services, do: github_link(service, bc_dev_domain)
service -> [github_link(service, bc_dev_domain)]
end
|> Enum.uniq()
open_firefox(urls)
end
def open_service("local", service, bc_dev_domain) do
urls =
case service do
"all" -> for {_, service} <- @services, do: local_link(service, bc_dev_domain)
service -> [local_link(service, bc_dev_domain)]
end
open_firefox(urls)
end
def open_service(tld, service, _) do
urls =
case service do
"all" ->
services = for {_, service} <- @services, do: service
services = if tld == "com", do: services -- @simulators, else: services
for service <- services, do: aws_link(service, tld)
service -> [aws_link(service, tld)]
end
open_firefox(urls)
end
def github_link(service, _bc_dev_domain) do
case service do
"pos-example" -> @bc_github <> "acquibase"
"acquibase" -> @bc_github <> "acquibase"
"payments-admin" -> @bc_github <> "scheme-services"
"simulator (acquiring)" -> @bc_github <> "empsa-scheme-simulator"
"bridge" -> @bc_github <> "empsa-bridge"
"simulator (issuing)" -> @bc_github <> "empsa-scheme-simulator"
end
end
def local_link(service, bc_dev_domain) do
case service do
"simulator (acquiring)" -> "https://bridge-simulator.#{bc_dev_domain}/admin/acq/acq_payments"
"simulator (issuing)" -> "https://bridge-simulator.#{bc_dev_domain}/admin/iss/payments"
service -> "https://#{service}.#{bc_dev_domain}"
end
end
def aws_link(service, tld) do
case service do
"simulator (acquiring)" -> "https://admin.empsa-scheme-simulator.spt-payments.#{tld}/admin/acq/acq_payments"
"simulator (issuing)" -> "https://admin.empsa-scheme-simulator.spt-payments.#{tld}/admin/iss/payments"
"bridge" -> "https://admin.empsa-bridge.spt-payments.#{tld}/bridge/admin/signin"
service -> "https://#{service}.bluecode.#{tld}"
end
end
def open_firefox(urls) do
# System.cmd blocked until I closed Firfox, running in Task prevents thakt
Task.async(fn ->
# Firefox prints stuff to stdout breaking the tui, stderr_to_stdout prevents that
System.cmd("firefox", urls, stderr_to_stdout: true)
end)
end
def noop do
:ok
end
end
defmodule ConfigHelper do
def get_env_or_raise(env_var) do
System.get_env(env_var) || raise "#{env_var} not set"
end
end
Ratatouille.run(SwissArmyKnife)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment