|
| 1 | +// |
| 2 | +// TypeCommands.swift |
| 3 | +// utiluti |
| 4 | +// |
| 5 | +// Created by Armin Briegel on 2022-11-10. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import ArgumentParser |
| 10 | + |
| 11 | +struct TypeCommands: ParsableCommand { |
| 12 | + static var configuration = CommandConfiguration( |
| 13 | + commandName: "type", |
| 14 | + abstract: "Manipulate default file type handlers", |
| 15 | + subcommands: [Get.self, List.self, Set.self], |
| 16 | + defaultSubcommand: Get.self |
| 17 | + ) |
| 18 | + |
| 19 | + struct UTIdentifier: ParsableArguments { |
| 20 | + @Argument(help: "universal type identifier, e.g. 'public.html'") var utidentifier: String |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + struct Get: ParsableCommand { |
| 25 | + static var configuration |
| 26 | + = CommandConfiguration(abstract: "Get the path to the default application.") |
| 27 | + |
| 28 | + @OptionGroup var args: UTIdentifier |
| 29 | + |
| 30 | + func run() { |
| 31 | + let appURL = LSKit.defaultAppURL(forTypeIdentifier: args.utidentifier) |
| 32 | + print(appURL?.path ?? "no default app found") |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + struct List: ParsableCommand { |
| 37 | + static var configuration |
| 38 | + = CommandConfiguration(abstract: "List all applications that can handle this type identifier.") |
| 39 | + |
| 40 | + @OptionGroup var args: UTIdentifier |
| 41 | + |
| 42 | + func run() { |
| 43 | + let appURLs = LSKit.appURLs(forTypeIdentifier: args.utidentifier) |
| 44 | + |
| 45 | + for appURL in appURLs { |
| 46 | + print(appURL.path) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + struct Set: ParsableCommand { |
| 52 | + static var configuration |
| 53 | + = CommandConfiguration(abstract: "Set the default app for this type identifier.") |
| 54 | + |
| 55 | + @OptionGroup var args: UTIdentifier |
| 56 | + @Argument var identifier: String |
| 57 | + |
| 58 | + func run() { |
| 59 | + let result = LSKit.setDefaultApp(identifier: identifier, forTypeIdentifier: args.utidentifier) |
| 60 | + |
| 61 | + if result == 0 { |
| 62 | + print("set \(identifier) for \(args.utidentifier)") |
| 63 | + } else { |
| 64 | + print("cannot set default app for \(args.utidentifier) (error \(result))") |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments