From 6587fb53a5cb46242540e75499739489045041a1 Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Thu, 4 Jan 2018 16:36:16 -0500 Subject: [PATCH] Add 'trust' command --- cli/Valet/Brew.php | 13 +++++++++++++ cli/Valet/Valet.php | 13 +++++++++++++ cli/valet.php | 10 ++++++++++ tests/ConfigurationTest.php | 13 +++++++++++++ 4 files changed, 49 insertions(+) diff --git a/cli/Valet/Brew.php b/cli/Valet/Brew.php index f37f75f..d6982d5 100644 --- a/cli/Valet/Brew.php +++ b/cli/Valet/Brew.php @@ -197,4 +197,17 @@ function restartLinkedPhp() { $this->restartService($this->linkedPhp()); } + + /** + * Create the "sudoers.d" entry for running Brew. + * + * @return void + */ + function createSudoersEntry() + { + $this->files->ensureDirExists('/etc/sudoers.d'); + + $this->files->put('/etc/sudoers.d/brew', 'Cmnd_Alias BREW = /usr/local/bin/brew * +%admin ALL=(root) NOPASSWD: BREW'.PHP_EOL); + } } diff --git a/cli/Valet/Valet.php b/cli/Valet/Valet.php index 68e82f1..a271042 100644 --- a/cli/Valet/Valet.php +++ b/cli/Valet/Valet.php @@ -65,4 +65,17 @@ function onLatestVersion($currentVersion) return version_compare($currentVersion, trim($response->body->tag_name, 'v'), '>='); } + + /** + * Create the "sudoers.d" entry for running Valet. + * + * @return void + */ + function createSudoersEntry() + { + $this->files->ensureDirExists('/etc/sudoers.d'); + + $this->files->put('/etc/sudoers.d/valet', 'Cmnd_Alias VALET = /usr/local/bin/valet * +%admin ALL=(root) NOPASSWD: VALET'.PHP_EOL); + } } diff --git a/cli/valet.php b/cli/valet.php index c9a7ef3..17b8251 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -253,6 +253,16 @@ output('NO'); } })->descriptions('Determine if this is the latest version of Valet'); + + /** + * Install the sudoers.d entries so password is no longer required. + */ + $app->command('trust', function () { + Brew::createSudoersEntry(); + Valet::createSudoersEntry(); + + info('Sudoers entries have been added for Brew and Valet.'); + })->descriptions('Add sudoers files for Brew and Valet to make Valet commands run without passwords'); } /** diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 8c77dfd..46c7d74 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -1,5 +1,7 @@ shouldReceive('write')->once()->with(['foo' => 'bar', 'bar' => 'baz']); $config->updateKey('bar', 'baz'); } + + + public function test_trust_adds_the_sudoer_files() + { + $files = Mockery::mock(Filesystem::class.'[ensureDirExists,put]'); + $files->shouldReceive('ensureDirExists')->with('/etc/sudoers.d')->twice(); + $files->shouldReceive('put')->twice(); + swap(Filesystem::class, $files); + resolve(Brew::class)->createSudoersEntry(); + resolve(Valet::class)->createSudoersEntry(); + } }