-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from rtbo/examples
Examples
- Loading branch information
Showing
7 changed files
with
259 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env dub | ||
/+ dub.sdl: | ||
name "box_upload" | ||
description "an example for squiz-box: create archive and upload to the web" | ||
dependency "squiz-box" path=".." | ||
dependency "requests" version="~>2.1.1" | ||
+/ | ||
|
||
module examples.box_upload; | ||
|
||
import squiz_box; | ||
import requests; | ||
|
||
import std.algorithm; | ||
import std.exception; | ||
import std.getopt; | ||
import std.format; | ||
import std.file; | ||
import std.path; | ||
import std.range; | ||
import std.stdio; | ||
|
||
void main(string[] args) | ||
{ | ||
string postTo = "https://httpbin.org/post"; | ||
string fmt = ".tar.xz"; | ||
string src = ".."; | ||
string prefix; | ||
|
||
auto opts = getopt(args, | ||
"post-to", &postTo, | ||
"format", &fmt, | ||
"src", &src, | ||
"prefix", &prefix, | ||
); | ||
|
||
if (opts.helpWanted) | ||
{ | ||
defaultGetoptPrinter("Squiz-box example, create archive, list and upload", opts.options); | ||
} | ||
|
||
// Algorithm matched at runtime (using extension) | ||
auto algo = boxAlgo(fmt); | ||
|
||
size_t numFiles; | ||
size_t dataSz; | ||
|
||
const exclusion = [".git", ".dub", ".vscode", "libsquiz-box.a", "build"]; | ||
|
||
auto archiveChunks = dirEntries(src, SpanMode.breadth, false) | ||
.filter!(e => !e.isDir) | ||
.filter!(e => !exclusion.any!(ex => e.name.canFind(ex))) | ||
.tee!(e => stdout.writeln(e.name)) | ||
.tee!(e => numFiles += 1) | ||
.map!(e => fileEntry(e.name, src, prefix)) | ||
.box(algo) | ||
.tee!(c => stderr.writefln!"uploaded %s bytes"(c.length)) | ||
.tee!(c => dataSz += c.length); | ||
|
||
auto rq = Request(); | ||
auto resp = rq.post(postTo, archiveChunks, algo.mimetype); | ||
enforce(resp.code < 300, format!"%s responded %s"(postTo, resp.code)); | ||
|
||
writefln!"POST %s - status %s"(postTo, resp.code); | ||
writefln!"Archived %s files. Uploaded %s bytes"(numFiles, dataSz); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env dub | ||
/+ dub.sdl: | ||
name "download_unbox" | ||
description "an example for squiz-box: download, list and extract archive" | ||
dependency "squiz-box" path=".." | ||
dependency "requests" version="~>2.1.1" | ||
+/ | ||
|
||
module examples.download_unbox; | ||
|
||
import squiz_box; | ||
import requests; | ||
|
||
import std.algorithm; | ||
import std.getopt; | ||
import std.file; | ||
import std.path; | ||
import std.range; | ||
import std.stdio; | ||
|
||
void main(string[] args) | ||
{ | ||
string url = "https://github.com/dlang/dmd/archive/master.tar.gz"; | ||
string dest; | ||
|
||
auto opts = getopt(args, | ||
"url", "URL of archive to download", &url, | ||
"dest", "The destination directory. Extracted files will disappear if not specified.", &dest, | ||
); | ||
|
||
if (opts.helpWanted) | ||
{ | ||
defaultGetoptPrinter("Squiz-box, download, list and extract archive", opts.options); | ||
} | ||
|
||
const outDir = dest.length ? dest : buildPath(tempDir, "squiz-box-example"); | ||
|
||
if (!exists(outDir)) | ||
mkdirRecurse(outDir); | ||
|
||
scope(success) | ||
{ | ||
if (!dest) | ||
rmdirRecurse(outDir); | ||
} | ||
|
||
// Algorithm matched at runtime with url (using extension) | ||
auto algo = boxAlgo(url); | ||
|
||
writefln!"GET %s"(url); | ||
|
||
size_t dataSz; | ||
size_t numFiles; | ||
|
||
auto rq = Request(); | ||
rq.useStreaming = true; | ||
rq.get(url).receiveAsRange() | ||
.map!(c => cast(const(ubyte)[])c) | ||
.tee!(c => stderr.writefln!"received %s bytes"(c.length)) | ||
.tee!(c => dataSz += c.length) | ||
.unbox(algo) | ||
.tee!(e => stdout.writeln(buildPath(dest, e.path))) | ||
.tee!(e => numFiles += 1) | ||
.each!(e => e.extractTo(outDir)); | ||
|
||
writefln!"Downloaded %s bytes. Extracted %s files."(dataSz, numFiles); | ||
} |
This file contains 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
This file contains 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
This file contains 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