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 an Akd.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

t()

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

Type representing a Command to be run

Link to this type

cmd_envs() View Source
cmd_envs() :: {String.t(), String.t()}

Type representind a command specific environment

Link to this type

t() View Source
t() :: %Akd.Operation{
  cmd: cmd(),
  cmd_envs: [cmd_envs()],
  destination: Akd.Destination.t()
}

Generic type for an Operation struct

Link to this section Functions

Link to this function

environmentalize_cmd(operation) View Source
environmentalize_cmd(t()) :: String.t()

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"
Link to this function

run(operation) View Source
run(t()) :: {:ok, term()} | {:error, term()}

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}}