Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
Shell scripts
* Readme
dict
edit_json (php)
encrypt/decrypt
pdoc
pftp - ftp a file to some place
php fork & IPC
php input cli
phplint
~/bin/inc/lib.php
10
found
auth
Name:
Category: Shell scripts
Body:
#!/usr/bin/env php <?php $file = @$argv[1]; if (! $file or $file == '-?' or $file == '-h') { echo " EditJson, supply a json file to edit it pretty-printed, it'll be written back compact.\n"; return; } if (! (file_exists($file) and is_readable($file) and is_writable($file))) { echo " E: file not found, or not readable, or not writeable.\n"; // you doofus exit(1); } // read the file, check it's valid json $data = file_get_contents($file); $json = ''; if (strlen($data) > 0) { $json = json_decode($data, 1); if ($error = json_last_error()) { echo " \e[31mE: json_decode error: ".json_last_error_msg()."\e[0m\n"; exit(2); } } // put the pretty-printed json in a temp file $fname = tempnam('/tmp', ''); file_put_contents($fname, json_encode($json, JSON_PRETTY_PRINT)); // edit the temp file, loop back if the saved content is not valid json. while (1) { system("nano $fname > $(tty)"); $content = file_get_contents($fname); if (strlen($content) > 0) { $json = json_decode($content, 1); if ($error = json_last_error()) { echo " \e[31mE: json_decode error: ".json_last_error_msg()."\e[0m\n"; $edit = input(" Edit again (y/n)? "); if (! in_array($edit, ['y', 'yes', 'ok', 'ja', 'j', 'oui', 'da', 'jawohl'])) { break; } }else{ file_put_contents($file, json_encode($json)); // write back compact json break; } } } unlink($fname); function input(string $prompt = null): string { echo $prompt; $handle = fopen ("php://stdin","r"); $output = fgets ($handle); return trim ($output); }
Footer