Last active
March 3, 2018 20:09
-
-
Save realvictorprm/2b4083ecfc3f77fb3e23d96d206f18e4 to your computer and use it in GitHub Desktop.
Provides the class ReferenceHelper with which one can simply open the documentation to a bash command through calling `helper.[commandyousearch]`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Net | |
open System.Text.RegularExpressions | |
open System.Collections.Generic | |
// Learn more about F# at http://fsharp.org. See the 'F# Tutorial' "project" | |
// for more guidance on F# programming. | |
module Option = | |
let inline defaultMap value f option = match option with | None -> value | Some value -> f value | |
module Result = | |
let inline isOk v = | |
match v with | |
| Ok _ -> true | |
| _ -> false | |
let inline getOk value = | |
match value with | |
| Ok value -> value | |
| _ -> invalidArg "value" "isn't case Result.Ok" | |
open System.IO | |
type ParserState<'a> = | |
| Valid of 'a | |
| Invalid | |
| End | |
[<RequireQualifiedAccess>] | |
type UrlKind = | |
| Valid of string | |
| Invalid of string | |
let inline read parser = | |
let rec readIn results = | |
let res = Console.ReadLine() |> parser | |
match res with | |
| Valid value -> results @ [value] |> readIn | |
| Invalid -> results |> readIn | |
| End -> results | |
readIn [] | |
let commands () = | |
let word = Regex("[a-zA-Z]+") | |
let parser str = | |
if word.IsMatch str then Valid str | |
elif String.IsNullOrWhiteSpace str then End | |
else Invalid | |
read parser | |
|> List.toArray | |
let inline (+++) b a = | |
a |> Array.append b | |
let getCommandReferencePages (cmds:string array option) = | |
let commands = Option.defaultWith commands cmds | |
//[| | |
// "echo" | |
// "ln" | |
// "mv" | |
// "cat" | |
// "cal" | |
// "df" | |
// "w" | |
// "id" | |
// "last" | |
// "uptime" | |
// "date" | |
// "top" | |
// "uname" | |
// "pwd" | |
// "ls" | |
// "cd" | |
// "dir" | |
// "more" | |
// "less" | |
// "mkdir" | |
// "chmod" | |
// "mount" | |
// "du" | |
//|] | |
let referencePages = | |
let verifyURL checkContent url = | |
printfn "%s" url | |
let webrequest = System.Net.WebRequest.Create url | |
webrequest.Timeout <- 2000 | |
webrequest.Method <- "HEAD" | |
let success = | |
try | |
use response = (webrequest.GetResponse() :?> System.Net.HttpWebResponse) | |
let res = response.StatusCode = HttpStatusCode.OK | |
if res then | |
let client = new WebClient() | |
use s = client.OpenRead(url) | |
use reader = new StreamReader(s) | |
let content = reader.ReadToEnd() | |
if checkContent content then true | |
else false | |
else | |
false | |
with _ -> false | |
if success then Some url | |
else None | |
let urls = | |
[| for i in 1..8 -> sprintf "https://linux.die.net/man/%d/" i, "", fun _ -> true |] | |
+++ | |
[| "https://ss64.com/bash/", ".html", fun _ -> true | |
"http://man.openbsd.org/", "#NAME", fun (content:string) -> content.Contains "No results found." |> not || content |> String.IsNullOrEmpty |] | |
+++ | |
[| let namespaces = [| "System."; "System.Net."; "System.Reflection."; ""|] | |
for str in namespaces -> | |
sprintf "https://docs.microsoft.com/de-de/dotnet/api/%s" str, "?view=netcore-2.0", fun _ -> true |] | |
let testOnUrls cmd = | |
cmd, | |
urls | |
|> Array.Parallel.choose (fun (url, ending, checker) -> url + cmd + ending |> verifyURL checker) | |
commands | |
|> Array.Parallel.map testOnUrls | |
|> Array.filter(fun (_, r) -> r |> Array.length > 0) | |
referencePages | |
let tryOpenPage (reference:'a when 'a :> IDictionary<string,string array>) cmd = | |
reference.[cmd] |> Array.head |> System.Diagnostics.Process.Start |> ignore | |
type ReferenceHelper(cmds:string array) = | |
let getCommandReferencePagesAdjusted cmds = | |
let res = getCommandReferencePages (Some cmds) | |
match res with | |
| [||] -> Map.empty | |
| r -> Map.ofArray r | |
let reference = getCommandReferencePagesAdjusted cmds |> Dictionary | |
member __.AddCmd cmd = | |
let res = getCommandReferencePagesAdjusted [| cmd |] |> Map.toArray | |
if res.Length > 0 then | |
for key, value in res do reference.[key] <- value | |
true | |
else false | |
member self.Item(``command or class name``) = | |
if reference.ContainsKey ``command or class name`` then | |
tryOpenPage reference ``command or class name`` | |
else | |
if self.AddCmd ``command or class name`` then | |
self.[``command or class name``] | |
else ArgumentException("command or class name", "This isn't a valid unix command or .NET class!") |> raise | |
let helper = ReferenceHelper([| "mv" |]) |
Author
realvictorprm
commented
Mar 3, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment