cli.v

3.1 KB · raw

1//! Command line entry: dispatch table, usage text and exit codes.2//!3//! Exit codes are fixed across every command: 0 success, 1 refusal or error,4//! 2 bad usage. Each command returns its code; nothing else calls `exit`.56module cli78import src.fmt910// Read from VERSION at build time, so the binary and the manifest cannot drift.11pub const version = $embed_file('../../VERSION').to_string().trim_space()1213// The help screen. It is built rather than embedded in a constant, because the14// colour follows NO_COLOR, KOH_NO_COLOR and whether the output is a terminal.15// Flags are not shown here: `koh man <command>` documents every one.16pub fn usage_text() string {17	commands := [18		['save', 'snapshot your work'],19		['checkpoint', 'mark a place to come back to'],20		['status', 'where things stand'],21		['view', 'browse saves and checkpoints'],22		['diff', 'what changed'],23		['undo', 'step back'],24		['revert', 'throw away unsaved changes'],25		['load', 'go to any save'],26		['offer', 'send to your remote'],27		['steal', 'bring in from your remote'],28	]29	mut text := '${fmt.light('koh ${version} — version control for the solo developer')}\n'30	// A blank line between commands, so the eye lands on one line at a time, and31	// the list grows as commands land.32	for row in commands {33		text += '\n  ${fmt.gold(row[0])}${' '.repeat(12 - row[0].len)}${fmt.muted(row[1])}\n'34	}35	text += '\n  ${fmt.dim('also: remote · flag · pluck · peek · build · update')}\n'36	text += '        ${fmt.dim('init · doctor · ignore · strip · migrate')}\n'37	text += '\n  ${fmt.dim('koh man <command>  for options and examples')}\n'38	text += '\n  ${fmt.dim('Asha Software · asha.software')}\n'39	return text40}4142pub fn run(argv []string) int {43	if argv.len < 2 {44		eprintln(usage_text())45		return 246	}47	if needs_migration(argv) {48		return refuse('--json' in argv, 'this is a Koh 2 repo — run "koh migrate" first')49	}50	rest := argv[2..]51	match argv[1] {52		'--version', '-v' {53			println('koh ${version}')54			return 055		}56		'--help', '-h' {57			println(usage_text())58			return 059		}60		'init' {61			return cmd_init(rest)62		}63		'save' {64			return cmd_save(rest, false)65		}66		'checkpoint' {67			return cmd_save(rest, true)68		}69		'status' {70			return cmd_status(rest)71		}72		'view' {73			return cmd_view(rest)74		}75		'diff' {76			return cmd_diff(rest)77		}78		'undo' {79			return cmd_undo(rest)80		}81		'revert' {82			return cmd_revert(rest)83		}84		'load' {85			return cmd_load(rest)86		}87		'flag' {88			return cmd_flag(rest)89		}90		'pluck' {91			return cmd_pluck(rest)92		}93		'peek' {94			return cmd_peek(rest)95		}96		'doctor' {97			return cmd_doctor(rest)98		}99		'ignore' {100			return cmd_ignore(rest)101		}102		'strip' {103			return cmd_strip(rest)104		}105		'remote' {106			return cmd_remote(rest)107		}108		'offer' {109			return cmd_offer(rest)110		}111		'steal' {112			return cmd_steal(rest)113		}114		'build' {115			return cmd_build(rest)116		}117		'update' {118			return cmd_update(rest)119		}120		'migrate' {121			return cmd_migrate(rest)122		}123		'man', 'help' {124			return cmd_man(rest)125		}126		else {127			return usage_error('unknown command "${argv[1]}" — run "koh --help" for the command list')128		}129	}130}