Skip to content

Commit

Permalink
add support of multiple values in host label
Browse files Browse the repository at this point in the history
- example of usage:
  host('prod.example.org')
      ->set('hostname', 'example.cloud.google.com');
      ->setLabel(
          'state' => 'prod'
          'role' => ['build','web','redis']
      ]);

  host('prod-db.example.org')
        ->set('hostname', 'example.cloud.google.com');
        ->setLabel(
            'state' => 'prod'
            'role' => ['db']
        ]);
  host('qa.example.org')
      ->set('hostname', 'example.cloud.google.com');
      ->setLabel(
          'state' => 'qa'
          'role' => ['build','web','redis','db']
      ]);
  host('dev.example.org')
        ->set('hostname', 'example.cloud.google.com');
        ->setLabel(
            'state' => 'qa'
            'role' => ['build','web','redis','db']
        ]);
  ...

  desc('Clear pagespeed');
  task('pagespeed:clear', function () {
      run("rm -rf {{deploy_path}}/{{pagespeed_path}}* || true");
  })->select('stage=prod|qa & role=web'); // assuming pagespeed is installed only on prod and qa envs
  • Loading branch information
Misha Medzhytov committed Mar 3, 2024
1 parent ef0087d commit e4839b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Host/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ public function setLabels(array $labels): self
return $this;
}

public function addLabels(array $labels): self
{
$existingLabels = $this->getLabels() ?? [];
$this->setLabels(array_replace_recursive($existingLabels, $labels));
return $this;
}

public function getLabels(): ?array
{
return $this->config->get('labels', null);
Expand Down
14 changes: 12 additions & 2 deletions src/Selector/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ public static function apply(?array $conditions, Host $host): bool
foreach ($conditions as $hmm) {
$ok = [];
foreach ($hmm as list($op, $var, $value)) {
$ok[] = self::compare($op, $labels[$var] ?? null, $value);
if (is_array($value)) {
$orOk = [];
foreach ($value as $val) {
$orOk[] = self::compare($op, $labels[$var] ?? null, $val);
}
$ok[] = count(array_filter($orOk, $isTrue)) > 0;
} else {
$ok[] = self::compare($op, $labels[$var] ?? null, $value);
}

}
if (count($ok) > 0 && array_all($ok, $isTrue)) {
return true;
Expand Down Expand Up @@ -102,7 +111,8 @@ public static function parse(string $expression): array
continue;
}
if (preg_match('/(?<var>.+?)(?<op>!?=)(?<value>.+)/', $part, $match)) {
$conditions[] = [$match['op'], trim($match['var']), trim($match['value'])];
$values = array_map('trim', explode('|', trim($match['value'])));
$conditions[] = [$match['op'], trim($match['var']), $values];
} else {
$conditions[] = ['=', 'alias', trim($part)];
}
Expand Down

0 comments on commit e4839b4

Please sign in to comment.