Skip to content

Commit

Permalink
feat(HasSudoPassword): Add new trait
Browse files Browse the repository at this point in the history
- Add a new trait HasSudoPassword.php to handle sudo password functionality.
- It includes methods to get and set sudo password.
  • Loading branch information
guanguans committed Jul 13, 2023
1 parent 565ddcd commit 6e3434c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/Concerns/HasSudoPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/soar-php.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\SoarPHP\Concerns;

/**
* @mixin \Guanguans\SoarPHP\Soar
*/
trait HasSudoPassword
{
/**
* @var null|string
*/
protected $sudoPassword;

public function getSudoPassword(): ?string
{
return $this->sudoPassword;
}

public function setSudoPassword(?string $sudoPassword): self
{
$this->sudoPassword = $sudoPassword;

return $this;
}
}
11 changes: 10 additions & 1 deletion src/Concerns/WithRunable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Guanguans\SoarPHP\Exceptions\InvalidArgumentException;
use Guanguans\SoarPHP\Exceptions\ProcessFailedException;
use Guanguans\SoarPHP\Support\EscapeArg;
use Symfony\Component\Process\Process;

/**
Expand Down Expand Up @@ -57,7 +58,15 @@ private function createProcess($withOptions = [], ?callable $processTapper = nul
{
$process = \is_string($withOptions)
? Process::fromShellCommandline("$this->soarPath {$this->getSerializedNormalizedOptions()} $withOptions")
: new Process(array_merge([$this->soarPath], $this->clone()->mergeOptions($withOptions)->getNormalizedOptions()));
: new Process(array_merge([$this->soarPath], $this->clone()->mergeOptions($withOptions)->getNormalizedOptions(), []));

if ($this->sudoPassword) {
$process = Process::fromShellCommandline(sprintf(
"'echo' %s | 'sudo' '-S' %s",
EscapeArg::escape($this->sudoPassword),
$process->getCommandLine()
));
}

return (static function (Process $process) use ($processTapper): Process {
if (\is_callable($processTapper)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Soar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Guanguans\SoarPHP\Concerns\ConcreteScores;
use Guanguans\SoarPHP\Concerns\HasOptions;
use Guanguans\SoarPHP\Concerns\HasSoarPath;
use Guanguans\SoarPHP\Concerns\HasSudoPassword;
use Guanguans\SoarPHP\Concerns\WithDumpable;
use Guanguans\SoarPHP\Concerns\WithRunable;

Expand All @@ -25,6 +26,7 @@ class Soar implements Contracts\Soar
use ConcreteScores;
use HasOptions;
use HasSoarPath;
use HasSudoPassword;
use WithDumpable;
use WithRunable;

Expand Down

0 comments on commit 6e3434c

Please sign in to comment.