mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
Add Env to cli example
This commit is contained in:
parent
21f3d4b8bc
commit
7d1376b273
5 changed files with 137 additions and 21 deletions
75
examples/interactive/cli-platform/Cargo.lock
generated
75
examples/interactive/cli-platform/Cargo.lock
generated
|
@ -430,6 +430,75 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "matches"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "miniz_oxide"
|
||||||
|
version = "0.5.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc"
|
||||||
|
dependencies = [
|
||||||
|
"adler",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "percent-encoding"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.40"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ring"
|
||||||
|
version = "0.16.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
"once_cell",
|
||||||
|
"spin",
|
||||||
|
"untrusted",
|
||||||
|
"web-sys",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "roc_std"
|
name = "roc_std"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
|
@ -644,9 +713,15 @@ checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
|
<<<<<<< HEAD
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
|
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
|
||||||
|
=======
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7"
|
||||||
|
>>>>>>> fdebd4191 (Add Env to cli example)
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-normalization"
|
name = "unicode-normalization"
|
||||||
|
|
|
@ -18,6 +18,7 @@ path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
roc_std = { path = "../../../crates/roc_std" }
|
roc_std = { path = "../../../crates/roc_std" }
|
||||||
|
ureq = "2.5.0"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
reqwest = { version="0.11.11", default-features=false, features=["blocking", "rustls-tls"] }
|
reqwest = { version="0.11.11", default-features=false, features=["blocking", "rustls-tls"] }
|
||||||
|
|
||||||
|
|
|
@ -12,12 +12,31 @@ interface Http
|
||||||
emptyBody,
|
emptyBody,
|
||||||
bytesBody,
|
bytesBody,
|
||||||
stringBody,
|
stringBody,
|
||||||
|
# jsonBody,
|
||||||
|
multiPartBody,
|
||||||
|
stringPart,
|
||||||
|
bytesPart,
|
||||||
handleStringResponse,
|
handleStringResponse,
|
||||||
defaultRequest,
|
defaultRequest,
|
||||||
errorToString,
|
errorToString,
|
||||||
send,
|
send,
|
||||||
]
|
]
|
||||||
imports [Effect, InternalTask, Task.{ Task }, InternalHttp]
|
imports [
|
||||||
|
Effect,
|
||||||
|
InternalTask,
|
||||||
|
# Json,
|
||||||
|
Task.{ Task },
|
||||||
|
# Encode.{ Encoding },
|
||||||
|
HttpTypes.{ Request, Method, Header, Timeout, ProgressTracking, Part, Body, Response, Metadata, Error },
|
||||||
|
Url.{ Url },
|
||||||
|
]
|
||||||
|
|
||||||
|
HttpErr a : [
|
||||||
|
HttpErr [
|
||||||
|
NotFound Url,
|
||||||
|
Timeout Url,
|
||||||
|
]
|
||||||
|
]a
|
||||||
|
|
||||||
Request : InternalHttp.Request
|
Request : InternalHttp.Request
|
||||||
Method : InternalHttp.Method
|
Method : InternalHttp.Method
|
||||||
|
@ -59,26 +78,29 @@ stringBody = \mimeType, str ->
|
||||||
# jsonBody : a -> Body | a has Encoding
|
# jsonBody : a -> Body | a has Encoding
|
||||||
# jsonBody = \val ->
|
# jsonBody = \val ->
|
||||||
# Body (MimeType "application/json") (Encode.toBytes val Json.format)
|
# Body (MimeType "application/json") (Encode.toBytes val Json.format)
|
||||||
#
|
multiPartBody : List Part -> Body
|
||||||
# multiPartBody : List Part -> Body
|
multiPartBody = \parts ->
|
||||||
# multiPartBody = \parts ->
|
boundary = "7MA4YWxkTrZu0gW" # TODO: what's this exactly? a hash of all the part bodies?
|
||||||
# boundary = "7MA4YWxkTrZu0gW" # TODO: what's this exactly? a hash of all the part bodies?
|
beforeName = Str.toUtf8 "-- \(boundary)\r\nContent-Disposition: form-data; name=\""
|
||||||
# beforeName = Str.toUtf8 "-- \(boundary)\r\nContent-Disposition: form-data; name=\""
|
afterName = Str.toUtf8 "\"\r\n"
|
||||||
# afterName = Str.toUtf8 "\"\r\n"
|
appendPart = \buffer, Part name partBytes ->
|
||||||
# appendPart = \buffer, Part name partBytes ->
|
buffer
|
||||||
# buffer
|
|> List.concat beforeName
|
||||||
# |> List.concat beforeName
|
|> List.concat (Str.toUtf8 name)
|
||||||
# |> List.concat (Str.toUtf8 name)
|
|> List.concat afterName
|
||||||
# |> List.concat afterName
|
|> List.concat partBytes
|
||||||
# |> List.concat partBytes
|
bodyBytes = List.walk parts [] appendPart
|
||||||
# bodyBytes = List.walk parts [] appendPart
|
|
||||||
# Body (MimeType "multipart/form-data;boundary=\"\(boundary)\"") bodyBytes
|
Body (MimeType "multipart/form-data;boundary=\"\(boundary)\"") bodyBytes
|
||||||
# bytesPart : Str, List U8 -> Part
|
|
||||||
# bytesPart =
|
bytesPart : Str, List U8 -> Part
|
||||||
# Part
|
bytesPart =
|
||||||
# stringPart : Str, Str -> Part
|
Part
|
||||||
# stringPart = \name, str ->
|
|
||||||
# Part name (Str.toUtf8 str)
|
stringPart : Str, Str -> Part
|
||||||
|
stringPart = \name, str ->
|
||||||
|
Part name (Str.toUtf8 str)
|
||||||
|
|
||||||
handleStringResponse : Response -> Result Str Error
|
handleStringResponse : Response -> Result Str Error
|
||||||
handleStringResponse = \response ->
|
handleStringResponse = \response ->
|
||||||
when response is
|
when response is
|
||||||
|
|
|
@ -39,6 +39,10 @@ succeed = \ok -> InternalTask.succeed ok
|
||||||
fail : err -> Task * err *
|
fail : err -> Task * err *
|
||||||
fail = \err -> InternalTask.fail err
|
fail = \err -> InternalTask.fail err
|
||||||
|
|
||||||
|
withDefault : Task ok * fx, ok -> Task ok * fx
|
||||||
|
withDefault = \task, default ->
|
||||||
|
onFail task \_ -> Task.succeed default
|
||||||
|
|
||||||
attempt : Task a b fx, (Result a b -> Task c d fx) -> Task c d fx
|
attempt : Task a b fx, (Result a b -> Task c d fx) -> Task c d fx
|
||||||
attempt = \task, transform ->
|
attempt = \task, transform ->
|
||||||
effect = Effect.after
|
effect = Effect.after
|
||||||
|
|
|
@ -377,3 +377,17 @@ pub extern "C" fn roc_fx_sendRequest(roc_request: &glue::Request) -> glue::Respo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn roc_fx_httpGetUtf8(url: &RocStr) -> RocStr {
|
||||||
|
let body: String = ureq::get(url).call().unwrap().into_string().unwrap();
|
||||||
|
|
||||||
|
RocStr::from(body.as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn roc_fx_envVarUtf8(key: &RocStr) -> RocStr {
|
||||||
|
let val: String = dbg!(std::env::var(dbg!(key.as_str()))).unwrap();
|
||||||
|
|
||||||
|
RocStr::from(val.as_str())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue