blib/lib/App/karr/Cmd/Config.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/App/karr/Cmd/Board.pm ↑ Summary blib/lib/App/karr/Cmd/Create.pm »
1 # ABSTRACT: View or modify board configuration
2  
3 package App::karr::Cmd::Config;
4 our $VERSION = '0.602';
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MooX::Options (
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 usage_string => 'USAGE: karr config [show|get KEY|set KEY VALUE] '
9 . '[--defaults] [--json] [--compact]',
10 2 );
11 0 ▶use App::karr::Role::BoardAccess;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use App::karr::Role::Output;
13 use App::karr::Role::CompactOutput;
14 use App::karr::Config;
15  
16 with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output',
17 'App::karr::Role::CompactOutput';
18  
19  
20 option defaults => (
21 is => 'ro',
22 doc => "Print karr's built-in defaults instead of this board's config",
23 );
24  
25 my %WRITABLE = map { $_ => 1 } qw(
26 board.name board.description
27 defaults.status defaults.priority defaults.class
28 claim_timeout lock_timeout
29 foundation.enabled foundation.reason
30 );
31  
32 # The sentence the no-board refusal carries beyond `karr sync` / `karr init`.
33 # This command is the only read that has a third answer worth naming: the
34 # defaults it used to print silently are true of karr, just not of any board,
35 # and --defaults is where they are still available (#136).
36 my $DEFAULTS_HINT =
37 "To see the values a board created here would start with, run\n"
38 . "'karr config show --defaults' -- those are karr's, not any board's.\n";
39  
40 sub execute {
41 my ($self, $args_ref, $chain_ref) = @_;
42 my @pos = $self->positional_args($args_ref);
43 my $action = $pos[0] // 'show';
44  
45 # Action-dependent arity: show=1, get KEY=2, set KEY VALUE=3 positionals
46 # (the action itself is a positional). Reject surplus before any work/sync --
47 # and reject an action that is not one of the three here rather than after the
48 # config is loaded, so `karr config bogus` still says so in a repository that
49 # has no board to refuse over.
50 my %arity = ( show => 1, get => 2, set => 3 );
51 die "Unknown action: $action (use show, get, or set)\n" unless $arity{$action};
52 $self->check_positional_args($args_ref, $arity{$action});
53  
54 # Option validation before the board checks, so misuse still exits 2 (ADR
55 # 0002) rather than being pre-empted by a refusal about the board.
56 $self->usage_error('--defaults reads no board, so there is nothing to set')
57 if $self->defaults && $action eq 'set';
58  
59 # `set` writes, so it pulls first and then requires a whole board. `get` and
60 # `show` read, so they stay offline (#135) -- but they must still say whether
61 # anything was read: printing the code defaults for a board that was never
62 # loaded is the same lie for `board.name` that `0 tasks` was for the task
63 # list, and in a fresh clone it is the normal case (#136). --defaults asks
64 # for those defaults on purpose and touches neither store nor Git, so it
65 # answers outside a repository too.
66 my $config;
67 if ( $self->defaults ) {
68 $config = App::karr::Config->from_merged( App::karr::Config->default_config );
69 }
70 else {
71 if ( $action eq 'set' ) {
72 $self->sync_before;
73 $self->require_board;
74 }
75 else {
76 $self->require_local_board( hint => $DEFAULTS_HINT );
77 }
78 $config = App::karr::Config->from_merged($self->store->effective_config);
79 }
80  
81 if ($action eq 'show') {
82 $self->_show_all($config);
83 } elsif ($action eq 'get') {
84 # length, not truth, on both keys below: `or` reads the key "0" as no key
85 # at all and answers a usage error (2) where an unknown key answers
86 # "Unknown key" (1) -- the fourth and last site of the shape #153, #230 and
87 # #239 closed elsewhere (#244). No config key is numeric, so nothing real
88 # was unreachable; what was wrong is which side of the exit-code contract
89 # (ADR 0002) a bad key falls on. The value below already used //, which
90 # keeps `config set foundation.enabled 0` writable.
91 my $key = $pos[1];
92 die "Usage: karr config get KEY\n" unless defined $key && length $key;
93 $self->_get_key($config, $key);
94 } elsif ($action eq 'set') {
95 my $key = $pos[1];
96 die "Usage: karr config set KEY VALUE\n" unless defined $key && length $key;
97 my $val = $pos[2] // die "Usage: karr config set KEY VALUE\n";
98 $self->_set_key($config, $key, $val);
99 $self->sync_after;
100 }
101 }
102  
103 sub _show_all {
104 my ($self, $config) = @_;
105 my $d = $config->data;
106  
107 if ($self->json) {
108 $self->print_json($d);
109 return;
110 }
111  
112 my @keys = $self->_display_keys($d);
113  
114 # Same rows, no column: `key=value` is what a script cuts on, and the padded
115 # table is what a person reads. Until #254 this command took --compact from
116 # App::karr::Role::Output and printed the padded table for it anyway.
117 if ($self->compact) {
118 for my $entry (@keys) {
119 my ($key, $val) = @$entry;
120 printf "%s=%s\n", $key, $self->_format_value($val);
121 }
122 return;
123 }
124  
125 for my $entry (@keys) {
126 my ($key, $val) = @$entry;
127 printf "%-25s %s\n", $key, $self->_format_value($val);
128 }
129 }
130  
131 sub _display_keys {
132 my ($self, $d) = @_;
133 # Every list and mapping guarded before it is dereferenced: `config show` is
134 # how you find out that a config is broken, so it must not die dereferencing
135 # the very key that is wrong (ticket #78).
136 my $c = App::karr::Config->from_merged($d);
137 my $board = ref $d->{board} eq 'HASH' ? $d->{board} : {};
138 my $defaults = ref $d->{defaults} eq 'HASH' ? $d->{defaults} : {};
139 # The lists as configured, not just their names: `_format_value` renders the
140 # mapping form legibly, and `show` and `get` should not disagree about what
141 # the board's statuses are (ticket #130).
142 my $statuses = ref $d->{statuses} eq 'ARRAY' ? $d->{statuses} : [];
143 my $classes = ref $d->{classes} eq 'ARRAY' ? $d->{classes} : [];
144 # `defined`, not truth, on the six optional rows below. The question the
145 # filter asks is "did this config set the key at all", and a user-set `0` is
146 # a value, not an absence: `config set board.name 0` was accepted, `config get
147 # board.name` answered 0 and `config show --json` carried it, while this table
148 # dropped the row -- and with it the POD promise above that `diff <(karr
149 # config show) <(karr config show --defaults)` is the set of keys this board
150 # overrides, because `board.description` and `foundation.reason` have no
151 # default row for the override to differ from (#255). `foundation.enabled`
152 # already prints a bare `0` two lines down, unfiltered, so a zero row is
153 # nothing new in this output.
154 #
155 # `defined` alone rather than `defined && length`: the empty string is the
156 # same disagreement with a different value. `config set` cannot write one
157 # (#243 refuses an empty argument), but `karr import` and a hand-edited
158 # config.yml can, and there `config get board.description` prints an empty
159 # line at exit 0 while `--json` carries "" -- both call the key present, so
160 # the table does too. Rendering stays `_format_value`'s: it answers '' and
161 # leaves the row's right-hand side blank, which is the same nothing `config
162 # get` prints for it. A placeholder like `(empty)` would read better but is a
163 # notation no other surface of this command has, and would put `show` and
164 # `get` back in disagreement from the other side.
165 my @out;
166 push @out, ['version', $d->{version}];
167 push @out, ['board.name', $board->{name}] if defined $board->{name};
168 push @out, ['board.description', $board->{description}] if defined $board->{description};
169 push @out, ['tasks_dir', $d->{tasks_dir}];
170 push @out, ['statuses', $statuses];
171 push @out, ['priorities', [$c->priorities]];
172 push @out, ['defaults.status', $defaults->{status}] if defined $defaults->{status};
173 push @out, ['defaults.priority', $defaults->{priority}] if defined $defaults->{priority};
174 push @out, ['defaults.class', $defaults->{class}] if defined $defaults->{class};
175 push @out, ['claim_timeout', $d->{claim_timeout}];
176 push @out, ['lock_timeout', $d->{lock_timeout}];
177 push @out, ['classes', $classes];
178 push @out, ['foundation.enabled', $c->foundation_enabled];
179 push @out, ['foundation.reason', $d->{foundation}{reason}]
180 if ref $d->{foundation} eq 'HASH' && defined $d->{foundation}{reason};
181 return @out;
182 }
183  
184 sub _get_key {
185 my ($self, $config, $key) = @_;
186 my $val = $self->_resolve_key($config->data, $key);
187 die "Unknown key: $key\n" unless defined $val;
188  
189 if ($self->json) {
190 # Always wrapped in the requested key, scalar or not (#131). It used to hand
191 # a list or mapping over bare, and then `config get board --json` answered
192 # {"name":"..."} -- indistinguishable from the wrapped form of a scalar key
193 # named `name`, with nothing in the payload saying which of the two it was.
194 # Wrapping unconditionally also makes the answer a one-key subset of the
195 # `config show --json` object rather than a second schema.
196 $self->print_json({ $key => $val });
197 } else {
198 printf "%s\n", $self->_format_value($val);
199 }
200 }
201  
202 sub _set_key {
203 my ($self, $config, $key, $val) = @_;
204 die "Key '$key' is read-only\n" unless $WRITABLE{$key};
205  
206 my $d = $config->data;
207  
208 # Validate values. The same App::karr::Config validators the task write paths
209 # use, so `karr config set defaults.status X` and `karr create --status X`
210 # cannot disagree about what a legal status is (ticket #54).
211 if ($key eq 'defaults.status') {
212 $config->validate_status($val);
213 } elsif ($key eq 'defaults.priority') {
214 $config->validate_priority($val);
215 } elsif ($key eq 'defaults.class') {
216 $config->validate_class($val) if $val ne '';
217 } elsif ($key eq 'claim_timeout') {
218 # Both timeouts through the one Go-grammar parser, so `1h30m` means ninety
219 # minutes here and in kanban-md (ticket #78). usage_error rather than a bare
220 # die: an option value that parses but is not a duration is misuse, and the
221 # exit-code contract says 2.
222 $self->usage_error(
223 qq{invalid claim_timeout "$val" (use e.g. 1h, 30m, 1h30m, 0s to disable)})
224 unless defined App::karr::Config->parse_duration($val);
225 } elsif ($key eq 'lock_timeout') {
226 $self->usage_error(qq{invalid lock_timeout "$val" (use e.g. 5m, 30s, 0s to disable)})
227 unless defined App::karr::Config->parse_duration($val);
228 } elsif ($key eq 'foundation.enabled') {
229 # A bare "false" from the command line is true in Perl -- coerce here so the
230 # stored value is the same 1/0 `karr disable`/`karr enable` write.
231 $val = App::karr::Config->parse_bool($val);
232 }
233  
234 # Set the value
235 if ($key =~ /^(\w+)\.(\w+)$/) {
236 $d->{$1}{$2} = $val;
237 } else {
238 $d->{$key} = $val;
239 }
240  
241 $self->store->save_config($d);
242  
243 if ($self->json) {
244 $self->print_json({ key => $key, value => $val });
245 } else {
246 printf "Set %s = %s\n", $key, $val;
247 }
248 }
249  
250 sub _resolve_key {
251 my ($self, $d, $key) = @_;
252 if ($key =~ /^(\w+)\.(\w+)$/) {
253 return $d->{$1}{$2};
254 }
255 return $d->{$key};
256 }
257  
258 sub _format_value {
259 my ($self, $val) = @_;
260 return '' unless defined $val;
261 if (ref $val eq 'ARRAY') {
262 return join(', ', map { $self->_format_entry($_) } @$val);
263 } elsif (ref $val eq 'HASH') {
264 return $self->_format_settings($val, sort keys %$val);
265 }
266 return "$val";
267 }
268  
269 # One entry of a config list. `statuses` and `classes` allow both a bare name
270 # and the mapping form ({ name => 'in-progress', require_claim => 1 }), and
271 # joining the raw list stringified every mapping as HASH(0x...) -- unreadable in
272 # the one output a reader consults to learn which columns a board has, which is
273 # how an "extended status set" nobody configures got invented (ticket #130). The
274 # name leads and its per-entry settings follow in parentheses, so the value
275 # stays a single greppable line and still says which status wants a claim. On a
276 # default board that claim flag is the only setting left to show: since #227 the
277 # four default classes are name-only mappings and karr models no WIP limits at
278 # all, so a class with settings in this rendering is one the board itself set or
279 # imported. --json is untouched by this: it carries the entries as configured,
280 # never a rendering of them.
281 sub _format_entry {
282 my ($self, $entry) = @_;
283 return $self->_format_value($entry) unless ref $entry eq 'HASH';
284 my @settings = grep { $_ ne 'name' } sort keys %$entry;
285 my $name = defined $entry->{name} ? $entry->{name} : '';
286 return $name unless @settings;
287 return sprintf '%s(%s)', length $name ? "$name " : '',
288 $self->_format_settings($entry, @settings);
289 }
290  
291 sub _format_settings {
292 my ($self, $hash, @keys) = @_;
293 return join(', ', map { "$_: " . $self->_format_value($hash->{$_}) } @keys);
294 }
295  
296 1;
297  
298 __END__
« blib/lib/App/karr/Cmd/Board.pm ↑ Summary blib/lib/App/karr/Cmd/Create.pm »