| 1 | package Kubernetes::REST::CLI::Cmd::Create; | ||||||||||||||||||||||||||||
| 2 | 1 | our $VERSION = '1.110'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: The create command of kube_client | ||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use MooX::Options; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MooX::Cmd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | option file => ( | ||||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||
| 12 | format => 's', | ||||||||||||||||||||||||||||
| 13 | short => 'f', | ||||||||||||||||||||||||||||
| 14 | doc => 'File to read (- for stdin)', | ||||||||||||||||||||||||||||
| 15 | 9 | ▶ | default => sub { '-' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | sub _source { | ||||||||||||||||||||||||||
|
Subroutine
_source: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 21 | 5 | ▶ | return $self->file eq '-' ? 'stdin' : $self->file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 8 | ▶ | sub _read_input { | ||||||||||||||||||||||||||
|
Subroutine
_read_input: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 25 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 8 | my $input; | |||||||||||||||||||||||||||
| 28 | 8 | ▶ | if ($self->file eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 2 | local $/; | |||||||||||||||||||||||||||
| 30 | 2 | $input = <STDIN>; | |||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||
| 32 | 6 | ▶ | open my $fh, '<', $self->file or die "Cannot open " . $self->file . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 5 | local $/; | |||||||||||||||||||||||||||
| 34 | 5 | $input = <$fh>; | |||||||||||||||||||||||||||
| 35 | 5 | close $fh; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 7 | ▶ | return defined $input ? $input : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 19 | ▶ | sub _parse_manifest { | ||||||||||||||||||||||||||
|
Subroutine
_parse_manifest: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 42 | 19 | my ($self, $api, $input) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 19 | ▶ | die "Empty manifest read from " . $self->_source . "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $input and $input =~ /\S/
MC/DC50%1/2
defined $input and $input =~ /\S/
defined $input
$input =~ /\S/
Truth table66%2/3
A and B
A= defined $inputB= $input =~ /\S/
| |||||||||||||||||||||||||||||
| 45 | unless defined $input && $input =~ /\S/; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # A JSON manifest is a JSON object, so after optional whitespace it starts | ||||||||||||||||||||||||||||
| 48 | # with a brace, while a YAML manifest starts with a key, a comment or '---'. | ||||||||||||||||||||||||||||
| 49 | # YAML is a superset of JSON and would parse both, but routing JSON to | ||||||||||||||||||||||||||||
| 50 | # inflate() leaves the path it already took completely untouched: the same | ||||||||||||||||||||||||||||
| 51 | # decoder, the same UTF-8 bytes straight in (that decoder is utf8 => 1, so | ||||||||||||||||||||||||||||
| 52 | # there is no decode step to get wrong) and JSON error messages, with a | ||||||||||||||||||||||||||||
| 53 | # character offset, for someone who wrote JSON. | ||||||||||||||||||||||||||||
| 54 | 16 | ▶ | return $api->inflate($input) if $input =~ /\A\s*\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # load_yaml() parses characters, not bytes - handing it the raw bytes turns | ||||||||||||||||||||||||||||
| 57 | # every non-ASCII value into mojibake. It also treats a newline-free | ||||||||||||||||||||||||||||
| 58 | # argument as a file name, which the trailing newline keeps us out of. | ||||||||||||||||||||||||||||
| 59 | 11 | ▶ | $input .= "\n" unless $input =~ /\n/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 11 | my $objects = $api->load_yaml(Encode::decode('UTF-8', $input)); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | die "No Kubernetes manifest documents found in " . $self->_source . "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | unless @$objects; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 9 | return @$objects; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 5 | my ($self, $args, $chain) = @_; | |||||||||||||||||||||||||||
| 70 | 5 | my $root = $chain->[0]; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | my @objects = $self->_parse_manifest($root->api, $self->_read_input); | |||||||||||||||||||||||||||
| 73 | 5 | my $multi = @objects > 1; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | for my $i (0 .. $#objects) { | |||||||||||||||||||||||||||
| 76 | 8 | my $obj = $objects[$i]; | |||||||||||||||||||||||||||
| 77 | 8 | my $result = eval { $root->api->create($obj) }; | |||||||||||||||||||||||||||
| 78 | 8 | ▶ | if (my $error = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | # A single-document manifest reports its own error unchanged; in a | ||||||||||||||||||||||||||||
| 80 | # multi-document one the caller cannot tell which document failed. | ||||||||||||||||||||||||||||
| 81 | 2 | ▶ | die $error unless $multi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 1 | die sprintf "Document %d of %d (%s) in %s failed: %s", | |||||||||||||||||||||||||||
| 83 | $i + 1, scalar @objects, $obj->kind, $self->_source, $error; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 6 | $root->format_output($result); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | return 0; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | 1; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||