cmd_update.v

2.3 KB · raw

1//! `koh update` — put a newer published koh in place of this one (§12).2//!3//! Nothing is installed without being asked, and nothing is removed: the new4//! binary replaces the running one by rename, which is the step that makes the5//! change visible.67module 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		// The question cannot be asked inside a JSON document, so an install22		// asked for in JSON has to be asked for with --yes as well.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		// `--check` reports and says so with its exit code, so a script can ask.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}