blib/lib/Kubernetes/REST/CLI.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 108 CC 11 SCAR 44.0
CC
11
Cov
0%
format_output
132.0
execute
2.0
BEGIN
2.0
CRAP 81.4
1 package Kubernetes::REST::CLI;
2 # ABSTRACT: CLI base class for Kubernetes::REST command-line tools
3 our $VERSION = '1.109';
4 use Moo;
5 use MooX::Options;
6 use MooX::Cmd;
7 use JSON::MaybeXS;
8  
9 with 'Kubernetes::REST::CLI::Role::Connection';
10  
11  
12 option namespace => (
13 is => 'ro',
14 format => 's',
15 short => 'n',
16 default => sub { 'default' },
17 doc => 'Namespace for namespaced resources',
18 );
19  
20  
21 option output => (
22 is => 'ro',
23 format => 's',
24 short => 'o',
25 default => sub { 'json' },
26 doc => 'Output format: json, yaml, name',
27 );
28  
29  
30 has json => (
31 is => 'ro',
32 default => sub { JSON::MaybeXS->new->pretty->canonical->utf8 },
33 );
34  
35  
36 sub format_output {
37 my ($self, $result) = @_;
38 return unless defined $result;
39  
40 my $format = $self->output;
41  
42 if ($format eq 'json') {
43 my $data = ref($result) && $result->can('TO_JSON') ? $result->TO_JSON : $result;
44 print $self->json->encode($data);
45 } elsif ($format eq 'yaml') {
46 require YAML::XS;
47 my $data = ref($result) && $result->can('TO_JSON') ? $result->TO_JSON : $result;
48 print YAML::XS::Dump($data);
49 } elsif ($format eq 'name') {
50 if (ref($result) && $result->can('items')) {
51 for my $item (@{$result->items // []}) {
52 my $meta = $item->metadata;
53 my $ns = $meta->namespace ? $meta->namespace . '/' : '';
54 print $ns, $meta->name, "\n";
55 }
56 } elsif (ref($result) && $result->can('metadata')) {
57 my $meta = $result->metadata;
58 my $ns = $meta->namespace ? $meta->namespace . '/' : '';
59 print $ns, $meta->name, "\n";
60 }
61 } else {
62 require Data::Dumper;
63 print Data::Dumper::Dumper($result);
64 }
65 }
66  
67  
68 sub execute {
69 my ($self, $args, $chain) = @_;
70 print "Usage: kube_client <command> [options]\n\n";
71 print "Commands:\n";
72 print " get <Kind> [name] Get resource(s)\n";
73 print " create -f <file> Create resource from file\n";
74 print " delete <Kind> <name> Delete resource\n";
75 print " raw <Group> <Method> Raw API call\n";
76 print "\nRun 'kube_client --help' for options.\n";
77 print "See also: kube_watch <Kind> for live event streaming.\n";
78 return 0;
79 }
80  
81  
82  
83 1;
84  
85 __END__
86  
87 =pod
88  
89 =encoding UTF-8
90  
91 =head1 NAME
92  
93 Kubernetes::REST::CLI - CLI base class for Kubernetes::REST command-line tools
94  
95 =head1 VERSION
96  
97 version 1.109
98  
99 =head1 SYNOPSIS
100  
101 kube_client <command> [options]
102  
103 # Get resources
104 kube_client get Pod my-pod -n default
105 kube_client get Deployment --output yaml
106  
107 # Create from file
108 kube_client create -f deployment.yaml
109  
110 # Delete resources
111 kube_client delete Pod my-pod -n default
112  
113 =head1 DESCRIPTION
114  
115 Base class for the C<kube_client> command-line tool. Provides common functionality for managing Kubernetes resources from the command line.
116  
117 This tool uses L<Kubernetes::REST::Kubeconfig> to connect to the cluster: C<--kubeconfig> if given, otherwise the C<KUBECONFIG> environment variable, otherwise C<~/.kube/config>. A C<:>-separated list of kubeconfig files is merged the way C<kubectl> merges it, see L<Kubernetes::REST::Kubeconfig/MERGING>.
118  
119 =head1 COMMANDS
120  
121 =head2 get
122  
123 kube_client get <Kind> [name] [options]
124  
125 Get a resource or list resources.
126  
127 =over
128  
129 =item B<get Pod> - List all pods in the namespace
130  
131 =item B<get Pod my-pod> - Get a specific pod
132  
133 =item B<--output json> (default) - JSON output
134  
135 =item B<--output yaml> - YAML output
136  
137 =item B<--output name> - Names only
138  
139 =back
140  
141 =head2 create
142  
143 kube_client create -f <file>
144  
145 Create resources from a YAML or JSON manifest. Use C<-f -> to read from stdin.
146 The format is detected from the content, and multi-document YAML creates every
147 C<--->-separated document in order.
148  
149 =head2 delete
150  
151 kube_client delete <Kind> <name> [options]
152  
153 Delete a resource by name.
154  
155 =head2 raw
156  
157 kube_client raw <Group> <Method> [key=value ...]
158  
159 Make a raw call through the v0 compatibility layer.
160  
161 =head1 GLOBAL OPTIONS
162  
163 =head2 namespace
164  
165 Namespace for namespaced resources. Defaults to C<default>.
166  
167 Short form: C<-n>
168  
169 =head2 output
170  
171 Output format. One of: C<json>, C<yaml>, C<name>.
172  
173 Defaults to C<json>.
174  
175 Short form: C<-o>
176  
177 =head1 ATTRIBUTES
178  
179 =head2 json
180  
181 L<JSON::MaybeXS> encoder instance used for C<--output json>. Configured
182 C<pretty>, C<canonical> (stable key ordering) and C<utf8> - the C<utf8> flag
183 is what makes C<format_output> print UTF-8 bytes instead of characters, so
184 non-ASCII values survive the way to the terminal instead of triggering "Wide
185 character in print".
186  
187 =head1 METHODS
188  
189 =head2 format_output
190  
191 $cli->format_output($result);
192  
193 Format and print the result according to the C<--output> option
194 (C<json>, C<yaml>, or C<name>). Any other value falls back to a raw dump via
195 L<Data::Dumper>, mainly useful for debugging.
196  
197 =head2 execute
198  
199 Default C<execute> method, invoked by L<MooX::Cmd> when C<kube_client> is run
200 without a recognized subcommand. Prints usage information to STDOUT.
201  
202 =head1 SEE ALSO
203  
204 =over
205  
206 =item * L<Kubernetes::REST> - Main API module
207  
208 =item * L<Kubernetes::REST::CLI::Cmd::Get> - The get command
209  
210 =item * L<Kubernetes::REST::CLI::Cmd::Create> - The create command
211  
212 =item * L<Kubernetes::REST::CLI::Cmd::Delete> - The delete command
213  
214 =item * L<Kubernetes::REST::CLI::Cmd::Raw> - The raw command
215  
216 =item * L<Kubernetes::REST::CLI::Watch> - Watch command implementation
217  
218 =item * L<Kubernetes::REST::CLI::Role::Connection> - Connection options
219  
220 =back
221  
222 =head1 SUPPORT
223  
224 =head2 Issues
225  
226 Please report bugs and feature requests on GitHub at
227 L<https://github.com/pplu/kubernetes-rest/issues>.
228  
229 =head2 IRC
230  
231 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
232  
233 =head1 CONTRIBUTING
234  
235 Contributions are welcome! Please fork the repository and submit a pull request.
236  
237 =head1 AUTHORS
238  
239 =over 4
240  
241 =item *
242  
243 Torsten Raudssus <getty@cpan.org>
244  
245 =item *
246  
247 Jose Luis Martinez Torres <jlmartin@cpan.org>
248  
249 =back
250  
251 =head1 COPYRIGHT AND LICENSE
252  
253 This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.
254  
255 This is free software, licensed under:
256  
257 The Apache License, Version 2.0, January 2004
258  
259 =cut