-
Notifications
You must be signed in to change notification settings - Fork 13
/
Config.php
102 lines (90 loc) · 2.69 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace DiffyCli;
use Symfony\Component\Yaml\Yaml;
class Config
{
/**
* Save API Key to configuration file.
*
* @param $key
* @throws \Exception
*/
public static function saveApiKey($key)
{
$config = self::getConfig(false);
$config['key'] = $key;
self::saveConfig($config);
}
/**
* Save Browserstack credentials.
*
* @param $username
* @param $accessKey
* @throws \Exception
*/
public static function saveBrowserstackCredentials($username, $accessKey)
{
$config = self::getConfig();
$config['browserStackUsername'] = $username;
$config['browserStackAccessKey'] = $accessKey;
self::saveConfig($config);
}
/**
* Save Lambdatest credentials.
*
* @param $username
* @param $accessToken
* @throws \Exception
*/
public static function saveLambdatestCredentials($username, $accessToken)
{
$config = self::getConfig();
$config['lambdaTestUsername'] = $username;
$config['lambdaTestAccessToken'] = $accessToken;
self::saveConfig($config);
}
/**
* Save configuration file.
*
* @param $config
* @throws \Exception
*/
public static function saveConfig($config)
{
$configPrefix = 'DIFFYCLI';
$configDirectory = getenv($configPrefix . '_CONFIG') ?: getenv('HOME') . '/.diffy-cli';
$writable = is_dir($configDirectory)
|| (!file_exists($configDirectory) && @mkdir($configDirectory, 0777, true));
$writable = $writable && is_writable($configDirectory);
if (!$writable) {
throw new \Exception(
'Could not save data to a file because the path "' . $configDirectory . '" cannot be written to.'
);
}
$configFilePath = $configDirectory . '/diffy-cli.yaml';
$yaml = Yaml::dump($config);
file_put_contents($configFilePath, $yaml);
}
/**
* Save Configuration.
*
* @return mixed
* @throws \Exception
*/
public static function getConfig($should_exist = true)
{
$configPrefix = 'DIFFYCLI';
$configFilePath = getenv($configPrefix . '_CONFIG') ?: getenv('HOME') . '/.diffy-cli/diffy-cli.yaml';
if (!file_exists($configFilePath)) {
$config = [];
if ($should_exist) {
throw new \Exception(
'Configuration file "' . $configFilePath . '" does not exist yet. Save your API KEY with "diffy auth:login" command.'
);
}
} else {
$config = Yaml::parseFile($configFilePath);
}
return $config;
}
}