module Osync.Program open Argu [] type ExtractArgs = | [] Dir of path: string interface IArgParserTemplate with member this.Usage = match this with | Dir _ -> "Override the osu! data directory path." [] type SyncArgs = | [] From of host: string | [] To of host: string | From_Dir of path: string | To_Dir of path: string | From_Osync of command: string | To_Osync of command: string interface IArgParserTemplate with member this.Usage = match this with | From _ -> "Source host (SSH hostname or omit for localhost)." | To _ -> "Destination host (SSH hostname or omit for localhost)." | From_Dir _ -> "Override the osu! data directory on the source side." | To_Dir _ -> "Override the osu! data directory on the destination side." | From_Osync _ -> "Remote shell command to run osync on the source host." | To_Osync _ -> "Remote shell command to run osync on the destination host." [] type DiffArgs = | [] Between of host: string | [] And of host: string | Between_Dir of path: string | And_Dir of path: string | Between_Osync of command: string | And_Osync of command: string interface IArgParserTemplate with member this.Usage = match this with | Between _ -> "First host to compare (SSH hostname or omit for localhost)." | And _ -> "Second host to compare (SSH hostname or omit for localhost)." | Between_Dir _ -> "Override the osu! data directory for the first host." | And_Dir _ -> "Override the osu! data directory for the second host." | Between_Osync _ -> "Remote shell command to run osync on the first host." | And_Osync _ -> "Remote shell command to run osync on the second host." [] type ImportArgs = | [] Dir of path: string | Source_Realm of path: string interface IArgParserTemplate with member this.Usage = match this with | Dir _ -> "Override the osu! data directory path." | Source_Realm _ -> "Path to source realm file to import from." let version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version |> fun v -> $"{v.Major}.{v.Minor}.{v.Build}" type OsyncArgs = | [] Sync of ParseResults | [] Diff of ParseResults | [] Extract of ParseResults | [] Import of ParseResults | [] Version interface IArgParserTemplate with member this.Usage = match this with | Sync _ -> "Synchronize beatmaps and settings between two hosts." | Diff _ -> "Show differences between two hosts without making changes." | Extract _ -> "Extract local state as JSON (used internally over SSH)." | Import _ -> "Import missing beatmaps and skins from a source realm file." | Version -> "Show version." let private runExtract (results: ParseResults) : int = let dirOverride = results.TryGetResult ExtractArgs.Dir match Sync.extractLocal dirOverride with | Ok state -> let json = Sync.serializeExtractJson state printfn "%s" json 0 | Error e -> eprintfn "Error: %s" e 1 let private runSync (results: ParseResults) : int = let fromHost = results.TryGetResult SyncArgs.From let toHost = results.TryGetResult SyncArgs.To let fromDir = results.TryGetResult SyncArgs.From_Dir let toDir = results.TryGetResult SyncArgs.To_Dir if fromHost.IsNone && toHost.IsNone && fromDir.IsNone && toDir.IsNone then eprintfn "Error: At least one of --from or --to must be provided." 1 else let fromResolved = match fromHost with | Some h -> Sync.Remote h | None -> Sync.Localhost let toResolved = match toHost with | Some h -> Sync.Remote h | None -> Sync.Localhost let fromLabel = match fromHost with | Some h -> h | None -> "localhost" let toLabel = match toHost with | Some h -> h | None -> "localhost" Sync.run { Mode = Sync.Sync FromHost = fromResolved ToHost = toResolved FromDir = fromDir ToDir = toDir FromOsync = results.GetResult(SyncArgs.From_Osync, "osync") ToOsync = results.GetResult(SyncArgs.To_Osync, "osync") FromLabel = fromLabel ToLabel = toLabel } let private runDiff (results: ParseResults) : int = let betweenHost = results.TryGetResult DiffArgs.Between let andHost = results.TryGetResult DiffArgs.And let betweenDir = results.TryGetResult DiffArgs.Between_Dir let andDir = results.TryGetResult DiffArgs.And_Dir if betweenHost.IsNone && andHost.IsNone && betweenDir.IsNone && andDir.IsNone then eprintfn "Error: At least one of --between or --and must be provided." 1 else let fromResolved = match betweenHost with | Some h -> Sync.Remote h | None -> Sync.Localhost let toResolved = match andHost with | Some h -> Sync.Remote h | None -> Sync.Localhost let fromLabel = match betweenHost with | Some h -> h | None -> "localhost" let toLabel = match andHost with | Some h -> h | None -> "localhost" Sync.run { Mode = Sync.Diff FromHost = fromResolved ToHost = toResolved FromDir = betweenDir ToDir = andDir FromOsync = results.GetResult(DiffArgs.Between_Osync, "osync") ToOsync = results.GetResult(DiffArgs.And_Osync, "osync") FromLabel = fromLabel ToLabel = toLabel } let private runImport (results: ParseResults) : int = match results.TryGetResult ImportArgs.Source_Realm with | None -> eprintfn "Error: --source-realm is required." 1 | Some sourceRealm -> let dirOverride = results.TryGetResult ImportArgs.Dir Sync.runImport sourceRealm dirOverride [] let main argv = let parser = ArgumentParser.Create(programName = "osync") try let results = parser.ParseCommandLine(inputs = argv, raiseOnUsage = true) if results.Contains OsyncArgs.Version then printfn "osync %s" version 0 else match results.GetSubCommand() with | Sync syncResults -> runSync syncResults | Diff diffResults -> runDiff diffResults | Extract extractResults -> runExtract extractResults | Import importResults -> runImport importResults | Version -> 0 with | :? ArguParseException as ex -> eprintfn "%s" ex.Message if ex.ErrorCode = ErrorCode.HelpText then 0 else 1 | ex -> eprintfn "Error: %s" ex.Message 1