Passed
Push — master ( 6015fa...83e3bb )
by Tom
02:48
created

Help::showUsage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\Cli\Args;
9
use Ktomk\Pipelines\Cli\Streams;
10
11
class Help
12
{
13
    /**
14
     * @var Streams
15
     */
16
    private $streams;
17
18
    /**
19
     * Help constructor.
20
     * @param Streams $streams
21
     */
22 1
    public function __construct(Streams $streams)
23
    {
24 1
        $this->streams = $streams;
25 1
    }
26
27 2
    public function showVersion()
28
    {
29 2
        $version = Version::resolve(App::VERSION);
30 2
        $this->streams->out(
31 2
            sprintf("pipelines version %s\n", $version)
32
        );
33
34 2
        return 0;
35
    }
36
37 3
    public function showUsage()
38
    {
39 3
        $this->streams->out(
40
            <<<'EOD'
41 3
usage: pipelines [<options>...] [--version | [-h | --help]]
42
       pipelines [-v | --verbose] [--working-dir <path>]
43
                 [--[no-|error-]keep] [--prefix <prefix>]
44
                 [--basename <basename>]
45
                 [[-e | --env] <variable>] [--env-file <path>]
46
                 [--file <path>] [--dry-run] [--no-run] [--list]
47
                 [--deploy mount | copy ] [--show] [--images]
48
                 [--pipeline <id>] [--trigger <ref>] [--verbatim]
49
       pipelines [-v | --verbose] [--dry-run] [--docker-list]
50
                 [--docker-kill] [--docker-clean]
51
52
EOD
53
        );
54 3
    }
55
56 2
    public function showHelp()
57
    {
58 2
        $this->showUsage();
59 2
        $this->streams->out(
60
            <<<'EOD'
61
62
    -h, --help            show usage and help information
63
    -v, --verbose         show commands executed
64
    --version             show version information only and exit
65
66
Common options
67
    --basename <basename> set basename for pipelines file,
68
                          default is 'bitbucket-pipelines.yml'
69
    --deploy mount|copy   how files from the working directory
70
                          are placed into the pipeline container:
71
                          copy     (default) working dir is
72
                                 copied into the container.
73
                                 stronger isolation as the
74
                                 pipeline scripts can change
75
                                 all files without side-effects
76
                                 in the working directory
77
                          mount    the working directory is
78
                                 mounted. fastest, no isolation
79
    -e, --env <variable>  pass or set an environment variables
80
                          for the docker container
81
    --env-file <path>     pass variables from environment file
82
                          to the docker container
83
    --file <path>         path to the pipelines file, overrides
84
                          looking up the <basename> file from
85
                          the current working directory
86
    --trigger <ref>       build trigger, <ref> can be of either
87
                          tag:<name>, branch:<name> or
88
                          bookmark:<name>. used in determination
89
                          of the pipeline to run
90
    --pipeline <id>       run pipeline with <id>, see --list
91
    --verbatim            only give verbatim output of the
92
                          pipeline, no other information around
93
                          like which step currently executes
94
    --working-dir <path>  run as if pipelines was started in
95
                          <path>
96
97
Run control options
98
    --dry-run             do not invoke docker or run containers,
99
                          with --verbose shows the commands that
100
                          would have run w/o the --dry-run flag
101
    --no-run              do not run the pipeline
102
103
Keep options
104
    --keep                always keep docker containers
105
    --error-keep          keep docker docker containers if a
106
                          step failed; the non-zero exit status
107
                          is output and the id of the container
108
                          kept
109
    --no-keep             do not keep docker containers; default
110
                          behaviour
111
112
File information options
113
    --images              list all images in file, in order
114
                          of use, w/o duplicate names and exit
115
    --list                list pipeline <id>s in file and exit
116
    --show                show information about pipelines in
117
                          file and exit
118
119
Docker container maintenance options
120
      usage might leave containers on the system. either by
121
      interrupting a running pipeline step or by keeping the
122
      running containers (--keep).
123
124
      pipelines uses a prefix followed by '-' and a UUID for
125
      container names. the prefix is either 'pipelines' or the
126
      one set by --prefix <prefix>.
127
128
      three options are built-in to monitor and interact with
129
      leftovers. if one or more of these are given, the following
130
      operations are executed in the order from top to down:
131
132
    --docker-list         list prefixed containers
133
    --docker-kill         kills prefixed containers
134
    --docker-clean        remove (non-running) containers with
135
                          pipelines prefix
136
137
Less common options
138
    --debug               flag for trouble-shooting fatal errors
139
    --prefix <prefix>     use a different prefix for container
140
                          names, default is 'pipelines'
141
142
EOD
143
        );
144
145 2
        return 0;
146
    }
147
148
    /**
149
     * @param Args $args
150
     * @throws InvalidArgumentException
151
     * @throws StatusException
152
     */
153 3
    public function run(Args $args)
154
    {
155
        # quickly handle version
156 3
        if ($args->hasOption('version')) {
157 1
            StatusException::status($this->showVersion());
158
        }
159
160
        # quickly handle help
161 2
        if ($args->hasOption(array('h', 'help'))) {
162 1
            StatusException::status($this->showHelp());
163
        }
164 1
    }
165
}
166