-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
153 lines (131 loc) · 5.93 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
namespace Pushbullet
open System
open Resources
open Argu
open Arguments
module Program =
let toOption x =
match x with
| null
| "" -> None
| _ -> Some x
let (|Int|_|) str =
match System.Int32.TryParse(str: string) with
| (true, int) -> Some int
| _ -> None
let (|Positive|Negative|Neither|) =
function
| "true"
| "1"
| "mute" -> Positive
| "false"
| "0"
| "unmute" -> Negative
| _ -> Neither
let valueToBool =
function
| Positive -> Some true
| Negative -> Some false
| Neither -> None
let getDeviceFromIndexOrDeviceId device =
match device with
| Int i -> i |> DeviceCommands.GetDeviceCommand |> DeviceCommands.getDeviceId
| _ -> device
let getDeviceIdFromResult result =
match result with
| Some v -> v |> getDeviceFromIndexOrDeviceId |> toOption
| None -> None
let runCommands (parser: ArgumentParser<CliArguments>) (args: string array) =
let parsingResult = parser.Parse args
if SystemCommands.getKey () <> "" then
match parsingResult.GetAllResults() with
| [ Key arg ] ->
match arg with
| Some a -> a |> SystemCommands.SetKeyCommand |> SystemCommands.setKey
| None -> SystemCommands.getKey ()
| [ Profile ] -> SystemCommands.getProfile ()
| [ Limits ] -> SystemCommands.getLimits ()
| [ Grants ] -> SystemCommands.listGrants ()
| [ PushInfo arg ] -> arg |> PushCommands.GetPushCommand |> PushCommands.getSinglePush
| [ Push subCommand ] ->
let deviceId = subCommand.TryGetResult(PushArgs.Device) |> getDeviceIdFromResult
if subCommand.Contains(Text) then
(subCommand.GetResult(Text), deviceId)
|> PushCommands.PushTextCommand
|> PushCommands.pushText
else if subCommand.Contains(Note) then
let (title, body) = subCommand.GetResult(Note)
(title |> toOption, body |> toOption, deviceId)
|> PushCommands.PushNoteCommand
|> PushCommands.pushNote
else
subCommand.Parser.PrintUsage()
| [ Link subCommand ] ->
let deviceId = subCommand.TryGetResult(LinkArgs.Device) |> getDeviceIdFromResult
if subCommand.Contains(Url) then
(subCommand.GetResult(Url),
subCommand.TryGetResult(Title) |> Option.bind id,
subCommand.TryGetResult(Body) |> Option.bind id,
deviceId)
|> PushCommands.PushLinkCommand
|> PushCommands.pushLink
else
subCommand.Parser.PrintUsage()
| [ Pushes arg ] ->
match arg with
| Some v -> v |> PushCommands.ListPushesCommand |> PushCommands.list
| None -> 0 |> PushCommands.ListPushesCommand |> PushCommands.list
| [ Delete subCommand ] ->
match subCommand.GetAllResults() with
| [ DeleteArgs.Push p ] -> p |> PushCommands.DeletePushCommand |> PushCommands.delete
| [ DeleteArgs.Chat c ] -> c |> ChatCommands.DeleteChatCommand |> ChatCommands.delete
| [ DeleteArgs.Device d ] -> d |> DeviceCommands.DeleteDeviceCommand |> DeviceCommands.delete
| [ Subscription sub ] ->
sub
|> SubscriptionCommands.DeleteSubscriptionCommand
|> SubscriptionCommands.delete
| [ DeleteArgs.Sms s ] -> s |> MessageCommands.DeleteMessageCommand |> MessageCommands.delete
| [ DeleteArgs.Key ] -> SystemCommands.deleteKey ()
| _ -> subCommand.Parser.PrintUsage()
| [ Clip arg ] -> arg |> PushCommands.PushClipCommand |> PushCommands.pushClip
| [ Sms(device, number, body) ] ->
(device |> getDeviceFromIndexOrDeviceId, number, body)
|> MessageCommands.SendMessageCommand
|> MessageCommands.create
| [ Device arg ] ->
arg
|> getDeviceFromIndexOrDeviceId
|> DeviceCommands.GetDeviceInfoCommand
|> DeviceCommands.getDeviceInfo
| [ Devices ] -> DeviceCommands.list ()
| [ Chat subCommand ] ->
match subCommand.GetAllResults() with
| [ Create c ] -> c |> ChatCommands.CreateChatCommand |> ChatCommands.create
| [ Update(id, status) ] ->
match status |> valueToBool with
| Some b -> (id, b) |> ChatCommands.UpdateChatCommand |> ChatCommands.update
| None -> Errors_ParameterInvalid.ResourceString
| _ -> subCommand.Parser.PrintUsage()
| [ Chats ] -> ChatCommands.list ()
| [ Subscriptions ] -> SubscriptionCommands.list ()
| [ ChannelInfo arg ] ->
arg
|> SubscriptionCommands.GetChannelInfoCommand
|> SubscriptionCommands.channelInfo
| [ Version ] -> SystemCommands.getVersion ()
| [ Help ] -> parser.PrintUsage()
| _ -> parser.PrintUsage()
else
match parsingResult.GetAllResults() with
| [ Key arg ] ->
match arg with
| Some a -> a |> SystemCommands.SetKeyCommand |> SystemCommands.setKey
| None -> SystemCommands.getKey ()
| _ -> Info_SetupKey.ResourceString
[<EntryPoint>]
let main ([<ParamArray>] argv: string[]) : int =
try
(ArgumentParser.Create<CliArguments>(), argv) ||> runCommands |> printfn "%s"
with ex ->
eprintfn $"{ex.Message}"
0