| 1 | package File::SOPS::Format::INI; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: INI format handler for SOPS | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 82 | our $VERSION = '0.005'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 82 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 82 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 82 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 82 | ▶ | use File::SOPS::Comment; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 82 | ▶ | use File::SOPS::Encrypted; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | # The tied hash docs/adr/0036 requires. USED WHERE IT STANDS rather than | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # copied: two copies of an order-preserving hash is exactly how ENV and INI | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # drift apart, which both tickets warned about for weeks. k158 gave it a | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # file of its own, so this loads the class itself where it used to load the | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # whole dotenv handler to reach it. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 82 | ▶ | use File::SOPS::Format::ENV::Ordered; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 82 | ▶ | use File::SOPS::Metadata; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 82 | ▶ | use File::SOPS::Metadata::Flat; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 82 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Same reason as in the other three handlers: every frame between a caller and | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # this module is this distribution's own, so a refusal raised here reported a | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # line inside File::SOPS rather than the line the caller wrote encrypt() or | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # emit() on. See the note in File::SOPS::Format::JSON. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 82 | our @CARP_NOT = qw( File::SOPS File::SOPS::Encrypted ); | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # THE COMMENT SLOT, and -- as in Format::ENV -- it is not a name but the PATH | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # sops authenticates a comment under. What it is NOT is the same path. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # Measured against sops 3.13.3, one comment in every position an ini document | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # has: every comment leaf decrypts under the AAD of its SECTION and under | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # nothing else -- `db:` inside `[db]`, `api:` for one written above `[api]`, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # `DEFAULT:` for one above a key that sits outside any section. Never `db::`. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # That is Go's own rule and not an ini quirk: sops walks a branch with the | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # branch's path and a comment is an ITEM of the branch whose key is a Comment | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # struct, so it contributes NO path component, where an ordinary key does. In | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # a dotenv document the branch is the document root, whose path is empty, and | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # `_path_to_aad([''])` spells that root as `:` -- which is why Format::ENV can | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # keep its comments under a real, empty KEY and still produce the AAD sops | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # wrote. Here the branch is a section and its path is NOT empty, so the same | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # spelling would produce `db::`. Measured, that is the AAD of a genuine empty | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # key nested in a mapping, and the leaf does not decrypt. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # So this key is reserved AND the walk in File::SOPS knows it: a `''` key | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # holding a sequence of comments contributes no path component when the path | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # is already non-empty. Nothing else in an ini document can collide with it, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # because sops refuses an empty key name outright (exit 2, `empty key name`). | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # Spelled here as well as in File::SOPS ($COMMENT_BUCKET_KEY, which carries the | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # walk rule) because this module is loaded FROM there and cannot read it at | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # compile time. t/61 pins that the two are the same string. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 82 | our $COMMENT_KEY = ''; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # The section the metadata lives in, and the one section name a data document | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # may not use. sops refuses one at exit 203, with the message it gives a YAML | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # document with a top-level `sops` key. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 82 | our $METADATA_SECTION = 'sops'; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Where a value outside any section goes. go-ini's own name for the implicit | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # first section; sops shows it as `"DEFAULT": {}` in --output-type json and | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # authenticates such a value under `DEFAULT:<key>:`, measured. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 82 | our $DEFAULT_SECTION = 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # ONE Flat object for the whole handler. The prefix is EMPTY here, where | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # Format::ENV's is `sops_`: the flat scheme is the same one (docs/adr/0022), | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # and an ini document separates the metadata by putting it in a section of its | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # own instead of by prefixing every key. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 82 | my $FLAT = File::SOPS::Metadata::Flat->new(prefix => ''); | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 82 | my $ORDERED = 'File::SOPS::Format::ENV::Ordered'; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | # go-ini reads BOTH of these as the key/value delimiter, and takes the first | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # one it finds -- measured: `host: localhost` is the key `host`, and `k := v` | ||||||||||||||||||||||||||||||||||||||||||
| 72 | # is the key `k` with the value `= v`. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 82 | my $DELIMITERS = '=:'; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # THE ONE LINE SCANNER | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # parse() and parse_in_document_order() both go through it, deliberately: the | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # second has to classify every line exactly as the first does or the shapes it | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # returns cannot line up with the tree the MAC walk holds (docs/adr/0036, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # condition 1). A second copy of "what is a section, what is a comment, where | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # does a value end" is how those two drift. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # It is go-ini's grammar, measured rather than read out of the source: the two | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # delimiters, the continuation backslash, the inline comment, the three quote | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # forms, and the fact that a comment block attaches to whatever comes NEXT. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 189 | ▶ | sub _scan { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scan: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 189 | my ($class, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 189 | my @lines = split /\n/, $content, -1; | |||||||||||||||||||||||||||||||||||||||||
| 94 | # split with -1 keeps the empty field a trailing newline produces; it is | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # not a line of the document and would only be skipped anyway. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 189 | ▶ | pop @lines if @lines && $lines[-1] eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@lines and $lines[-1] eq ""
MC/DC0%0/2
@lines and $lines[-1] eq ""
@lines
$lines[-1] eq ""
Truth table33%1/3
A and B
A= @linesB= $lines[-1] eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 189 | my @items; | |||||||||||||||||||||||||||||||||||||||||
| 99 | my @comment; # raw comment lines, waiting for a node | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 189 | my $section = $DEFAULT_SECTION; # what a key belongs to before any header | |||||||||||||||||||||||||||||||||||||||||
| 101 | 189 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 189 | ▶ | while ($i <= $#lines) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 588 | my $lineno = $i + 1; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 588 | my $line = $lines[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 588 | $line =~ s/\r\z//; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 588 | $line =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 588 | $line =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 588 | ▶ | if (!length $line) { $i++; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 550 | ▶ | if ($line =~ /\A[;#]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 31 | push @comment, $line; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 31 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 31 | next; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 519 | ▶ | if (substr($line, 0, 1) eq '[') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | # The LAST ']' closes it, which is go-ini's rule and not a typo. | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 85 | my $close = rindex($line, ']'); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 85 | ▶ | croak "line $lineno: a section header that is never closed. sops " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | . "refuses the same line with 'unclosed section'" | ||||||||||||||||||||||||||||||||||||||||||
| 123 | if $close < 0; | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 85 | $section = substr($line, 1, $close - 1); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 85 | ▶ | croak "line $lineno: a section header with an EMPTY name. sops " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | . "refuses the same line with 'empty section name'" | ||||||||||||||||||||||||||||||||||||||||||
| 128 | unless length $section; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 84 | push @items, { kind => 'section', name => $section, line => $lineno }; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 84 | ▶ | push @items, { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | kind => 'comment', | ||||||||||||||||||||||||||||||||||||||||||
| 133 | section => $section, | ||||||||||||||||||||||||||||||||||||||||||
| 134 | lines => [ splice @comment ], | ||||||||||||||||||||||||||||||||||||||||||
| 135 | line => $lineno, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } if @comment; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 84 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 84 | next; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 434 | my ($key, $rest) = _read_key($line, $lineno); | |||||||||||||||||||||||||||||||||||||||||
| 143 | # $i is advanced by the reader for a continued or multi-line value. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 295 | my ($value, $inline) = _read_value(\@lines, \$i, $rest); | |||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | # An inline comment goes into the SAME key's comment block, after any | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # lines above it: go-ini reads the value before it assigns the block, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # so `port = 5432 ; note` puts `note` on `port` and not on the next | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # key. Measured -- the leaf comes back ABOVE the value it was written | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # beside, which is why the round trip is not position-preserving. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 295 | ▶ | push @comment, $inline if defined $inline && length $inline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $inline and length $inline
MC/DC50%1/2
defined $inline and length $inline
defined $inline
length $inline
Truth table66%2/3
A and B
A= defined $inlineB= length $inline
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 295 | ▶ | push @items, { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | kind => 'comment', | ||||||||||||||||||||||||||||||||||||||||||
| 155 | section => $section, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | lines => [ splice @comment ], | ||||||||||||||||||||||||||||||||||||||||||
| 157 | line => $lineno, | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } if @comment; | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 295 | push @items, { | |||||||||||||||||||||||||||||||||||||||||
| 161 | kind => 'pair', | ||||||||||||||||||||||||||||||||||||||||||
| 162 | section => $section, | ||||||||||||||||||||||||||||||||||||||||||
| 163 | key => $key, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | value => $value, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | line => $lineno, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 295 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | # A comment block at the end of the file is DROPPED. There is no node left | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # for it to attach to, and sops drops it too (measured). | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 49 | return \@items; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | # go-ini's readKey. The key may be quoted -- with backticks when it holds a | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # quote or a delimiter, with `"""` when it holds a backtick -- which is what | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # the writer below produces for the same keys. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 456 | ▶ | sub _read_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_key: called CC=10 SCAR=23.5
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 456 | my ($line, $lineno) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 456 | my $quote; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 456 | ▶ | if (substr($line, 0, 1) eq '"') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | $quote = (length($line) > 6 && substr($line, 0, 3) eq '"""') ? '"""' : '"'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $line > 6 and substr($line, 0, 3) eq "\"\"\""
MC/DC0%0/2
length $line > 6 and substr($line, 0, 3) eq "\"\"\""
length $line > 6
substr($line, 0, 3) eq "\"\"\""
Truth table33%1/3
A and B
A= length $line > 6B= substr($line, 0, 3) eq "\"\"\""
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | elsif (substr($line, 0, 1) eq '`') { | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | $quote = '`'; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 456 | ▶ | if (defined $quote) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | my $start = length $quote; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | my $pos = index($line, $quote, $start); | |||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | ▶ | croak "line $lineno: a quoted key that is never closed. sops refuses " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | . "the same line with 'missing closing key quote'" | ||||||||||||||||||||||||||||||||||||||||||
| 196 | if $pos < 0; | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | my $after = $pos + $start; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | my $d = _index_any(substr($line, $after), $DELIMITERS); | |||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | croak "line $lineno: no '=' or ':' after the quoted key. sops refuses " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | . "the same line with 'key-value delimiter not found'" | ||||||||||||||||||||||||||||||||||||||||||
| 202 | if $d < 0; | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | my $key = substr($line, $start, $pos - $start); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | $key =~ s/\A\s+//; $key =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | return ($key, substr($line, $after + $d + 1)); | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 455 | my $end = _index_any($line, $DELIMITERS); | |||||||||||||||||||||||||||||||||||||||||
| 210 | # The line is NOT quoted into the message: an unreadable line in an | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # encrypted document is still the document's content. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 455 | ▶ | croak "line $lineno is neither a comment, a section header nor a " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | . "'key = value' pair: it holds no '=' and no ':'. sops refuses the " | ||||||||||||||||||||||||||||||||||||||||||
| 214 | . "same line with 'key-value delimiter not found'" | ||||||||||||||||||||||||||||||||||||||||||
| 215 | if $end < 0; | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 317 | ▶ | croak "line $lineno has an EMPTY key name. sops refuses the same line " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | . "with 'empty key name'" | ||||||||||||||||||||||||||||||||||||||||||
| 218 | if $end == 0; | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 316 | my $key = substr($line, 0, $end); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 316 | $key =~ s/\A\s+//; $key =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 316 | return ($key, substr($line, $end + 1)); | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | # go-ini's readValue, and the reason it is here in full rather than as | ||||||||||||||||||||||||||||||||||||||||||
| 226 | # `everything after the =`: an UNENCRYPTED leaf reaches the file as its own | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # text, so a reader that undoes less than sops's does hands the digest a | ||||||||||||||||||||||||||||||||||||||||||
| 228 | # different plaintext and the document fails its own MAC. | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # Returns the value and the inline comment it swallowed, if any. | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 317 | ▶ | sub _read_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_value: called CC=17 SCAR=28.5
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 317 | my ($lines, $idx, $rest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 317 | my $line = $rest; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 317 | $line =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 317 | ▶ | return ('', undef) unless length $line; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 291 | my $quote; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 291 | ▶ | if (length($line) > 3 && substr($line, 0, 3) eq '"""') { $quote = '"""' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
length $line > 3 and substr($line, 0, 3) eq "\"\"\""
MC/DC100%2/2
length $line > 3 and substr($line, 0, 3) eq "\"\"\""
length $line > 3
substr($line, 0, 3) eq "\"\"\""
Truth table100%3/3
A and B
A= length $line > 3B= substr($line, 0, 3) eq "\"\"\""
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 5 | elsif (substr($line, 0, 1) eq '`') { $quote = '`' } | |||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 291 | ▶ | if (defined $quote) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 11 | my $start = length $quote; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 11 | my $pos = index($line, $quote, $start); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 11 | ▶ | return (substr($line, $start, $pos - $start), undef) if $pos >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # A quote left open runs on to the line that closes it. This is how a | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # value holding a newline is written and the only way it is readable. | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 3 | my @acc = (substr($line, $start)); | |||||||||||||||||||||||||||||||||||||||||
| 250 | 3 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | $$idx++; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | ▶ | return (join("\n", @acc), undef) if $$idx > $#$lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 3 | my $next = $lines->[$$idx]; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 3 | $next =~ s/\r\z//; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 3 | my $p = index($next, $quote); | |||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | ▶ | if ($p >= 0) { push @acc, substr($next, 0, $p); last } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | push @acc, $next; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 3 | return (join("\n", @acc), undef); | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 280 | $line =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | # A trailing backslash continues the value onto the next line, and the | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # continued value is returned WITHOUT the inline-comment strip and without | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # the unquoting below -- go-ini returns from there. Measured: `a = one\` | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # followed by `b = two` is the single value `oneb = two`. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 280 | ▶ | if (length($line) && substr($line, -1) eq "\\") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $line and substr($line, -1) eq "\\"
MC/DC50%1/2
length $line and substr($line, -1) eq "\\"
length $line
substr($line, -1) eq "\\"
Truth table66%2/3
A and B
A= length $lineB= substr($line, -1) eq "\\"
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 1 | my $value = substr($line, 0, -1); | |||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | $$idx++; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | ▶ | last if $$idx > $#$lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | my $next = $lines->[$$idx]; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 1 | $next =~ s/\r\z//; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | $next =~ s/\A\s+//; $next =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | ▶ | last unless length $next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | $value .= $next; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | ▶ | last unless substr($value, -1) eq "\\"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $value = substr($value, 0, -1); | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 1 | return ($value, undef); | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 279 | my $inline; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 279 | my $at = _index_any($line, '#;'); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 279 | ▶ | if ($at >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 3 | $inline = substr($line, $at); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 3 | $line = substr($line, 0, $at); | |||||||||||||||||||||||||||||||||||||||||
| 289 | 3 | $line =~ s/\A\s+//; $line =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 279 | ▶ | $line = substr($line, 1, length($line) - 2) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_has_surrounded_quote($line, "'") or _has_surrounded_quote($line, "\"")
MC/DC100%2/2
_has_surrounded_quote($line, "'") or _has_surrounded_quote($line, "\"")
_has_surrounded_quote($line, "'")
_has_surrounded_quote($line, "\"")
Truth table100%3/3
A or B
A= _has_surrounded_quote($line, "'")B= _has_surrounded_quote($line, "\"")
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | if _has_surrounded_quote($line, "'") || _has_surrounded_quote($line, '"'); | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 279 | return ($line, $inline); | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | # Go's strings.IndexAny over a set of single bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 735 | ▶ | sub _index_any { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_index_any: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 735 | my ($string, $set) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 735 | my $best = -1; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 735 | for my $char (split //, $set) { | |||||||||||||||||||||||||||||||||||||||||
| 303 | 1470 | my $at = index($string, $char); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 1470 | ▶ | $best = $at if $at >= 0 && ($best < 0 || $at < $best); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$at >= 0 and $best < 0 || $at < $best
$best < 0 or $at < $best
MC/DC100%3/3
$at >= 0 and $best < 0 || $at < $best
$at >= 0
$best < 0
$at < $best
Truth table100%4/4
A and B or C
A= $at >= 0B= $best < 0C= $at < $best
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 735 | return $best; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # Go's hasSurroundedQuote, and the third clause is the whole of why `"a"b"` | ||||||||||||||||||||||||||||||||||||||||||
| 310 | # survives where `"quoted"` does not: the quote may not appear INSIDE. | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 556 | ▶ | sub _has_surrounded_quote { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_has_surrounded_quote: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 556 | my ($string, $quote) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 556 | ▶ | return 0 unless length($string) >= 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 476 | ▶ | return 0 unless substr($string, 0, 1) eq $quote; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 10 | ▶ | return 0 unless substr($string, -1) eq $quote; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 10 | ▶ | return 0 unless index($string, $quote, 1) > -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 10 | ▶ | return 0 unless index(substr($string, 1, length($string) - 2), $quote) == -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 10 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | # sops's stripCommentChar over go-ini's accumulated block. Only the FIRST | ||||||||||||||||||||||||||||||||||||||||||
| 322 | # line's marker goes -- measured, the leaf's plaintext keeps every later line's | ||||||||||||||||||||||||||||||||||||||||||
| 323 | # `;` or `#` exactly as the file had it, which is why a `#` comment can come | ||||||||||||||||||||||||||||||||||||||||||
| 324 | # back out of a `;` block unchanged. | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 29 | ▶ | sub _comment_text_from_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comment_text_from_lines: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 29 | my ($lines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 29 | my $text = join "\n", @$lines; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 29 | $text =~ s/\A\s+//; $text =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | 29 | ▶ | if (substr($text, 0, 1) eq ';') { $text =~ s/\A[; ]+// } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 2 | elsif (substr($text, 0, 1) eq '#') { $text =~ s/\A[# ]+// } | |||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | 29 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | # The wire is UTF-8 bytes and this library's boundary is characters, so every | ||||||||||||||||||||||||||||||||||||||||||
| 338 | # section name, key, value and comment crossing in gets decoded exactly once -- | ||||||||||||||||||||||||||||||||||||||||||
| 339 | # the crossing YAML::XS and Cpanel::JSON::XS(utf8) do for the other two | ||||||||||||||||||||||||||||||||||||||||||
| 340 | # formats, and the one the emitters below undo with an unconditional encode | ||||||||||||||||||||||||||||||||||||||||||
| 341 | # (docs/adr/0003). | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 405 | ▶ | sub _characters { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_characters: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 405 | my ($bytes, $what) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 405 | my $chars = $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 346 | 405 | ▶ | croak "$what is not valid UTF-8, and an ini document has no other way to " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | . "say what its bytes mean. Both nested parsers here refuse the same " | ||||||||||||||||||||||||||||||||||||||||||
| 348 | . "document; sops, whose strings are byte slices, reads it" | ||||||||||||||||||||||||||||||||||||||||||
| 349 | unless utf8::decode($chars); | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 405 | return $chars; | |||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | # The bytes a key or a section name goes onto the wire as. The value's half of | ||||||||||||||||||||||||||||||||||||||||||
| 355 | # this is Encrypted::value_to_bytes, which encodes the same way and for the | ||||||||||||||||||||||||||||||||||||||||||
| 356 | # same reason; a key has no type ladder to go through, so it is the encode | ||||||||||||||||||||||||||||||||||||||||||
| 357 | # alone. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 105 | ▶ | sub _key_bytes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_key_bytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 105 | my ($key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 360 | 105 | my $bytes = $key; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 105 | utf8::encode($bytes); # unconditional: no-op for ASCII, correct for the rest | |||||||||||||||||||||||||||||||||||||||||
| 362 | 105 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 48 | ▶ | sub _value_bytes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_value_bytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 48 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 48 | my $bytes = $value; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 48 | utf8::encode($bytes); | |||||||||||||||||||||||||||||||||||||||||
| 369 | 48 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 38 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=15 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 38 | my ($class, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 38 | ▶ | croak "content required" unless defined $content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | 38 | my (%data, %flat, %header_seen); | |||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 38 | for my $item (@{ $class->_scan($content) }) { | |||||||||||||||||||||||||||||||||||||||||
| 379 | 240 | my $kind = $item->{kind}; | |||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 240 | ▶ | if ($kind eq 'section') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | my $name = _characters($item->{name}, | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 56 | "the section name on line $item->{line}"); | |||||||||||||||||||||||||||||||||||||||||
| 384 | 56 | ▶ | next if $name eq $METADATA_SECTION; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | croak "the section '[$name]' is opened twice (line $item->{line}). " | ||||||||||||||||||||||||||||||||||||||||||
| 387 | . "sops keeps both as separate branches -- its tree is an " | ||||||||||||||||||||||||||||||||||||||||||
| 388 | . "ordered list, not a map -- and a Perl hash cannot, so this " | ||||||||||||||||||||||||||||||||||||||||||
| 389 | . "document is refused rather than silently merging them" | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 42 | ▶ | if $header_seen{$name}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 41 | ▶ | $data{$name} ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data{$name} ||= {}
MC/DC0%0/1
$data{$name} ||= {}
$data{$name}
Truth table50%1/2
A
A= $data{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 41 | next; | |||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | 184 | ▶ | next if $item->{section} eq $METADATA_SECTION && $kind eq 'comment'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$item->{"section"} eq $File::SOPS::Format::INI::METADATA_SECTION and $kind eq "comment"
MC/DC0%0/2
$item->{"section"} eq $File::SOPS::Format::INI::METADATA_SECTION and $kind eq "comment"
$item->{"section"} eq $File::SOPS::Format::INI::METADATA_SECTION
$kind eq "comment"
Truth table66%2/3
A and B
A= $item->{"section"} eq $File::SOPS::Format::INI::METADATA_SECTIONB= $kind eq "comment"
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | 184 | ▶ | if ($kind eq 'comment') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | 22 | my $text = _comment_text_from_lines($item->{lines}); | |||||||||||||||||||||||||||||||||||||||||
| 400 | # sops writes no leaf for a comment with no text: a bare `#` is | ||||||||||||||||||||||||||||||||||||||||||
| 401 | # dropped entirely, measured, and File::SOPS::Comment refuses an | ||||||||||||||||||||||||||||||||||||||||||
| 402 | # empty text for the same reason. | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 22 | ▶ | next unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | my $section = _characters($item->{section}, | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 21 | "the section name for the comment on line $item->{line}"); | |||||||||||||||||||||||||||||||||||||||||
| 407 | 21 | push @{ $data{$section}{$COMMENT_KEY} }, | |||||||||||||||||||||||||||||||||||||||||
| 408 | 21 | _comment_leaf($text, $item->{line}); | |||||||||||||||||||||||||||||||||||||||||
| 409 | 21 | next; | |||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 162 | ▶ | if ($item->{section} eq $METADATA_SECTION) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | croak "the flat metadata key '$item->{key}' is set twice " | ||||||||||||||||||||||||||||||||||||||||||
| 414 | . "(line $item->{line})" | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 84 | ▶ | if exists $flat{ $item->{key} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | 84 | $flat{ $item->{key} } = $item->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 417 | 84 | next; | |||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | my $section = _characters($item->{section}, | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 78 | "the section name on line $item->{line}"); | |||||||||||||||||||||||||||||||||||||||||
| 422 | 78 | my $key = _characters($item->{key}, "the key on line $item->{line}"); | |||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | croak "the key '$key' in section '[$section]' is set twice " | ||||||||||||||||||||||||||||||||||||||||||
| 425 | . "(line $item->{line}). sops keeps the LAST one and drops the " | ||||||||||||||||||||||||||||||||||||||||||
| 426 | . "other without a word; this document is refused rather than " | ||||||||||||||||||||||||||||||||||||||||||
| 427 | . "losing a line somebody wrote" | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 78 | ▶ | if exists $data{$section}{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | $data{$section}{$key} = _characters($item->{value}, | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 77 | "the value of '$key' on line $item->{line}"); | |||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | my $metadata = %flat | ||||||||||||||||||||||||||||||||||||||||||
| 435 | ? File::SOPS::Metadata->from_hash($FLAT->unflatten({ | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 33 | ▶ | map { $_ => _characters($FLAT->unescape_value($flat{$_}), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | "the metadata key '$_'") } | ||||||||||||||||||||||||||||||||||||||||||
| 438 | keys %flat | ||||||||||||||||||||||||||||||||||||||||||
| 439 | })) | ||||||||||||||||||||||||||||||||||||||||||
| 440 | : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 33 | return (\%data, $metadata); | |||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | # ENCRYPTED or PLAINTEXT, decided by the type LABEL and not by the position -- | ||||||||||||||||||||||||||||||||||||||||||
| 446 | # Format::ENV's rule, for the same reason: the same method has to read a | ||||||||||||||||||||||||||||||||||||||||||
| 447 | # plaintext .ini and an encrypted one. | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 21 | ▶ | sub _comment_leaf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comment_leaf: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | 21 | my ($text, $lineno) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | 21 | ▶ | return $text | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
"File::SOPS::Encrypted"->encrypted_type($text) // ""
MC/DC100%1/1
"File::SOPS::Encrypted"->encrypted_type($text) // ""
"File::SOPS::Encrypted"->encrypted_type($text)
Truth table100%2/2
A
A= "File::SOPS::Encrypted"->encrypted_type($text)
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | if (File::SOPS::Encrypted->encrypted_type($text) // '') eq 'comment'; | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 11 | return File::SOPS::Comment->new( | |||||||||||||||||||||||||||||||||||||||||
| 455 | text => _characters($text, "the comment on line $lineno")); | ||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 10 | ▶ | sub parse_in_document_order { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_in_document_order: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 10 | my ($class, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 10 | ▶ | return unless defined $content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | # Declining is safe, guessing is not: a document this handler cannot scan | ||||||||||||||||||||||||||||||||||||||||||
| 464 | # must not turn a MAC check into an error the caller reads as corruption. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | # parse() will raise the real message a moment later if the document is | ||||||||||||||||||||||||||||||||||||||||||
| 466 | # being read at all. | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 10 | ▶ | my $items = eval { $class->_scan($content) } or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 10 | my %doc; | |||||||||||||||||||||||||||||||||||||||||
| 470 | 10 | tie %doc, $ORDERED; | |||||||||||||||||||||||||||||||||||||||||
| 471 | 10 | my %header_seen; | |||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | 10 | for my $item (@$items) { | |||||||||||||||||||||||||||||||||||||||||
| 474 | my $section = $item->{kind} eq 'section' ? $item->{name} | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 94 | ▶ | : $item->{section}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 94 | utf8::decode($section); | |||||||||||||||||||||||||||||||||||||||||
| 477 | 94 | ▶ | next if $section eq $METADATA_SECTION; # the handler drops it, condition 3 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 45 | ▶ | if ($item->{kind} eq 'section') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 12 | ▶ | return if $header_seen{$section}++; # a document parse() refuses | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | 45 | ▶ | unless (exists $doc{$section}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 484 | 13 | my %branch; | |||||||||||||||||||||||||||||||||||||||||
| 485 | 13 | tie %branch, $ORDERED; | |||||||||||||||||||||||||||||||||||||||||
| 486 | 13 | $doc{$section} = \%branch; | |||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||
| 488 | 45 | ▶ | next if $item->{kind} eq 'section'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | 33 | my $branch = $doc{$section}; | |||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | 33 | ▶ | if ($item->{kind} eq 'comment') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | 7 | my $text = _comment_text_from_lines($item->{lines}); | |||||||||||||||||||||||||||||||||||||||||
| 494 | 7 | ▶ | next unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | # The bucket takes the position of the FIRST comment, which is | ||||||||||||||||||||||||||||||||||||||||||
| 496 | # where parse() would put it in a hash that had positions. Only the | ||||||||||||||||||||||||||||||||||||||||||
| 497 | # shape is read, so what matters is that the sequence is here and | ||||||||||||||||||||||||||||||||||||||||||
| 498 | # is as long as the tree's. | ||||||||||||||||||||||||||||||||||||||||||
| 499 | $branch->{$COMMENT_KEY} = [] | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 7 | ▶ | unless ref $branch->{$COMMENT_KEY} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 7 | push @{ $branch->{$COMMENT_KEY} }, undef; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 7 | next; | |||||||||||||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | 26 | my $key = $item->{key}; | |||||||||||||||||||||||||||||||||||||||||
| 506 | 26 | utf8::decode($key); | |||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 26 | ▶ | return if exists $branch->{$key}; # a document parse() refuses | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | 25 | $branch->{$key} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | 9 | return \%doc; | |||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | 9 | ▶ | sub serialize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 9 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 519 | 9 | ▶ | my $data = $args{data} // croak "data required"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"data"} // croak("data required")
MC/DC0%0/1
$args{"data"} // croak("data required")
$args{"data"}
Truth table50%1/2
A
A= $args{"data"}
| |||||||||||||||||||||||||||||||||||||||||||
| 520 | 9 | ▶ | my $metadata = $args{metadata} // croak "metadata required"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"metadata"} // croak("metadata required")
MC/DC0%0/1
$args{"metadata"} // croak("metadata required")
$args{"metadata"}
Truth table50%1/2
A
A= $args{"metadata"}
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | # emit() refuses a section named `sops`, so the section written below | ||||||||||||||||||||||||||||||||||||||||||
| 523 | # cannot be shadowed by one -- the same job Format::JSON's `sops` refusal | ||||||||||||||||||||||||||||||||||||||||||
| 524 | # does, moved to where this format's collision is. | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 9 | my $out = $class->emit($data); | |||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | # flatten() has already escaped the values, in the order sops writes them: | ||||||||||||||||||||||||||||||||||||||||||
| 528 | # maps sorted, lists ascending. The section goes LAST, where sops puts it; | ||||||||||||||||||||||||||||||||||||||||||
| 529 | # line order is not load-bearing (measured: a file with its metadata lines | ||||||||||||||||||||||||||||||||||||||||||
| 530 | # reversed still decrypts), and the metadata is excluded from the MAC | ||||||||||||||||||||||||||||||||||||||||||
| 531 | # structurally. | ||||||||||||||||||||||||||||||||||||||||||
| 532 | 8 | ▶ | $out .= "\n" if length $out; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 8 | $out .= '[' . $METADATA_SECTION . "]\n"; | |||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | 8 | my @pairs = map { [ _key_bytes($_->[0]), _value_bytes($_->[1]) ] } | |||||||||||||||||||||||||||||||||||||||||
| 536 | $FLAT->flatten($metadata->to_hash); | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 8 | $out .= _aligned_lines(\@pairs); | |||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | 8 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | # The one place this distribution turns a Perl tree into an ini document. | ||||||||||||||||||||||||||||||||||||||||||
| 544 | # serialize() is this plus the metadata section, and File::SOPS::_serialize_ | ||||||||||||||||||||||||||||||||||||||||||
| 545 | # plaintext -- what decrypt_file writes and what edit hands the editor -- is | ||||||||||||||||||||||||||||||||||||||||||
| 546 | # this on its own. | ||||||||||||||||||||||||||||||||||||||||||
| 547 | # | ||||||||||||||||||||||||||||||||||||||||||
| 548 | # So this is ON THE WIRE PATH and is not a plaintext formatting knob. In | ||||||||||||||||||||||||||||||||||||||||||
| 549 | # particular the sorted order below is not a diff-readability choice: the MAC's | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # encrypt side hashes leaves in `sort keys` order at every level, and that is | ||||||||||||||||||||||||||||||||||||||||||
| 551 | # the document's own order only because this loop sorts. Measured on a file | ||||||||||||||||||||||||||||||||||||||||||
| 552 | # sops wrote, whose order is the document's: sorting the data lines of one | ||||||||||||||||||||||||||||||||||||||||||
| 553 | # section makes `sops -d` fail at exit 51. | ||||||||||||||||||||||||||||||||||||||||||
| 554 | 29 | ▶ | sub emit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
emit: called CC=15 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 555 | 29 | my ($class, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 556 | 29 | ▶ | croak "data required" unless defined $data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 29 | ▶ | croak "an ini document is a mapping of sections to their keys, so emit " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 558 | . "needs a HashRef, not " . (ref($data) ? ref($data) . " reference" | ||||||||||||||||||||||||||||||||||||||||||
| 559 | : "a scalar") | ||||||||||||||||||||||||||||||||||||||||||
| 560 | unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | 29 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||
| 563 | 29 | my $first = 1; | |||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | 29 | for my $name (sort keys %$data) { | |||||||||||||||||||||||||||||||||||||||||
| 566 | 32 | ▶ | croak "[$name]: '$METADATA_SECTION' is the section the SOPS metadata " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | . "goes in, so this section would be read back as metadata instead " | ||||||||||||||||||||||||||||||||||||||||||
| 568 | . "of as data. sops refuses such a document too (exit 203). Rename " | ||||||||||||||||||||||||||||||||||||||||||
| 569 | . "the section" | ||||||||||||||||||||||||||||||||||||||||||
| 570 | if $name eq $METADATA_SECTION; | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | 30 | ▶ | croak "a section name cannot be empty; sops refuses such a header with " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | . "'empty section name'" | ||||||||||||||||||||||||||||||||||||||||||
| 574 | unless length $name; | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | 30 | my $section = $data->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | # A top-level SCALAR is what sops itself refuses -- `Section values | ||||||||||||||||||||||||||||||||||||||||||
| 579 | # should always be TreeBranches`, exit 4 -- and an ArrayRef has no | ||||||||||||||||||||||||||||||||||||||||||
| 580 | # spelling here either. | ||||||||||||||||||||||||||||||||||||||||||
| 581 | 30 | ▶ | croak "[$name]: an ini document is exactly two levels deep, so every " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 582 | . "top-level entry is a section and has to be a HashRef, not " | ||||||||||||||||||||||||||||||||||||||||||
| 583 | . (ref($section) ? lc(ref $section) . " reference" | ||||||||||||||||||||||||||||||||||||||||||
| 584 | : defined $section ? "a scalar" : "an undef") | ||||||||||||||||||||||||||||||||||||||||||
| 585 | . ". sops refuses a top-level scalar with 'Section values should " | ||||||||||||||||||||||||||||||||||||||||||
| 586 | . "always be TreeBranches'" | ||||||||||||||||||||||||||||||||||||||||||
| 587 | unless ref $section eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | 29 | ▶ | $out .= "\n" unless $first; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 590 | 29 | $first = 0; | |||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | my @comments = exists $section->{$COMMENT_KEY} | ||||||||||||||||||||||||||||||||||||||||||
| 593 | 29 | ▶ | ? $class->_comment_blocks($section->{$COMMENT_KEY}, $name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 594 | : (); | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | 28 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||
| 597 | 28 | for my $key (sort grep { $_ ne $COMMENT_KEY } keys %$section) { | |||||||||||||||||||||||||||||||||||||||||
| 598 | push @pairs, [ | ||||||||||||||||||||||||||||||||||||||||||
| 599 | _key_bytes($key), | ||||||||||||||||||||||||||||||||||||||||||
| 600 | 35 | _leaf_text($section->{$key}, $name, $key), | |||||||||||||||||||||||||||||||||||||||||
| 601 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | # ONE comment leaf per NODE, and the nodes are the header plus the | ||||||||||||||||||||||||||||||||||||||||||
| 605 | # keys. Not a layout preference: consecutive comment LINES are one | ||||||||||||||||||||||||||||||||||||||||||
| 606 | # leaf on the way back in, so two `; ENC[...]` lines written next to | ||||||||||||||||||||||||||||||||||||||||||
| 607 | # each other come back as a single comment holding both tokens as | ||||||||||||||||||||||||||||||||||||||||||
| 608 | # text. sops never writes that because it keeps each comment where the | ||||||||||||||||||||||||||||||||||||||||||
| 609 | # document had it; this emitter has no positions, so it spreads them. | ||||||||||||||||||||||||||||||||||||||||||
| 610 | # | ||||||||||||||||||||||||||||||||||||||||||
| 611 | # The capacity is exactly what a document can hold: a comment block | ||||||||||||||||||||||||||||||||||||||||||
| 612 | # needs a following node to attach to, so a section with K keys can | ||||||||||||||||||||||||||||||||||||||||||
| 613 | # carry at most K+1 of them, and this writes K+1. | ||||||||||||||||||||||||||||||||||||||||||
| 614 | 23 | ▶ | croak "[$name]: " . scalar(@comments) . " comment blocks in a section " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 615 | . "with " . scalar(@pairs) . " keys. A comment attaches to the " | ||||||||||||||||||||||||||||||||||||||||||
| 616 | . "node that FOLLOWS it and the nodes here are the section header " | ||||||||||||||||||||||||||||||||||||||||||
| 617 | . "and its keys, so at most " . (@pairs + 1) . " can be written -- " | ||||||||||||||||||||||||||||||||||||||||||
| 618 | . "any more would end up on consecutive lines, which this format " | ||||||||||||||||||||||||||||||||||||||||||
| 619 | . "reads back as ONE comment. Join them into one comment, or add " | ||||||||||||||||||||||||||||||||||||||||||
| 620 | . "the keys they belong to" | ||||||||||||||||||||||||||||||||||||||||||
| 621 | if @comments > @pairs + 1; | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | 22 | ▶ | $out .= shift @comments if @comments; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 624 | 22 | $out .= '[' . _key_bytes($name) . "]\n"; | |||||||||||||||||||||||||||||||||||||||||
| 625 | 22 | $out .= _aligned_lines(\@pairs, \@comments); | |||||||||||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | 19 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | # go-ini's writer pads every key in a section to the longest one IN THAT | ||||||||||||||||||||||||||||||||||||||||||
| 632 | # SECTION, counted in BYTES and after the key's own quoting. Measured cosmetic | ||||||||||||||||||||||||||||||||||||||||||
| 633 | # for the digest -- a sops-written file with the padding stripped still reads | ||||||||||||||||||||||||||||||||||||||||||
| 634 | # at exit 0 -- and reproduced because it is what makes a diff against a | ||||||||||||||||||||||||||||||||||||||||||
| 635 | # sops-written file readable, and our [sops] section byte-identical to its. | ||||||||||||||||||||||||||||||||||||||||||
| 636 | 30 | ▶ | sub _aligned_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_aligned_lines: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 637 | 30 | my ($pairs, $comments) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | 30 | my $align = 0; | |||||||||||||||||||||||||||||||||||||||||
| 640 | 30 | my @quoted; | |||||||||||||||||||||||||||||||||||||||||
| 641 | 30 | for my $pair (@$pairs) { | |||||||||||||||||||||||||||||||||||||||||
| 642 | 78 | my $key = _quote_key($pair->[0]); | |||||||||||||||||||||||||||||||||||||||||
| 643 | 78 | ▶ | $align = length $key if length $key > $align; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 644 | 78 | push @quoted, [ $key, $pair->[1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||
| 647 | 30 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||
| 648 | 30 | for my $pair (@quoted) { | |||||||||||||||||||||||||||||||||||||||||
| 649 | 78 | ▶ | $out .= shift @$comments if $comments && @$comments; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$comments and @$comments
MC/DC100%2/2
$comments and @$comments
$comments
@$comments
Truth table100%3/3
A and B
A= $commentsB= @$comments
| |||||||||||||||||||||||||||||||||||||||||||
| 650 | 78 | $out .= $pair->[0] . (' ' x ($align - length $pair->[0])) . ' = ' | |||||||||||||||||||||||||||||||||||||||||
| 651 | . $pair->[1] . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||
| 654 | 30 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||
| 657 | 10 | ▶ | sub _comment_blocks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comment_blocks: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 658 | 10 | my ($class, $bucket, $section) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||
| 660 | 10 | ▶ | croak "[$section]: the empty key is reserved for this section's comments " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 661 | . "and holds a sequence of them; a " | ||||||||||||||||||||||||||||||||||||||||||
| 662 | . (ref($bucket) ? lc(ref $bucket) . " reference" : "scalar") | ||||||||||||||||||||||||||||||||||||||||||
| 663 | . " here has no line to be written as. See the comment section of " | ||||||||||||||||||||||||||||||||||||||||||
| 664 | . "File::SOPS::Format::INI" | ||||||||||||||||||||||||||||||||||||||||||
| 665 | unless ref $bucket eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | 9 | return map { _comment_block(_comment_text($_, $section)) } @$bucket; | |||||||||||||||||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||
| 670 | 11 | ▶ | sub _comment_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comment_text: called CC=7 SCAR=23.4
| |||||||||||||||||||||||||||||||||||||||||||
| 671 | 11 | my ($comment, $section) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||
| 673 | # An encrypted comment is already the line's text. | ||||||||||||||||||||||||||||||||||||||||||
| 674 | 11 | ▶ | return $comment | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
not ref $comment and ("File::SOPS::Encrypted"->encrypted_type($comment) // "") eq "comment"
"File::SOPS::Encrypted"->encrypted_type($comment) // ""
MC/DC50%1/2
not ref $comment and ("File::SOPS::Encrypted"->encrypted_type($comment) // "") eq "comment"
not ref $comment
"File::SOPS::Encrypted"->encrypted_type($comment)
Truth table66%2/3
not(A) and B
A= not ref $commentB= "File::SOPS::Encrypted"->encrypted_type($comment)
| |||||||||||||||||||||||||||||||||||||||||||
| 675 | if !ref $comment | ||||||||||||||||||||||||||||||||||||||||||
| 676 | && (File::SOPS::Encrypted->encrypted_type($comment) // '') eq 'comment'; | ||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||
| 678 | 4 | ▶ | croak "[$section]: the empty key holds this section's comments, and " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 679 | . (ref($comment) ? "a " . lc(ref $comment) . " reference" | ||||||||||||||||||||||||||||||||||||||||||
| 680 | : defined $comment ? "a plain scalar" | ||||||||||||||||||||||||||||||||||||||||||
| 681 | : "an undef") | ||||||||||||||||||||||||||||||||||||||||||
| 682 | . " is not one. Put File::SOPS::Comment objects there, or leave the " | ||||||||||||||||||||||||||||||||||||||||||
| 683 | . "key out" | ||||||||||||||||||||||||||||||||||||||||||
| 684 | unless File::SOPS::Encrypted->is_comment($comment); | ||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||
| 686 | 4 | return File::SOPS::Encrypted->value_to_bytes($comment); | |||||||||||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||
| 689 | # go-ini's comment writer, line by line: a line that already carries a marker | ||||||||||||||||||||||||||||||||||||||||||
| 690 | # keeps it and is re-spaced, one that does not gets `; `. A multi-line comment | ||||||||||||||||||||||||||||||||||||||||||
| 691 | # leaf is therefore several lines again -- which is how it was read, since | ||||||||||||||||||||||||||||||||||||||||||
| 692 | # consecutive comment lines are ONE leaf. | ||||||||||||||||||||||||||||||||||||||||||
| 693 | 11 | ▶ | sub _comment_block { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_comment_block: called CC=3 SCAR=13.0
| |||||||||||||||||||||||||||||||||||||||||||
| 694 | 11 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||
| 696 | 11 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||
| 697 | 11 | for my $line (split /\n/, $text, -1) { | |||||||||||||||||||||||||||||||||||||||||
| 698 | 11 | ▶ | if ($line =~ /\A([;#])(.*)\z/s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 699 | 0 | my ($marker, $rest) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 700 | 0 | $rest =~ s/\A\s+//; $rest =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 701 | 0 | $out .= $marker . ' ' . $rest . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||||||||||||||||
| 703 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 704 | 11 | $out .= '; ' . $line . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 705 | } | ||||||||||||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||
| 708 | 11 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||||||||||||
| 711 | 35 | ▶ | sub _leaf_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_leaf_text: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 712 | 35 | my ($value, $section, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||
| 714 | # An ENCRYPTED leaf is an ENC[...] string by the time the emitter sees it. | ||||||||||||||||||||||||||||||||||||||||||
| 715 | # Its alphabet is base64 plus []:,= -- no `#`, no `;`, no backtick, no | ||||||||||||||||||||||||||||||||||||||||||
| 716 | # newline and no edge whitespace -- so the writer leaves it bare and the | ||||||||||||||||||||||||||||||||||||||||||
| 717 | # reader returns it unchanged. The guard below has nothing to catch on it, | ||||||||||||||||||||||||||||||||||||||||||
| 718 | # and refusing one would reject documents that work. | ||||||||||||||||||||||||||||||||||||||||||
| 719 | 35 | ▶ | return $value | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $value and "File::SOPS::Encrypted"->is_encrypted($value)
MC/DC100%2/2
not ref $value and "File::SOPS::Encrypted"->is_encrypted($value)
not ref $value
"File::SOPS::Encrypted"->is_encrypted($value)
Truth table100%3/3
not(A) and B
A= not ref $valueB= "File::SOPS::Encrypted"->is_encrypted($value)
| |||||||||||||||||||||||||||||||||||||||||||
| 720 | if !ref $value && File::SOPS::Encrypted->is_encrypted($value); | ||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||
| 722 | 23 | ▶ | croak "[$section] $key: cannot write a " . lc(ref $value) . " into an ini " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $value and not builtin::blessed($value)
MC/DC50%1/2
ref $value and not builtin::blessed($value)
ref $value
not builtin::blessed($value)
Truth table66%2/3
A and not(B)
A= ref $valueB= not builtin::blessed($value)
| |||||||||||||||||||||||||||||||||||||||||||
| 723 | . "document; the format is two levels deep and has no spelling for a " | ||||||||||||||||||||||||||||||||||||||||||
| 724 | . "nested value. sops does not refuse this one -- it writes a dump of " | ||||||||||||||||||||||||||||||||||||||||||
| 725 | . "the Go value, which is not a document any more" | ||||||||||||||||||||||||||||||||||||||||||
| 726 | if ref $value && !blessed($value); | ||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||
| 728 | 22 | ▶ | croak "[$section] $key: a comment cannot be a mapping value. In an ini " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 729 | . "document a comment is a line of its own, which this handler keeps " | ||||||||||||||||||||||||||||||||||||||||||
| 730 | . "under the section's empty key -- see the comment section of " | ||||||||||||||||||||||||||||||||||||||||||
| 731 | . "File::SOPS::Format::INI" | ||||||||||||||||||||||||||||||||||||||||||
| 732 | if File::SOPS::Encrypted->is_comment($value); | ||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||
| 734 | # docs/adr/0035: in an untyped store an unencrypted leaf is written as | ||||||||||||||||||||||||||||||||||||||||||
| 735 | # exactly the bytes the digest covers. ASKED of the single source of truth | ||||||||||||||||||||||||||||||||||||||||||
| 736 | # rather than re-derived, so the emitter and the digest cannot drift apart. | ||||||||||||||||||||||||||||||||||||||||||
| 737 | 22 | my $bytes = File::SOPS::Encrypted->value_to_bytes($value); | |||||||||||||||||||||||||||||||||||||||||
| 738 | 22 | my $quoted = _quote_value($bytes); | |||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||
| 740 | # docs/adr/0030's rule, in this format's spelling: ask the ROUND TRIP, not | ||||||||||||||||||||||||||||||||||||||||||
| 741 | # a character. go-ini's reader strips a matching pair of surrounding quotes | ||||||||||||||||||||||||||||||||||||||||||
| 742 | # its writer never wrote, and follows a trailing backslash onto the next | ||||||||||||||||||||||||||||||||||||||||||
| 743 | # line, so `"quoted"`, `'quoted'`, `""` and `"""x"""` are all values sops | ||||||||||||||||||||||||||||||||||||||||||
| 744 | # writes at exit 0 and then refuses to read (MAC mismatch, exit 51). | ||||||||||||||||||||||||||||||||||||||||||
| 745 | 22 | my $read = _reads_back($quoted); | |||||||||||||||||||||||||||||||||||||||||
| 746 | 22 | ▶ | croak "[$section] $key: cannot write this value into an ini document; the " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $read and $read eq $bytes
MC/DC50%1/2
defined $read and $read eq $bytes
defined $read
$read eq $bytes
Truth table66%2/3
A and B
A= defined $readB= $read eq $bytes
| |||||||||||||||||||||||||||||||||||||||||||
| 747 | . "format's quoting does not round-trip it -- written out it reads " | ||||||||||||||||||||||||||||||||||||||||||
| 748 | . "back as something else, so the document would say one thing and its " | ||||||||||||||||||||||||||||||||||||||||||
| 749 | . "own MAC another. sops writes such a file at exit 0 and then refuses " | ||||||||||||||||||||||||||||||||||||||||||
| 750 | . "to read it (MAC mismatch, exit 51)" | ||||||||||||||||||||||||||||||||||||||||||
| 751 | unless defined $read && $read eq $bytes; | ||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||
| 753 | 18 | return $quoted; | |||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||
| 756 | # The round trip itself: the quoted value is put on a line the way emit() puts | ||||||||||||||||||||||||||||||||||||||||||
| 757 | # it there, and the line is read back through THIS handler's own scanner. Not a | ||||||||||||||||||||||||||||||||||||||||||
| 758 | # test for a character -- a second spelling of the rule is how a guard and the | ||||||||||||||||||||||||||||||||||||||||||
| 759 | # thing it guards drift apart (docs/adr/0030). | ||||||||||||||||||||||||||||||||||||||||||
| 760 | # | ||||||||||||||||||||||||||||||||||||||||||
| 761 | # Returns undef where the line does not read back as one whole value: the | ||||||||||||||||||||||||||||||||||||||||||
| 762 | # reader swallowed part of it as an inline comment, or the value ran on past | ||||||||||||||||||||||||||||||||||||||||||
| 763 | # the lines it was written on. | ||||||||||||||||||||||||||||||||||||||||||
| 764 | 22 | ▶ | sub _reads_back { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reads_back: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||||||||||||||||
| 765 | 22 | my ($quoted) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||
| 767 | 22 | my @lines = split /\n/, 'k = ' . $quoted, -1; | |||||||||||||||||||||||||||||||||||||||||
| 768 | 22 | $lines[0] =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 769 | 22 | $lines[0] =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||
| 771 | 22 | my ($key, $rest) = eval { _read_key($lines[0], 0) }; | |||||||||||||||||||||||||||||||||||||||||
| 772 | 22 | ▶ | return undef unless defined $rest; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||
| 774 | 22 | my $index = 0; | |||||||||||||||||||||||||||||||||||||||||
| 775 | 22 | my ($read, $inline) = _read_value(\@lines, \$index, $rest); | |||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||
| 777 | 22 | ▶ | return undef if defined $inline && length $inline; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $inline and length $inline
MC/DC0%0/2
defined $inline and length $inline
defined $inline
length $inline
Truth table33%1/3
A and B
A= defined $inlineB= length $inline
| |||||||||||||||||||||||||||||||||||||||||||
| 778 | 22 | ▶ | return undef unless $index == $#lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 779 | 22 | return $read; | |||||||||||||||||||||||||||||||||||||||||
| 780 | } | ||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||
| 782 | # go-ini's writer, measured: a newline or a backtick takes the `"""` form, a | ||||||||||||||||||||||||||||||||||||||||||
| 783 | # `#` or a `;` takes backticks, edge whitespace takes double quotes, and | ||||||||||||||||||||||||||||||||||||||||||
| 784 | # everything else goes bare. | ||||||||||||||||||||||||||||||||||||||||||
| 785 | 22 | ▶ | sub _quote_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_quote_value: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 786 | 22 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||
| 788 | 22 | ▶ | return '"""' . $value . '"""' if $value =~ /[\n`]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 789 | 20 | ▶ | return '`' . $value . '`' if $value =~ /[#;]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 790 | 18 | ▶ | return '"' . $value . '"' if $value =~ /\A\s/ || $value =~ /\s\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value =~ /\A\s/ or $value =~ /\s\z/
MC/DC100%2/2
$value =~ /\A\s/ or $value =~ /\s\z/
$value =~ /\A\s/
$value =~ /\s\z/
Truth table100%3/3
A or B
A= $value =~ /\A\s/B= $value =~ /\s\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 791 | 16 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 792 | } | ||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | # The key's half of the same thing, and the order matters: a key holding a | ||||||||||||||||||||||||||||||||||||||||||
| 795 | # backtick takes `"""`, one holding a quote or a delimiter takes backticks. | ||||||||||||||||||||||||||||||||||||||||||
| 796 | 78 | ▶ | sub _quote_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_quote_key: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||||||
| 797 | 78 | my ($key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||
| 799 | 78 | ▶ | return '"""' . $key . '"""' if index($key, '`') >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 800 | 78 | ▶ | return '`' . $key . '`' if $key =~ /["\Q$DELIMITERS\E]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 801 | 78 | return $key; | |||||||||||||||||||||||||||||||||||||||||
| 802 | } | ||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||
| 805 | 141 | ▶ | sub detect_content { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detect_content: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 806 | 141 | my ($class, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 807 | 141 | ▶ | return 0 unless defined $content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||
| 809 | # BOTH halves of what an encrypted ini document is, because either alone is | ||||||||||||||||||||||||||||||||||||||||||
| 810 | # too weak: any line of `key: value` scans as an ini pair, so "it parses" | ||||||||||||||||||||||||||||||||||||||||||
| 811 | # says nothing, and a plaintext .ini has no metadata to find. | ||||||||||||||||||||||||||||||||||||||||||
| 812 | 141 | ▶ | my $items = eval { $class->_scan($content) } or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 813 | 4 | for my $item (@$items) { | |||||||||||||||||||||||||||||||||||||||||
| 814 | return 1 if $item->{kind} eq 'section' | ||||||||||||||||||||||||||||||||||||||||||
| 815 | 22 | ▶ | && $item->{name} eq $METADATA_SECTION; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$item->{"kind"} eq "section" and $item->{"name"} eq $File::SOPS::Format::INI::METADATA_SECTION
MC/DC100%2/2
$item->{"kind"} eq "section" and $item->{"name"} eq $File::SOPS::Format::INI::METADATA_SECTION
$item->{"kind"} eq "section"
$item->{"name"} eq $File::SOPS::Format::INI::METADATA_SECTION
Truth table100%3/3
A and B
A= $item->{"kind"} eq "section"B= $item->{"name"} eq $File::SOPS::Format::INI::METADATA_SECTION
| |||||||||||||||||||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||
| 818 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 819 | } | ||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||
| 822 | 16 | ▶ | sub format_name { 'ini' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||||||||
| 825 | 0 | ▶ | sub file_extensions { qw(ini) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_extensions: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||||||||
| 828 | 0 | ▶ | sub detect { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detect: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 829 | 0 | my ($class, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 830 | 0 | ▶ | return 1 if $filename =~ /\.ini$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 831 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||||||||||
| 836 | 82 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||
| 838 | __END__ | ||||||||||||||||||||||||||||||||||||||||||