A macOS utility to track home-manager JJ repo status
at main 78 lines 1.8 kB view raw
1import Foundation 2import HMStatus 3 4@main 5struct HMStatusCLI { 6 static func main() async { 7 let args = CommandLine.arguments.dropFirst() 8 9 var shortOutput = false 10 var failOnOutOfSync = false 11 var repoPath: String? 12 13 for arg in args { 14 switch arg { 15 case "--short": 16 shortOutput = true 17 case "--fail": 18 failOnOutOfSync = true 19 case "--help", "-h": 20 printUsage() 21 exit(0) 22 default: 23 if arg.hasPrefix("-") { 24 fputs("Unknown option: \(arg)\n", stderr) 25 printUsage() 26 exit(2) 27 } 28 repoPath = arg 29 } 30 } 31 32 let path = repoPath ?? defaultRepoPath() 33 34 let status = await StatusChecker.check(repoPath: path) 35 36 if status.hasError { 37 fputs("\(status.statusDescription)\n", stderr) 38 exit(2) 39 } 40 41 if shortOutput { 42 print(status.menuBarLabel) 43 } else { 44 print(status.statusDescription) 45 } 46 47 if failOnOutOfSync && !status.isSynced { 48 exit(1) 49 } 50 } 51 52 static func defaultRepoPath() -> String { 53 let home = FileManager.default.homeDirectoryForCurrentUser.path 54 return "\(home)/.config/home-manager" 55 } 56 57 static func printUsage() { 58 let usage = """ 59 Usage: hm-status [OPTIONS] [PATH] 60 61 Check jj repository status relative to trunk. 62 63 Arguments: 64 PATH Path to jj repository (default: ~/.config/home-manager) 65 66 Options: 67 --short Print compact status (e.g. ✓, ↑2●, ↓3) 68 --fail Exit with code 1 when out of sync 69 --help Show this help message 70 71 Exit codes: 72 0 Success (or out of sync without --fail) 73 1 Out of sync (only with --fail) 74 2 Error (jj not found, invalid repo, etc.) 75 """ 76 print(usage) 77 } 78}