Skip to content

Commit

Permalink
Added support for Triggers (json)
Browse files Browse the repository at this point in the history
Multiple instances of the same keyword are now supported (but usually useless) - this allows for multiple triggers for example.
Command / Custom Command = Yes was improved: If you specify a command, it will add custom command for you if not already specified.
  • Loading branch information
Blizzke committed Aug 7, 2018
1 parent 14fb1e4 commit 6cbf2dd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ As an example, the generated profile could look like this:
"Jumpstations",
"tag with spaces"
],
"Custom Command": "Yes",
"Command": "ssh bastion"
}
```
Expand Down Expand Up @@ -118,16 +117,27 @@ With regular profiles this is impossible. As soon as you duplicate it it is no l

### CustomCommand

"`Yes`" to configure a custom command to execute via the `Command` keyword. If no `CustomCommand` option is found, an `ssh <first-host-pattern>` custom command will be automatically added for you.
"`Yes`" to configure a custom command to execute via the `Command` keyword. You can just specify a `Command` though, `Custom Command` will automatically be added.

### Command

See above, the actual command to execute if you want to specify your own. (`CustomCommand` is not automatically added at this point, so make sure to add it yourself)
Related to above, but the actual command to execute if you want to specify your own. `CustomCommand` is automatically added if you specify a command, so adding that is optional.
If no `CustomCommand` option is found after the processing of the Host, an `ssh <first-host-pattern>` custom command will be automatically added for you.

### Tags

A space-separated list of tags for the profile. Spaces within tags are supported by double-quoting them ("*tag with spaces*").

### Trigger

Can contain the JSON to define a trigger. This can be specified multiple times (one per trigger).
The easiest way to obtain the syntax is to define them manually in iTerm2 and then use "Copy Profile as JSON" under "Other Actions..." to extract it.
Just specify it as a one liner to use (example is to open the password manager):

```
# Trigger {"partial": true, "parameter": "client\/hostname\/username", "regex": "assword:", "action" : "PasswordTrigger"}
```

## Command Line Arguments

An overview of the command line arguments. The default behavior is to use `~/.ssh/config` and write the resulting json will to the screen.
Expand Down
6 changes: 3 additions & 3 deletions src/Console/GenerateProfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function getCallbacks(InputInterface $input): array
$callbacks = [
// If no custom command was specified add one. We can safely assume all hosts are SSH hosts I think...
// Note that if the first pattern is a wildcard pattern, this won't work
'CustomCommand' => function($profile, $host, $array) {
'CustomCommand' => function($array, $host) {
if (!array_key_exists('Command', $array)) {
// Use the first pattern to connect instead of hostname (on the chance that hostname is not a part of
// the config section and will not be assigned the correct options by ssh)
Expand All @@ -147,7 +147,7 @@ private function getCallbacks(InputInterface $input): array
];

if ($input->getOption('bind')) {
$callbacks['BoundHosts'] = function($profile, $host, $array) {
$callbacks['BoundHosts'] = function($array, $host) {
$array['Bound Hosts'] = $host->get('Host')->value;
if ($hostname = $host->get('Hostname')) {
$array['Bound Hosts'][] = $hostname->valueString;
Expand All @@ -159,7 +159,7 @@ private function getCallbacks(InputInterface $input): array

if ($autoTags = $input->getOption('autotag')) {
$autoTags = new Line('AutoTag ' . $autoTags);
$callbacks['AutoTag'] = function($profile, $host, $array) use ($autoTags) {
$callbacks['AutoTag'] = function($array, $host) use ($autoTags) {
$tags = array_key_exists('Tags', $array) ? $array['Tags'] : [];

// Assemble substitutes
Expand Down
14 changes: 8 additions & 6 deletions src/DynamicProfiles/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,29 @@ public function __construct(string $pattern, Host $host, string $guid = null)
public function asArray($options, $extraCallbacks = [])
{
$profile = [
'Name' => $this->name(),
'Name' => $name = $this->name(),
'Guid' => $this->guid(),
];

foreach ($options as $keyword => $action) {
$line = $this->host->get($keyword);
if ($line) {
$lines = $this->host->get($keyword, [], true);
foreach ($lines as $line) {
if ($action === false) {
continue;
}
if (\is_string($action)) {
$profile[$action] = $line->valueString;
} elseif (\is_callable($action)) {
$profile = $action($profile, $line, $this->host, $this);
}
elseif (\is_callable($action)) {
$profile = $action($this, $this->host, $profile);
if (!\is_array($profile)) {
throw new \RuntimeException("Processing keyword '$keyword' for host '$name' did not result in an array");
}
}
}

foreach ($extraCallbacks as $callback) {
$profile = $callback($this, $this->host, $profile);
$profile = $callback($profile, $this->host, $this);
}

return $profile;
Expand Down
24 changes: 21 additions & 3 deletions src/DynamicProfiles/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,30 @@
'Badge' => 'Badge Text',
'ParentProfile' => 'Dynamic Profile Parent Name',
'CustomCommand' => 'Custom Command',
'Command' => 'Command',
'Tags' => function($profile, $host, $array) {
$tags = $host->get('tags');
'Command' => function($array, $line) {
$command = $line->firstValue;
if ($command) {
if (!array_key_exists('Custom Command', $array)) {
$array['Custom Command'] = 'Yes';
}
$array['Command'] = $command;
}
return $array;
},
'Tags' => function($array, $tags) {
if ($tags && \count($tags->value)) {
$array['Tags'] = $tags->value;
}
return $array;
},
'Trigger' => function($array, $line) {
$trigger = json_decode($line->valueString, true);
if ($trigger) {
if (!array_key_exists('Triggers', $array)) {
$array['Triggers'] = [];
}
$array['Triggers'][] = $trigger;
}
return $array;
}
];
10 changes: 8 additions & 2 deletions src/SSH/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,23 @@ public function __set($name, $value)
}
}

public function get($name, $default = null)
public function get($name, $default = null, $supportMultiple = false)
{
if (\in_array($name, ['patterns', 'commentValues', 'file'])) {
return $this->$name;
}

$lines = [];
foreach ($this->lines as $line) {
if ($line->is($name)) {
return $line;
$lines[] = $line;
}
}

if (\count($lines)) {
return $supportMultiple ? $lines : reset($lines);
}

return $default;
}

Expand Down

0 comments on commit 6cbf2dd

Please sign in to comment.