From 69a60295552389f4d705e59c8841494e345473bd Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 11:05:46 +0200 Subject: [PATCH] put env tool --- tools/putenv.php | 15 ------------- tools/putenv/env | 5 +++++ tools/putenv/putenv.php | 50 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 15 deletions(-) delete mode 100644 tools/putenv.php create mode 100644 tools/putenv/env create mode 100644 tools/putenv/putenv.php diff --git a/tools/putenv.php b/tools/putenv.php deleted file mode 100644 index b40c44b..0000000 --- a/tools/putenv.php +++ /dev/null @@ -1,15 +0,0 @@ -file = $argv[1]; + $this->key = $argv[2]; + $this->value -> $argv[3]; + break; + + case 2: + $this->file = $argv[1]; + $this->key = $argv[2]; + $this->value = fgets(STDIN); + break; + } + if (!file_exists($this->file)) { + throw new Exception('File not found'); + } + $this->content = file($this->file); + foreach ($this->content as $l => $line) { + if (!str_starts_with($line, $this->key)) { + continue; + } + $next = trim(substr($line, strlen($this->key))); + if (strlen($next) === 0 || $next[0] !== '=') { + continue; + } + $key = $this->key; + $value = $this->value; + if (strpos($value, ' ') !== false) { + $value = '"' . addslashes($value) . '"'; + } + $this->content[$l] = $key . '=' . $value; + } + $content = implode("\n", $this->content); + file_put_contents($this->file, $content); + } +} + +new PutEnv();