Skip to content

Commit 58e491b

Browse files
authored
refactor: Tidy Argu processing (#135)
1 parent b4474d6 commit 58e491b

File tree

43 files changed

+342
-403
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+342
-403
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The `Unreleased` section name is replaced by the expected version of next releas
1515
### Changed
1616

1717
- Target `Equinox` v `4.0.0-rc.14.5`, `Propulsion` v `3.0.0-rc.9.11`, `FsCodec` v `3.0.0-rc.14.1` [#131](https://github.com/jet/dotnet-templates/pull/131)
18+
- Target `Argu` v `6.0.14` [#135](https://github.com/jet/dotnet-templates/pull/135)
1819

1920
### Removed
2021

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,9 @@ let main argv =
353353
try let args = Args.parse EnvVar.tryGet argv
354354
try Log.Logger <- LoggerConfiguration().Configure(verbose=args.Verbose).CreateLogger()
355355
try run args |> Async.RunSynchronously; 0
356-
with e when not (e :? MissingArg) -> Log.Fatal(e, "Exiting"); 2
356+
with e when not (e :? System.Threading.Tasks.TaskCanceledException) -> Log.Fatal(e, "Exiting"); 2
357357
finally Log.CloseAndFlush()
358-
with MissingArg msg -> eprintfn "%s" msg; 1
359-
| :? Argu.ArguParseException as e -> eprintfn "%s" e.Message; 1
358+
with :? Argu.ArguParseException as e -> eprintfn "%s" e.Message; 1
360359
| e -> eprintf "Exception %s" e.Message; 1
361360
```
362361

equinox-shipping/Watchdog.Lambda/Function.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ open System
1313
type Configuration(appName, ?tryGet) =
1414
let envVarTryGet = Environment.GetEnvironmentVariable >> Option.ofObj
1515
let tryGet = defaultArg tryGet envVarTryGet
16-
let get key = match tryGet key with Some value -> value | None -> failwithf "Missing Argument/Environment Variable %s" key
16+
let get key = match tryGet key with Some value -> value | None -> failwithf $"Missing Argument/Environment Variable %s{key}"
1717

1818
member _.DynamoRegion = tryGet Propulsion.DynamoStore.Lambda.Args.Dynamo.REGION
1919
member _.DynamoServiceUrl = get Propulsion.DynamoStore.Lambda.Args.Dynamo.SERVICE_URL

equinox-shipping/Watchdog/Args.fs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ module Args
44
open System
55
open FSharp.Control
66

7-
exception MissingArg of message: string with override this.Message = this.message
8-
let missingArg msg = raise (MissingArg msg)
9-
107
let [<Literal>] REGION = "EQUINOX_DYNAMO_REGION"
118
let [<Literal>] SERVICE_URL = "EQUINOX_DYNAMO_SERVICE_URL"
129
let [<Literal>] ACCESS_KEY = "EQUINOX_DYNAMO_ACCESS_KEY_ID"
@@ -16,20 +13,20 @@ let [<Literal>] INDEX_TABLE = "EQUINOX_DYNAMO_TABLE_INDEX"
1613

1714
type Configuration(tryGet: string -> string option) =
1815

16+
let get key = match tryGet key with Some value -> value | None -> failwith $"Missing Argument/Environment Variable %s{key}"
1917
member val tryGet = tryGet
20-
member _.get key = match tryGet key with Some value -> value | None -> missingArg $"Missing Argument/Environment Variable %s{key}"
2118

22-
member x.CosmosConnection = x.get "EQUINOX_COSMOS_CONNECTION"
23-
member x.CosmosDatabase = x.get "EQUINOX_COSMOS_DATABASE"
24-
member x.CosmosContainer = x.get "EQUINOX_COSMOS_CONTAINER"
19+
member _.CosmosConnection = get "EQUINOX_COSMOS_CONNECTION"
20+
member _.CosmosDatabase = get "EQUINOX_COSMOS_DATABASE"
21+
member _.CosmosContainer = get "EQUINOX_COSMOS_CONTAINER"
2522

26-
member x.DynamoServiceUrl = x.get SERVICE_URL
27-
member x.DynamoAccessKey = x.get ACCESS_KEY
28-
member x.DynamoSecretKey = x.get SECRET_KEY
29-
member x.DynamoTable = x.get TABLE
30-
member x.DynamoRegion = x.tryGet REGION
23+
member _.DynamoServiceUrl = get SERVICE_URL
24+
member _.DynamoAccessKey = get ACCESS_KEY
25+
member _.DynamoSecretKey = get SECRET_KEY
26+
member _.DynamoTable = get TABLE
27+
member _.DynamoRegion = tryGet REGION
3128

32-
member x.EventStoreConnection = x.get "EQUINOX_ES_CONNECTION"
29+
member _.EventStoreConnection = get "EQUINOX_ES_CONNECTION"
3330
member _.MaybeEventStoreConnection = tryGet "EQUINOX_ES_CONNECTION"
3431
member _.MaybeEventStoreCredentials = tryGet "EQUINOX_ES_CREDENTIALS"
3532

@@ -50,7 +47,7 @@ module Cosmos =
5047
| [<AltCommandLine "-rt">] RetriesWaitTime of float
5148
interface IArgParserTemplate with
5249
member p.Usage = p |> function
53-
| Verbose _ -> "request verbose logging."
50+
| Verbose -> "request verbose logging."
5451
| ConnectionMode _ -> "override the connection mode. Default: Direct."
5552
| Connection _ -> "specify a connection string for a Cosmos account. (optional if environment variable EQUINOX_COSMOS_CONNECTION specified)"
5653
| Database _ -> "specify a database name for Cosmos store. (optional if environment variable EQUINOX_COSMOS_DATABASE specified)"
@@ -60,15 +57,15 @@ module Cosmos =
6057
| RetriesWaitTime _ -> "specify max wait-time for retry when being throttled by Cosmos in seconds (default: 5)"
6158

6259
type Arguments(c: Configuration, p: ParseResults<Parameters>) =
63-
let connection = p.TryGetResult Connection |> Option.defaultWith (fun () -> c.CosmosConnection)
60+
let connection = p.GetResult(Connection, fun () -> c.CosmosConnection)
6461
let discovery = Equinox.CosmosStore.Discovery.ConnectionString connection
6562
let mode = p.TryGetResult ConnectionMode
6663
let timeout = p.GetResult(Timeout, 5.) |> TimeSpan.FromSeconds
6764
let retries = p.GetResult(Retries, 1)
6865
let maxRetryWaitTime = p.GetResult(RetriesWaitTime, 5.) |> TimeSpan.FromSeconds
6966
let connector = Equinox.CosmosStore.CosmosStoreConnector(discovery, timeout, retries, maxRetryWaitTime, ?mode = mode)
70-
let database = p.TryGetResult Database |> Option.defaultWith (fun () -> c.CosmosDatabase)
71-
let container = p.TryGetResult Container |> Option.defaultWith (fun () -> c.CosmosContainer)
67+
let database = p.GetResult(Database, fun () -> c.CosmosDatabase)
68+
let container = p.GetResult(Container, fun () -> c.CosmosContainer)
7269
member val Verbose = p.Contains Verbose
7370
member _.Connect() = connector.ConnectContext(database, container)
7471

@@ -102,9 +99,9 @@ module Dynamo =
10299
| Some systemName ->
103100
Choice1Of2 systemName
104101
| None ->
105-
let serviceUrl = p.TryGetResult ServiceUrl |> Option.defaultWith (fun () -> c.DynamoServiceUrl)
106-
let accessKey = p.TryGetResult AccessKey |> Option.defaultWith (fun () -> c.DynamoAccessKey)
107-
let secretKey = p.TryGetResult SecretKey |> Option.defaultWith (fun () -> c.DynamoSecretKey)
102+
let serviceUrl = p.GetResult(ServiceUrl, fun () -> c.DynamoServiceUrl)
103+
let accessKey = p.GetResult(AccessKey, fun () -> c.DynamoAccessKey)
104+
let secretKey = p.GetResult(SecretKey, fun () -> c.DynamoSecretKey)
108105
Choice2Of2 (serviceUrl, accessKey, secretKey)
109106
let connector = let timeout = p.GetResult(RetriesTimeoutS, 5.) |> TimeSpan.FromSeconds
110107
let retries = p.GetResult(Retries, 1)
@@ -113,7 +110,7 @@ module Dynamo =
113110
Equinox.DynamoStore.DynamoStoreConnector(systemName, timeout, retries)
114111
| Choice2Of2 (serviceUrl, accessKey, secretKey) ->
115112
Equinox.DynamoStore.DynamoStoreConnector(serviceUrl, accessKey, secretKey, timeout, retries)
116-
let table = p.TryGetResult Table |> Option.defaultWith (fun () -> c.DynamoTable)
113+
let table = p.GetResult(Table, fun () -> c.DynamoTable)
117114
member _.Connect() = connector.CreateClient().CreateContext("Main", table)
118115

119116
type [<RequireQualifiedAccess; NoComparison; NoEquality>]

equinox-shipping/Watchdog/Program.fs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open System
66
module Args =
77

88
open Argu
9-
[<NoEquality; NoComparison>]
9+
[<NoEquality; NoComparison; RequireSubcommand>]
1010
type Parameters =
1111
| [<AltCommandLine "-V"; Unique>] Verbose
1212
| [<AltCommandLine "-g"; Mandatory>] ProcessorName of string
@@ -17,9 +17,9 @@ module Args =
1717
| [<AltCommandLine "-i"; Unique>] IdleDelayMs of int
1818
| [<AltCommandLine "-W"; Unique>] WakeForResults
1919

20-
| [<CliPrefix(CliPrefix.None); Unique; Last>] Cosmos of ParseResults<SourceArgs.Cosmos.Parameters>
21-
| [<CliPrefix(CliPrefix.None); Unique; Last>] Dynamo of ParseResults<SourceArgs.Dynamo.Parameters>
22-
| [<CliPrefix(CliPrefix.None); Unique; Last>] Esdb of ParseResults<SourceArgs.Esdb.Parameters>
20+
| [<CliPrefix(CliPrefix.None)>] Cosmos of ParseResults<SourceArgs.Cosmos.Parameters>
21+
| [<CliPrefix(CliPrefix.None)>] Dynamo of ParseResults<SourceArgs.Dynamo.Parameters>
22+
| [<CliPrefix(CliPrefix.None)>] Esdb of ParseResults<SourceArgs.Esdb.Parameters>
2323
interface IArgParserTemplate with
2424
member p.Usage = p |> function
2525
| Verbose -> "request Verbose Logging. Default: off."
@@ -29,7 +29,7 @@ module Args =
2929
| TimeoutS _ -> "Timeout (in seconds) before Watchdog should step in to process transactions. Default: 10."
3030

3131
| IdleDelayMs _ -> "Idle delay for scheduler. Default 1000ms"
32-
| WakeForResults _ -> "Wake for all results to provide optimal throughput"
32+
| WakeForResults -> "Wake for all results to provide optimal throughput"
3333

3434
| Cosmos _ -> "specify CosmosDB parameters."
3535
| Dynamo _ -> "specify DynamoDB input parameters"
@@ -55,7 +55,7 @@ module Args =
5555
| Cosmos a -> Choice1Of3 <| SourceArgs.Cosmos.Arguments(c, a)
5656
| Dynamo a -> Choice2Of3 <| SourceArgs.Dynamo.Arguments(c, a)
5757
| Esdb a -> Choice3Of3 <| SourceArgs.Esdb.Arguments(c, a)
58-
| a -> Args.missingArg $"Unexpected Store subcommand %A{a}"
58+
| a -> failwith $"Unexpected Store subcommand %A{a}"
5959
member x.VerboseStore = match x.Store with
6060
| Choice1Of3 s -> s.Verbose
6161
| Choice2Of3 s -> s.Verbose
@@ -128,8 +128,7 @@ let main argv =
128128
try let args = Args.parse EnvVar.tryGet argv
129129
try Log.Logger <- LoggerConfiguration().Configure(verbose = args.Verbose).CreateLogger()
130130
try run args |> Async.RunSynchronously; 0
131-
with e when not (e :? Args.MissingArg) && not (e :? System.Threading.Tasks.TaskCanceledException) -> Log.Fatal(e, "Exiting"); 2
131+
with e when not (e :? System.Threading.Tasks.TaskCanceledException) -> Log.Fatal(e, "Exiting"); 2
132132
finally Log.CloseAndFlush()
133-
with Args.MissingArg msg -> eprintfn "%s" msg; 1
134-
| :? Argu.ArguParseException as e -> eprintfn "%s" e.Message; 1
135-
| e -> eprintf "Exception %s" e.Message; 1
133+
with :? Argu.ArguParseException as e -> eprintfn $"%s{e.Message}"; 1
134+
| e -> eprintf $"Exception %s{e.Message}"; 1

equinox-shipping/Watchdog/SourceArgs.fs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ module Cosmos =
4242
| LagFreqM _ -> "specify frequency (minutes) to dump lag stats. Default: 1"
4343

4444
type Arguments(c: Args.Configuration, p: ParseResults<Parameters>) =
45-
let discovery = p.TryGetResult Connection |> Option.defaultWith (fun () -> c.CosmosConnection) |> Equinox.CosmosStore.Discovery.ConnectionString
45+
let discovery = p.GetResult(Connection, fun () -> c.CosmosConnection) |> Equinox.CosmosStore.Discovery.ConnectionString
4646
let mode = p.TryGetResult ConnectionMode
4747
let timeout = p.GetResult(Timeout, 5.) |> TimeSpan.FromSeconds
4848
let retries = p.GetResult(Retries, 9)
4949
let maxRetryWaitTime = p.GetResult(RetriesWaitTime, 30.) |> TimeSpan.FromSeconds
5050
let connector = Equinox.CosmosStore.CosmosStoreConnector(discovery, timeout, retries, maxRetryWaitTime, ?mode = mode)
51-
let database = p.TryGetResult Database |> Option.defaultWith (fun () -> c.CosmosDatabase)
52-
let containerId = p.TryGetResult Container |> Option.defaultWith (fun () -> c.CosmosContainer)
51+
let database = p.GetResult(Database, fun () -> c.CosmosDatabase)
52+
let containerId = p.GetResult(Container, fun () -> c.CosmosContainer)
5353
let leaseContainerId = p.GetResult(LeaseContainer, containerId + "-aux")
5454
let fromTail = p.Contains FromTail
5555
let maxItems = p.TryGetResult MaxItems
@@ -99,9 +99,9 @@ module Dynamo =
9999
| Some systemName ->
100100
Choice1Of2 systemName
101101
| None ->
102-
let serviceUrl = p.TryGetResult ServiceUrl |> Option.defaultWith (fun () -> c.DynamoServiceUrl)
103-
let accessKey = p.TryGetResult AccessKey |> Option.defaultWith (fun () -> c.DynamoAccessKey)
104-
let secretKey = p.TryGetResult SecretKey |> Option.defaultWith (fun () -> c.DynamoSecretKey)
102+
let serviceUrl = p.GetResult(ServiceUrl, fun () -> c.DynamoServiceUrl)
103+
let accessKey = p.GetResult(AccessKey, fun () -> c.DynamoAccessKey)
104+
let secretKey = p.GetResult(SecretKey, fun () -> c.DynamoSecretKey)
105105
Choice2Of2 (serviceUrl, accessKey, secretKey)
106106
let connector = let timeout = p.GetResult(RetriesTimeoutS, 60.) |> TimeSpan.FromSeconds
107107
let retries = p.GetResult(Retries, 9)
@@ -110,9 +110,9 @@ module Dynamo =
110110
Equinox.DynamoStore.DynamoStoreConnector(systemName, timeout, retries)
111111
| Choice2Of2 (serviceUrl, accessKey, secretKey) ->
112112
Equinox.DynamoStore.DynamoStoreConnector(serviceUrl, accessKey, secretKey, timeout, retries)
113-
let table = p.TryGetResult Table |> Option.defaultWith (fun () -> c.DynamoTable)
113+
let table = p.GetResult(Table, fun () -> c.DynamoTable)
114114
let indexSuffix = p.GetResult(IndexSuffix, "-index")
115-
let indexTable = p.TryGetResult IndexTable |> Option.orElseWith (fun () -> c.DynamoIndexTable) |> Option.defaultWith (fun () -> table + indexSuffix)
115+
let indexTable = p.GetResult(IndexTable, fun () -> defaultArg c.DynamoIndexTable (table + indexSuffix))
116116
let fromTail = p.Contains FromTail
117117
let tailSleepInterval = TimeSpan.FromMilliseconds 500.
118118
let batchSizeCutoff = p.GetResult(MaxItems, 100)
@@ -142,9 +142,9 @@ module Esdb =
142142
Propulsion.Feed.ReaderCheckpoint.CosmosStore.create Store.Metrics.log (consumerGroup, checkpointInterval) (context, cache)
143143
| Store.Config.Dynamo (context, cache) ->
144144
Propulsion.Feed.ReaderCheckpoint.DynamoStore.create Store.Metrics.log (consumerGroup, checkpointInterval) (context, cache)
145-
| Store.Config.Memory _ | Store.Config.Esdb _ -> Args.missingArg "Unexpected store type"
145+
| Store.Config.Memory _ | Store.Config.Esdb _ -> failwith "Unexpected store type"
146146

147-
type [<NoEquality; NoComparison>] Parameters =
147+
type [<NoEquality; NoComparison; RequireSubcommand>] Parameters =
148148
| [<AltCommandLine "-V">] Verbose
149149
| [<AltCommandLine "-c">] Connection of string
150150
| [<AltCommandLine "-p"; Unique>] Credentials of string
@@ -154,8 +154,8 @@ module Esdb =
154154
| [<AltCommandLine "-b"; Unique>] MaxItems of int
155155
| [<AltCommandLine "-Z"; Unique>] FromTail
156156

157-
| [<CliPrefix(CliPrefix.None); Unique(*ExactlyOnce is not supported*); Last>] Cosmos of ParseResults<Args.Cosmos.Parameters>
158-
| [<CliPrefix(CliPrefix.None); Unique(*ExactlyOnce is not supported*); Last>] Dynamo of ParseResults<Args.Dynamo.Parameters>
157+
| [<CliPrefix(CliPrefix.None)>] Cosmos of ParseResults<Args.Cosmos.Parameters>
158+
| [<CliPrefix(CliPrefix.None)>] Dynamo of ParseResults<Args.Dynamo.Parameters>
159159
interface IArgParserTemplate with
160160
member p.Usage = p |> function
161161
| Verbose -> "Include low level Store logging."
@@ -174,7 +174,7 @@ module Esdb =
174174
let startFromTail = p.Contains FromTail
175175
let maxItems = p.GetResult(MaxItems, 100)
176176
let tailSleepInterval = TimeSpan.FromSeconds 0.5
177-
let connectionStringLoggable = p.TryGetResult Connection |> Option.defaultWith (fun () -> c.EventStoreConnection)
177+
let connectionStringLoggable = p.GetResult(Connection, fun () -> c.EventStoreConnection)
178178
let credentials = p.TryGetResult Credentials |> Option.orElseWith (fun () -> c.MaybeEventStoreCredentials)
179179
let discovery = match credentials with Some x -> String.Join(";", connectionStringLoggable, x) | None -> connectionStringLoggable
180180
|> Equinox.EventStoreDb.Discovery.ConnectionString
@@ -193,7 +193,7 @@ module Esdb =
193193
match p.GetSubCommand() with
194194
| Cosmos cosmos -> Args.TargetStoreArgs.Cosmos (Args.Cosmos.Arguments(c, cosmos))
195195
| Dynamo dynamo -> Args.TargetStoreArgs.Dynamo (Args.Dynamo.Arguments(c, dynamo))
196-
| _ -> Args.missingArg "Must specify `cosmos` or `dynamo` target store when source is `esdb`"
196+
| _ -> p.Raise "Must specify `cosmos` or `dynamo` target store when source is `esdb`"
197197

198198
member _.MonitoringParams(log: ILogger) =
199199
log.Information("EventStoreSource MaxItems {maxItems} ", maxItems)

equinox-shipping/Watchdog/Watchdog.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Argu" Version="6.1.1" />
20+
<PackageReference Include="Argu" Version="6.1.4" />
2121
<PackageReference Include="Propulsion.CosmosStore" Version="3.0.0-rc.9.11" />
2222
<PackageReference Include="Propulsion.DynamoStore" Version="3.0.0-rc.9.11" />
2323
<PackageReference Include="Propulsion.EventStoreDb" Version="3.0.0-rc.9.11" />

0 commit comments

Comments
 (0)