Akd v0.3.0 Akd.Operation View Source
This module represents an Operation
struct which contains metadata about
a command/operation that can be run on a destination.
Please refer to Nomenclature
for more information about the terms used.
The meta data involves:
cmd
- Commands that run when anAkd.Operation.t
struct is run.cmd_envs
- ENV variables that the command is run with. Represented by a list of two-element (strings) tuples. Example: [{"SOME_ENV", "1"}, {"OTHER_ENV", "2"}]destination
-Akd.Destination.t
where an operation's commands are executed.
This struct is mainly used by native hooks in Akd
, but it can be leveraged
to write custom hooks.
Link to this section Summary
Types
Type representing a Command to be run
Type representind a command specific environment
Generic type for an Operation struct
Functions
Takes an Operation
and returns a string of commands with cmd_envs
preprended
to the cmd
script
Runs a given Operation.t
command on it's destination.
If the destination is local, it just runs it on the local machine.
If the destination is remote, it runs it through SSH
Link to this section Types
cmd()
View Source
cmd() :: String.t() | :exit
cmd() :: String.t() | :exit
Type representing a Command to be run
cmd_envs() View Source
Type representind a command specific environment
t()
View Source
t() :: %Akd.Operation{
cmd: cmd(),
cmd_envs: [cmd_envs()],
destination: Akd.Destination.t()
}
t() :: %Akd.Operation{ cmd: cmd(), cmd_envs: [cmd_envs()], destination: Akd.Destination.t() }
Generic type for an Operation struct
Link to this section Functions
environmentalize_cmd(operation) View Source
Takes an Operation
and returns a string of commands with cmd_envs
preprended
to the cmd
script.
Examples:
When a non-empty list of environments are given:
iex> envs = [{"NAME", "dragonborn"}, {"NOK", "dovahkiin"}]
iex> dest = %Akd.Destination{}
iex> op = %Akd.Operation{cmd_envs: envs, cmd: "thuum", destination: dest}
iex> Akd.Operation.environmentalize_cmd(op)
"NAME=dragonborn NOK=dovahkiin thuum"
When an empty list of environments are given:
iex> dest = %Akd.Destination{}
iex> op = %Akd.Operation{cmd_envs: [], cmd: "thuum", destination: dest}
iex> Akd.Operation.environmentalize_cmd(op)
" thuum"
run(operation) View Source
Runs a given Operation.t
command on it's destination.
If the destination is local, it just runs it on the local machine.
If the destination is remote, it runs it through SSH.
NOTE: It will automatically create the folder when run locally
Examples:
When the destination is local
iex> envs = [{"AKDNAME", "dragonborn"}]
iex> dest = %Akd.Destination{}
iex> cmd = "echo $AKDNAME; exit 0"
iex> op = %Akd.Operation{cmd_envs: envs, cmd: cmd, destination: dest}
iex> Akd.Operation.run(op)
{:ok, %IO.Stream{device: :standard_io, line_or_bytes: :line, raw: false}}
iex> dest = %Akd.Destination{}
iex> cmd = "exit 1"
iex> op = %Akd.Operation{cmd: cmd, destination: dest}
iex> Akd.Operation.run(op)
{:error, %IO.Stream{device: :standard_io, line_or_bytes: :line, raw: false}}
iex> dest = %Akd.Destination{}
iex> cmd = "exit 2"
iex> op = %Akd.Operation{cmd: cmd, destination: dest}
iex> Akd.Operation.run(op)
{:error, %IO.Stream{device: :standard_io, line_or_bytes: :line, raw: false}}
When the destination is remote
iex> envs = [{"AKDNAME", "dragonborn"}]
iex> dest = %Akd.Destination{user: "dovahkiin", host: "skyrim"}
iex> cmd = "echo $AKDNAME"
iex> op = %Akd.Operation{cmd_envs: envs, cmd: cmd, destination: dest}
iex> Akd.Operation.run(op)
{:error, %IO.Stream{device: :standard_io, line_or_bytes: :line, raw: false}}