module main import cli import log import os const install_command = cli.Command{ name: 'install' description: 'Installs a browser' execute: execute_install required_args: 1 flags: [ cli.Flag{ flag: .bool name: 'verbose' abbrev: 'v' description: 'Enable debug logging' }, cli.Flag{ flag: .string name: 'cache-path' abbrev: 'c' description: 'Path to cache directory' }, cli.Flag{ flag: .string name: 'install-to' abbrev: 'i' description: 'Path to which the binary is linked' }, ] } fn execute_install(cmd cli.Command) ! { if cmd.flags.get_bool('verbose') or { false } { log.set_level(.debug) } browser, version := parse_target(cmd.args[0]) or { return error('failed to parse target: ${err}') } cache_path := cmd.flags.get_string('cache-path') or { '' } cache := match cache_path { '' { Cache.default() or { return error('failed to create default cache: ${err}') } } else { Cache.new(cache_path) or { return error('failed to create cache: ${err}') } } } install_path := cmd.flags.get_string('install-to') or { '' } install_to := match install_path { '' { cache.file('bin', 'uchromium') or { return error('failed to create default install dir: ${err}') } } else { install_dir := os.dir(install_path) os.mkdir_all(install_dir) or { return error('failed to create install dir ${install_dir}: ${err}') } install_path } } log.info('using install path: ${install_path}') prov := Provider.for_browser(browser, cache) or { return error('failed to get provider: ${err}') } return prov.install(version, install_to) } enum BrowserType { uchromium } fn parse_target(target string) !(BrowserType, string) { parts := target.split_n(':', 2) name, version := match parts.len { 1 { parts[0], 'latest' } 2 { parts[0], parts[1] } else { return error('unexpected target ${target}') } } browser := BrowserType.from(name) or { return error('unexpected browser name ${name}') } return browser, version }