Misc improvements

- Renamed the state file from `fresh.yml` to `.fresh.yml`.
- Added option `--state` to override the state file name.
- Renamed the lock file from `fresh.lock` to `.fresh.lock`.
- Added option `--lockfile` to override lockfile file name.
This commit is contained in:
2022-03-21 00:48:19 +01:00
parent 702cc3101e
commit 214db1cee3
4 changed files with 115 additions and 36 deletions

View File

@ -2,6 +2,25 @@
namespace NoccyLabs\FreshDocker;
/*
Fresh.phar
Copyright (C) 2022 NoccyLabs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use NoccyLabs\FreshDocker\State\Log;
use NoccyLabs\FreshDocker\Configuration\ComposeConfiguration;
use NoccyLabs\FreshDocker\Configuration\LocalConfiguration;
@ -16,6 +35,11 @@ use NoccyLabs\FreshDocker\State\Lockfile;
class Refresher
{
/**
* Map for determining options from command line and environment. Each item
* has the format:
* name => [ short, long, description, [envvar, [default]]]
*/
private static array $optionsMap = [
'General' => [
'help' => [ 'h', 'help', "Show this help" ],
@ -26,7 +50,7 @@ class Refresher
'pull' => [ null, 'pull', "Only pull if updated, don't up" ],
'check' => [ null, 'check', "Only check for updates, set exit code" ],
'prune' => [ null, 'prune', "Prune dangling images after pull and up" ],
'write-state' => [ 'w', 'write-state', "Always write updated state (only useful with --check)", false ],
'write-state' => [ 'w', 'write-state', "Always write updated state (only useful with --check)", null, false ],
],
'Hooks' => [
'slack' => [ null, 'slack:', "Notify a slack webhook when updating", "FRESH_SLACK" ],
@ -36,10 +60,16 @@ class Refresher
'Config' => [
'config' => [ 'c:', 'config:', "Use custom configuration file", "FRESH_CONFIG" ],
'config-type' => [ 'C:', 'config-type:', "Configuration type (auto, fresh, compose)", "FRESH_CONFIG_TYPE", "auto" ],
'state' => [ 's:', 'state:', "Override the state file name", "FRESH_STATE", ".fresh.yml" ],
'lockfile' => [ 'l:', 'lockfile:', "Override the lockfile file name", "FRESH_LOCKFILE", ".fresh.lock" ],
'credentials' => [ null, 'credentials:', "Set credentials loader type (auto or basic)", "FRESH_CREDENTIALS", "auto" ],
]
];
private static $StateFileName = ".fresh.yml";
private static $LockFileName = ".fresh.lock";
/** @var array The parsed options */
private array $options = [];
/** @var HookInterface[] The hooks to invoke */
@ -112,6 +142,11 @@ class Refresher
}
/**
* Print the usage info (from --help or -h)
*
*
*/
public function printUsage()
{
$tty = posix_isatty(STDOUT);
@ -186,11 +221,13 @@ class Refresher
exit(($updated === null) ? 0 : 1);
} catch (\Throwable $t) {
fprintf(STDERR, "fatal: %s (%s#%d)\n", $t->getMessage(), $t->getFile(), $t->getLine());
if (!$this->options['verbose']) {
fprintf(STDERR, $this->log->asString()."\n");
}
exit(2);
}
}
@ -210,8 +247,14 @@ class Refresher
$this->log->append("Working dir: ".$this->path);
chdir($this->path);
$this->state = new PersistentState($this->path . "/fresh.yml");
$this->lockfile = new Lockfile($this->path . "/fresh.lock");
$statefile = $this->options['state'];
if (!str_starts_with($statefile,'/')) $statefile = $this->path . "/" . $statefile;
$lockfile = $this->options['lockfile'];
if (!str_starts_with($lockfile,'/')) $lockfile = $this->path . "/" . $lockfile;
$this->state = new PersistentState($statefile); // $this->path . "/" . self::$StateFileName);
$this->lockfile = new Lockfile($lockfile); // $this->path . "/" . self::$LockFileName);
}
/**
@ -224,7 +267,7 @@ class Refresher
case 'auto':
case 'basic':
$this->credentialsLoader = new BasicCredentialsLoader();
$this->log->append("Using BasicCredentialsLoader for credentials");
$this->log->append("Using BasicCredentialsLoader");
break;
default:
fwrite(STDERR, "error: Invalid credentials loader type\n");