Maintaining a traditional CLI program for a web service can be difficult, since any new CLI features are not available to users unless the user updates their CLI program. That defeats some of the advantages of using a web service to being with, where new features are immediately available for users to use when they are deployed. To avoid this issue, this uses an approach I'm dubbing thin-CLI, where the CLI program is as a thin as possible. With thin-CLI, the CLI submits the argv to the web service, and the web service parses the argv and takes appropriate action. This allows the CLI to support most new features as soon as they are deployed, without the user having to update the CLI program. Here's a breakdown of the parts: * bin/ubi: The CLI program that users will run. This will eventually be written in a cross-compiliable language, but for now it is prototyped in Ruby. This submits the argv to the server (thin-CLI approach), and depending on the response, prints to stdout (success) or stderr (error). It has support for executing ssh or psql, but to limit potential issues with a adverse server, only a single command line argument is provided by the server. The allowed commands are fixed by the client (currently only ssh and psql are allowed commands). At this prototype stage, configuration of bin/ubid is done via environment variables. You must provide the UBI_TOKEN environment variable, and some other UBI_* environment variables can be set. bin/ubi will read .env.rb if it exists, so you can put the token into there. bin/ubi currently defaults to http://api.localhost:9292/cli as the address (puma running in development locally). * routes/cli.rb: Clover route file, only allowed for api requests, which passes the argv to UbiCli. * lib/ubi_cli.rb: Adds UbiCli, which is used as the scope class when executing the command blocks for CLI commands. Passes the argv to UbiRodish to do the parsing, providing the instance of UbiCli as the context object. This class has instance methods that issue internal requests to Clover's API. It currently takes the JSON responses from Clover's API, and turns them into plain-text responses, which will be provided to the client (bin/ubi), which will just take the response output and print to stdout/stderr. This currently contains code not currently used, but will be used by future commits. * lib/ubi_rodish.rb: Rodish processor. In this initial commit, only provides --help and --verbose options, and no subcommands. This has one small change to the create_vm spec method, allowing you to pass an existing project_id keyword, to use that project instead of creating a new project.
19 lines
554 B
Ruby
19 lines
554 B
Ruby
# frozen_string_literal: true
|
|
|
|
class Clover
|
|
hash_branch("cli") do |r|
|
|
r.post api? do
|
|
response["content-type"] = "text/plain"
|
|
|
|
unless (argv = r.POST["argv"]).is_a?(Array) && argv.all?(String)
|
|
response.status = 400
|
|
next "Invalid request: No or invalid argv parameter provided"
|
|
end
|
|
|
|
project_id = env["clover.project_id"] = ApiKey.where(id: rodauth.session["pat_id"]).get(:project_id)
|
|
env["clover.project_ubid"] = UBID.from_uuidish(project_id).to_s
|
|
r.halt UbiCli.process(argv, env)
|
|
end
|
|
end
|
|
end
|