Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Majic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pleroma
Elixir libraries
Majic
Commits
60fde9c3
Commit
60fde9c3
authored
4 years ago
by
href
Browse files
Options
Downloads
Patches
Plain Diff
Unified API: `GenMagic.perform/2,3`
parent
9a70442a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+9
-9
9 additions, 9 deletions
README.md
lib/gen_magic.ex
+33
-0
33 additions, 0 deletions
lib/gen_magic.ex
lib/gen_magic/server.ex
+4
-2
4 additions, 2 deletions
lib/gen_magic/server.ex
with
46 additions
and
11 deletions
README.md
+
9
−
9
View file @
60fde9c3
...
...
@@ -27,7 +27,7 @@ Depending on the use case, you may utilise a single (one-off) GenMagic process w
To use GenMagic directly, you can use
`GenMagic.Helpers.perform_once/1`
:
```
elixir
iex
(
1
)
>
GenMagic
.
Helpers
.
perform_once
"."
iex
(
1
)
>
GenMagic
.
perform
(
"."
,
once:
true
)
{
:ok
,
%
GenMagic
.
Result
{
content:
"directory"
,
...
...
@@ -40,7 +40,7 @@ To use the GenMagic server as a daemon, you can start it first, keep a reference
```
elixir
{
:ok
,
pid
}
=
GenMagic
.
Server
.
start_link
([])
{
:ok
,
result
}
=
GenMagic
.
Server
.
perform
(
p
id
,
path
)
{
:ok
,
result
}
=
GenMagic
.
perform
(
p
ath
,
server:
pid
)
```
See
`GenMagic.Server.start_link/1`
and
`t:GenMagic.Server.option/0`
for more information on startup parameters.
...
...
@@ -67,7 +67,7 @@ See `t:GenMagic.Server.option/0` for details.
For ad-hoc requests, you can use the helper method
`GenMagic.Helpers.perform_once/2`
:
```
elixir
iex
(
1
)
>
GenMagic
.
Helpers
.
perform
_once
(
Path
.
join
(
File
.
cwd!
(),
"Makefile"
))
iex
(
1
)
>
GenMagic
.
perform
(
Path
.
join
(
File
.
cwd!
(),
"Makefile"
)
,
once:
true
)
{
:ok
,
%
GenMagic
.
Result
{
content:
"makefile script, ASCII text"
,
...
...
@@ -90,8 +90,8 @@ iex(1)> {:ok, pid} = Supervisor.start_link([{GenMagic.Server, name: :gen_magic}]
Now we can ask it to inspect a file:
```
elixir
iex
(
2
)
>
GenMagic
.
Server
.
perform
(
:gen_magic
,
Path
.
expand
(
"~/.bash_history"
))
{
:ok
,
[
mime_type:
"text/plain"
,
encoding:
"us-ascii"
,
content:
"ASCII text"
]
}
iex
(
2
)
>
GenMagic
.
perform
(
Path
.
expand
(
"~/.bash_history"
)
,
server:
:gen_magic
)
{
:ok
,
%
GenMagic
.
Result
{
mime_type:
"text/plain"
,
encoding:
"us-ascii"
,
content:
"ASCII text"
}
}
```
Note that in this case we have opted to use a named process.
...
...
@@ -114,11 +114,11 @@ You can add a pool in your application supervisor by adding it as a child:
Supervisor.start_link(children, opts)
```
And then you can use it with
`GenMagic.
Pool.
perform/2`
:
And then you can use it with
`GenMagic.perform/2`
with
`pool: YourApp.GenMagicPool`
option
:
```
iex(1)> GenMagic.
Pool.
perform(
YourApp.GenMagicPool,
Path.expand("~/.bash_history"))
{:ok,
[
mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"
]
}
iex(1)> GenMagic.perform(Path.expand("~/.bash_history")
, pool: YourApp.GenMagicPool
)
{:ok,
%GenMagic.Result{
mime_type: "text/plain", encoding: "us-ascii", content: "ASCII text"
}
}
```
### Check Uploaded Files
...
...
@@ -127,7 +127,7 @@ If you use Phoenix, you can inspect the file from your controller:
```
elixir
def
upload
(
conn
,
%{
"upload"
=>
%{
path:
path
}})
do
,
{
:ok
,
result
}
=
GenMagic
.
Helpers
.
perform_once
(
:gen_magic
,
path
)
{
:ok
,
result
}
=
GenMagic
.
perform
(
path
,
server:
:gen_magic
)
text
(
conn
,
"Received your file containing
#{
result
.
content
}
"
)
end
```
...
...
This diff is collapsed.
Click to expand it.
lib/gen_magic.ex
+
33
−
0
View file @
60fde9c3
...
...
@@ -4,4 +4,37 @@ defmodule GenMagic do
See `GenMagic.Server` or the README for usage.
"""
@doc
"""
Perform on `path`.
An option of `server: ServerName`, `pool: PoolName` or `once: true` must be passed.
"""
@type
option
::
name
when
name:
{
:pool
,
atom
()}
|
{
:server
,
GenMagic
.
Server
.
t
()}
|
{
:once
,
true
}
@spec
perform
(
GenMagic
.
Server
.
target
(),
[
option
()])
::
GenMagic
.
Server
.
result
()
def
perform
(
path
,
opts
,
timeout
\\
5000
)
do
mod
=
cond
do
Keyword
.
has_key?
(
opts
,
:pool
)
->
{
GenMagic
.
Pool
,
Keyword
.
get
(
opts
,
:pool
)}
Keyword
.
has_key?
(
opts
,
:server
)
->
{
GenMagic
.
Server
,
Keyword
.
get
(
opts
,
:server
)}
Keyword
.
has_key?
(
opts
,
:once
)
->
{
GenMagic
.
Helpers
,
nil
}
true
->
nil
end
if
mod
do
do_perform
(
mod
,
path
,
timeout
)
else
{
:error
,
:no_method
}
end
end
defp
do_perform
({
GenMagic
.
Helpers
,
_
},
path
,
timeout
)
do
GenMagic
.
Helpers
.
perform_once
(
path
,
timeout
)
end
defp
do_perform
({
mod
,
name
},
path
,
timeout
)
do
mod
.
perform
(
name
,
path
,
tiemout
)
end
end
This diff is collapsed.
Click to expand it.
lib/gen_magic/server.ex
+
4
−
2
View file @
60fde9c3
...
...
@@ -55,6 +55,9 @@ defmodule GenMagic.Server do
|
{
:recycle_threshold
,
non_neg_integer
()
|
:infinity
}
|
{
:database_patterns
,
nonempty_list
(
:default
|
Path
.
t
())}
@type
target
::
Path
.
t
()
|
{
:bytes
,
binary
()}
@type
result
::
{
:ok
,
Result
.
t
()}
|
{
:error
,
term
()
|
String
.
t
()}
@typedoc
"""
Current state of the Server:
...
...
@@ -81,8 +84,7 @@ defmodule GenMagic.Server do
@spec
child_spec
([
option
()])
::
Supervisor
.
child_spec
()
@spec
start_link
([
option
()])
::
:gen_statem
.
start_ret
()
@spec
perform
(
t
(),
Path
.
t
()
|
{
:bytes
,
binary
()},
timeout
())
::
{
:ok
,
Result
.
t
()}
|
{
:error
,
term
()
|
String
.
t
()}
@spec
perform
(
t
(),
target
(),
timeout
())
::
result
()
@spec
status
(
t
(),
timeout
())
::
{
:ok
,
Status
.
t
()}
|
{
:error
,
term
()}
@spec
stop
(
t
(),
term
(),
timeout
())
::
:ok
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment