cmd_update.v

2.5 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 install asked22		// for in JSON has to be asked for with --yes as well. That is a mistake in23		// the command line, not a refusal of the work.24		return usage_error('--json needs --yes when it would install')25	}26	current := version27	platform := build.this_machine() or { return refuse(args.json(), err.msg()) }28	checked := update.check(current, platform) or { return refuse(args.json(), err.msg()) }29	latest := if checked.update {30		checked.available31	} else {32		if args.json() {33			emit_json(json2.encode(UpdateJson{34				current:   current35				latest:    current36				available: false37			}))38		} else {39			ok('koh ${current} is the newest — nothing to do')40		}41		return 042	}43	if args.flag('check') {44		if args.json() {45			emit_json(json2.encode(UpdateJson{46				current:   current47				latest:    latest.version48				available: true49				notes:     latest.notes50			}))51		} else {52			ok('koh ${latest.version} is available — you have ${current}')53			if latest.notes != '' {54				note(latest.notes)55			}56			note('run "koh update" to install it')57		}58		// `--check` reports and says so with its exit code, so a script can ask.59		return 160	}61	if !args.json() {62		note('${current} → ${latest.version}')63		if latest.notes != '' {64			note(latest.notes)65		}66	}67	// A "no" at the question is a refusal, not a reason to carry on.68	approved := confirm(args.flag('yes')) or { return refuse(args.json(), err.msg()) }69	if !approved {70		return refuse(args.json(), 'not updating, then')71	}72	target := update.install(latest) or { return refuse(args.json(), err.msg()) }73	if args.json() {74		emit_json(json2.encode(UpdateJson{75			current:   current76			latest:    latest.version77			available: true78			installed: target79		}))80		return 081	}82	ok('installed koh ${latest.version} as ${target}')83	return 084}8586struct UpdateJson {87	current   string88	latest    string89	available bool90	installed string91	notes     string92}