openssl signing
[pharext/pharext] / src / pharext / CliCommand.php
1 <?php
2
3 namespace pharext;
4
5 require_once __DIR__."/Version.php";
6
7 trait CliCommand
8 {
9 /**
10 * Command line arguments
11 * @var pharext\CliArgs
12 */
13 private $args;
14
15 /**
16 * @inheritdoc
17 * @see \pharext\Command::getArgs()
18 */
19 public function getArgs() {
20 return $this->args;
21 }
22
23 /**
24 * Output pharext vX.Y.Z header
25 */
26 function header() {
27 printf("pharext v%s (c) Michael Wallner <mike@php.net>\n\n", VERSION);
28 }
29
30 /**
31 * @inheritdoc
32 * @see \pharext\Command::info()
33 */
34 public function info($fmt) {
35 if (!$this->args->quiet) {
36 vprintf($fmt, array_slice(func_get_args(), 1));
37 }
38 }
39
40 /**
41 * @inheritdoc
42 * @see \pharext\Command::error()
43 */
44 public function error($fmt) {
45 if (!$this->args->quiet) {
46 if (!isset($fmt)) {
47 $fmt = "%s\n";
48 $arg = error_get_last()["message"];
49 } else {
50 $arg = array_slice(func_get_args(), 1);
51 }
52 vfprintf(STDERR, "ERROR: $fmt", $arg);
53 }
54 }
55
56 /**
57 * Output command line help message
58 * @param string $prog
59 */
60 public function help($prog) {
61 printf("Usage:\n\n \$ %s", $prog);
62
63 $flags = [];
64 $required = [];
65 $optional = [];
66 foreach ($this->args->getSpec() as $spec) {
67 if ($spec[3] & CliArgs::REQARG) {
68 if ($spec[3] & CliArgs::REQUIRED) {
69 $required[] = $spec;
70 } else {
71 $optional[] = $spec;
72 }
73 } else {
74 $flags[] = $spec;
75 }
76 }
77
78 if ($flags) {
79 printf(" [-%s]", implode("", array_column($flags, 0)));
80 }
81 foreach ($required as $req) {
82 printf(" -%s <arg>", $req[0]);
83 }
84 if ($optional) {
85 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
86 }
87 printf("\n\n");
88 $spc = $this->args->getSpec();
89 $max = max(array_map("strlen", array_column($spc, 1)));
90 $max += $max % 8 + 2;
91 foreach ($spc as $spec) {
92 if (isset($spec[0])) {
93 printf(" -%s|", $spec[0]);
94 } else {
95 printf(" ");
96 }
97 printf("--%s ", $spec[1]);
98 if ($spec[3] & CliArgs::REQARG) {
99 printf("<arg> ");
100 } elseif ($spec[3] & CliArgs::OPTARG) {
101 printf("[<arg>]");
102 } else {
103 printf(" ");
104 }
105 printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0])), $spec[2]);
106 if ($spec[3] & CliArgs::REQUIRED) {
107 printf(" (REQUIRED)");
108 }
109 if (isset($spec[4])) {
110 printf(" [%s]", $spec[4]);
111 }
112 printf("\n");
113 }
114 printf("\n");
115 }
116
117 /**
118 * Create temporary file/directory name
119 * @param string $prefix
120 * @param string $suffix
121 */
122 private function tempname($prefix, $suffix = null) {
123 if (!isset($suffix)) {
124 $suffix = uniqid();
125 }
126 return sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, $suffix);
127 }
128
129 /**
130 * Create a new temp directory
131 * @param string $prefix
132 * @return string
133 */
134 private function newtemp($prefix) {
135 $temp = $this->tempname($prefix);
136 if (!is_dir($temp)) {
137 if (!mkdir($temp, 0700, true)) {
138 $this->error(null);
139 exit(3);
140 }
141 }
142 return $temp;
143 }
144
145 /**
146 * rm -r
147 * @param string $dir
148 */
149 private function rm($dir) {
150 foreach (scandir($dir) as $entry) {
151 if ($entry === "." || $entry === "..") {
152 continue;
153 } elseif (is_dir("$dir/$entry")) {
154 $this->rm("$dir/$entry");
155 } elseif (!unlink("$dir/$entry")) {
156 $this->error(null);
157 }
158 }
159 if (!rmdir($dir)) {
160 $this->error(null);
161 }
162 }
163 }