local createCloud = require('/apis/libcloud'); local createVersion = require('/apis/libversion'); local URL_SETTING = 'cloud.url'; local PASSWORD_SETTING = 'cloud.password'; local RECONNECT_EVENT = 'trapos_cloud_reconnect'; local rawArgs = table.pack(...); local command = rawArgs[1]; local function isBlank(value) return type(value) ~= 'string' or value == ''; end local function printUsage() print('cloud usage:'); print(); print(' cloud status'); print(' cloud login [--force|-f] [password]'); print(' cloud --version'); print(' cloud --help'); print(); print('settings required:'); print(' cloud.url (gateway origin, e.g. ws://host:4444)'); print('settings optional:'); print(' cloud.password (shared secret for the hello handshake)'); end local function readPassword() write('Password: '); if type(read) == 'function' then return read('*'); end if io and type(io.read) == 'function' then return io.read(); end return ''; end local function parseLoginArgs(args) local force = false; local password = nil; for i = 2, args.n do local arg = args[i]; if arg == '--force' or arg == '-f' then force = true; elseif arg and string.sub(arg, 1, 1) == '-' then return nil, 'unknown option: ' .. arg; elseif not password then password = arg; else return nil, 'unexpected argument: ' .. tostring(arg); end end return { force = force, password = password }, nil; end if command == '--version' or command == '-version' or command == 'version' or command == '-v' then print('v' .. createVersion().forSelf()); return; end if command == '--help' or command == '-help' or command == 'help' or command == '-h' then printUsage(); return; end if command == 'status' then local url = settings.get(URL_SETTING); if isBlank(url) then print('set cloud.url first'); return; end local ok, result = createCloud().request('probe_server'); if not ok then local code = (type(result) == 'table' and result.code) or 'internal'; local message = (type(result) == 'table' and result.message) or ''; print('cloud status failed: ' .. tostring(code) .. ' ' .. tostring(message)); return; end print('serverOk: ' .. tostring(result.serverOk)); print('opencodeOk: ' .. tostring(result.opencodeOk)); if not result.opencodeOk and result.opencodeDetail then print('opencode: ' .. tostring(result.opencodeDetail)); end return; end if command == 'login' then local opts, parseError = parseLoginArgs(rawArgs); if parseError then print(parseError); printUsage(); return; end if not opts.force and not isBlank(settings.get(PASSWORD_SETTING)) then print('cloud.password is already set; use cloud login --force to replace it'); return; end local password = opts.password or readPassword(); if isBlank(password) then print('cloud.password not changed: password cannot be empty'); return; end settings.set(PASSWORD_SETTING, password); settings.save(); os.queueEvent(RECONNECT_EVENT); print('cloud.password set'); return; end printUsage();