main.lua

  1#!/usr/bin/env lua
  2
  3-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  4--
  5-- SPDX-License-Identifier: GPL-3.0-or-later
  6
  7if _VERSION < "Lua 5.2" then
  8	io.stderr:write("error: wt requires Lua 5.2 or later\n")
  9	os.exit(1)
 10end
 11
 12local exit = require("wt.exit")
 13local shell = require("wt.shell")
 14local path_mod = require("wt.path")
 15local git_mod = require("wt.git")
 16local config_mod = require("wt.config")
 17local hooks_mod = require("wt.hooks")
 18local help_mod = require("wt.help")
 19local fetch_mod = require("wt.cmd.fetch")
 20local list_mod = require("wt.cmd.list")
 21local remove_mod = require("wt.cmd.remove")
 22local add_mod = require("wt.cmd.add")
 23local new_mod = require("wt.cmd.new")
 24local clone_mod = require("wt.cmd.clone")
 25local init_mod = require("wt.cmd.init")
 26
 27-- Main entry point
 28
 29local function main()
 30	local command = arg[1]
 31
 32	if not command or command == "help" or command == "--help" or command == "-h" then
 33		help_mod.print_usage()
 34		os.exit(exit.EXIT_SUCCESS)
 35	end
 36
 37	---@cast command string
 38
 39	-- Collect remaining args
 40	local subargs = {}
 41	for i = 2, #arg do
 42		table.insert(subargs, arg[i])
 43	end
 44
 45	-- Check for --help on any command
 46	if subargs[1] == "--help" or subargs[1] == "-h" then
 47		help_mod.show_command_help(command)
 48	end
 49
 50	if command == "c" then
 51		clone_mod.cmd_clone(subargs)
 52	elseif command == "n" then
 53		new_mod.cmd_new(subargs)
 54	elseif command == "a" then
 55		add_mod.cmd_add(subargs)
 56	elseif command == "r" then
 57		remove_mod.cmd_remove(subargs)
 58	elseif command == "l" then
 59		list_mod.cmd_list()
 60	elseif command == "f" then
 61		fetch_mod.cmd_fetch()
 62	elseif command == "init" then
 63		init_mod.cmd_init(subargs)
 64	else
 65		shell.die("unknown command: " .. command)
 66	end
 67end
 68
 69-- Export for testing when required as module
 70if pcall(debug.getlocal, 4, 1) then
 71	---@diagnostic disable: duplicate-set-field
 72	return {
 73		-- URL/project parsing
 74		extract_project_name = config_mod.extract_project_name,
 75		resolve_url_template = config_mod.resolve_url_template,
 76		-- Path manipulation
 77		branch_to_path = path_mod.branch_to_path,
 78		split_path = path_mod.split_path,
 79		relative_path = path_mod.relative_path,
 80		path_inside = path_mod.path_inside,
 81		-- Config loading
 82		load_global_config = config_mod.load_global_config,
 83		load_project_config = config_mod.load_project_config,
 84		-- Git output parsing (testable without git)
 85		parse_branch_remotes = git_mod.parse_branch_remotes,
 86		parse_worktree_list = git_mod.parse_worktree_list,
 87		escape_pattern = path_mod.escape_pattern,
 88		-- Hook helpers (re-exported from wt.hooks)
 89		summarize_hooks = hooks_mod.summarize_hooks,
 90		load_hook_permissions = hooks_mod.load_hook_permissions,
 91		save_hook_permissions = hooks_mod.save_hook_permissions,
 92		run_hooks = hooks_mod.run_hooks,
 93		-- Project root detection (re-exported from wt.git)
 94		find_project_root = git_mod.find_project_root,
 95		detect_source_worktree = git_mod.detect_source_worktree,
 96		-- Command execution (for integration tests)
 97		run_cmd = shell.run_cmd,
 98		run_cmd_silent = shell.run_cmd_silent,
 99		-- Exit codes (re-exported from wt.exit)
100		EXIT_SUCCESS = exit.EXIT_SUCCESS,
101		EXIT_USER_ERROR = exit.EXIT_USER_ERROR,
102		EXIT_SYSTEM_ERROR = exit.EXIT_SYSTEM_ERROR,
103	}
104end
105
106main()