Files
puregym-unofficial-dotnet/PureGym.App/AuthArg.fs
2023-12-27 20:21:08 +00:00

42 lines
1.7 KiB
Forth

namespace PureGym.App
open Argu
open PureGym
type AuthArg =
| [<Unique ; CustomAppSettings "PUREGYM_BEARER_TOKEN">] Bearer_Token of string
| [<Unique ; EqualsAssignmentOrSpaced>] User_Email of string
| [<Unique ; EqualsAssignmentOrSpaced>] Pin of string
| [<GatherUnrecognized>] Others of string
interface IArgParserTemplate with
member s.Usage =
match s with
| AuthArg.Bearer_Token _ -> "A bearer token for the PureGym API"
| AuthArg.User_Email _ -> "PureGym user's email address"
| AuthArg.Pin _ -> "Eight-digit PureGym user's PIN"
| AuthArg.Others _ -> "<specific args for command>"
static member Parse (args : ParseResults<AuthArg>) : Result<Auth * string[], ArguParseException> =
let unmatchedArgs = args.GetResults AuthArg.Others |> List.toArray
match
args.TryGetResult AuthArg.User_Email, args.TryGetResult AuthArg.Pin, args.TryGetResult AuthArg.Bearer_Token
with
| Some email, Some pin, _ ->
let auth =
Auth.User
{
Username = email
Pin = pin
}
Ok (auth, unmatchedArgs)
| Some _email, None, _ -> failwith "Supplied --user-email but no --pin; either both or neither are required."
| None, Some _pin, _ -> failwith "Supplied --pin but no --user-email; either both or neither are required."
| None, None, None ->
failwith "No creds given: expected at least one of `--bearer-token` or `--user-email --pin`"
| None, None, Some token ->
let auth = Auth.Token (AuthToken.ofBearerToken token)
Ok (auth, unmatchedArgs)