1234567module cli89import json210import src.build11import src.update1213pub fn cmd_update(raw []string) int {14 args := parse('update', raw, Options{15 flags: ['check', 'json', 'yes']16 }) or { return usage_error(err.msg()) }17 if args.positional.len > 0 {18 return usage_error('update takes no arguments, not "${args.positional_at(0)}"')19 }20 if args.json() && !args.flag('yes') && !args.flag('check') {21 22 23 return refuse(true, 'koh update --json needs --yes')24 }25 current := version26 platform := build.this_machine() or { return refuse(args.json(), err.msg()) }27 checked := update.check(current, platform) or { return refuse(args.json(), err.msg()) }28 latest := if checked.update {29 checked.available30 } else {31 if args.json() {32 emit_json(json2.encode(UpdateJson{33 current: current34 latest: current35 available: false36 }))37 } else {38 ok('koh ${current} is the newest — nothing to do')39 }40 return 041 }42 if args.flag('check') {43 if args.json() {44 emit_json(json2.encode(UpdateJson{45 current: current46 latest: latest.version47 available: true48 notes: latest.notes49 }))50 } else {51 ok('koh ${latest.version} is available — you have ${current}')52 if latest.notes != '' {53 note(latest.notes)54 }55 note('run "koh update" to install it')56 }57 58 return 159 }60 if !args.json() {61 note('${current} → ${latest.version}')62 if latest.notes != '' {63 note(latest.notes)64 }65 }66 confirm(args.flag('yes')) or { return refuse(args.json(), err.msg()) }67 target := update.install(latest) or { return refuse(args.json(), err.msg()) }68 if args.json() {69 emit_json(json2.encode(UpdateJson{70 current: current71 latest: latest.version72 available: true73 installed: target74 }))75 return 076 }77 ok('installed koh ${latest.version} as ${target}')78 return 079}8081struct UpdateJson {82 current string83 latest string84 available bool85 installed string86 notes string87}