Skip to content

Commit 788c8e6

Browse files
committed
Update specs and README
1 parent e9d20fb commit 788c8e6

File tree

5 files changed

+129
-99
lines changed

5 files changed

+129
-99
lines changed

README.md

+15-22
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,29 @@ Python expressions are **not** supported.
128128
## Layering data from multiple sources
129129

130130
By default envcat reads variables only from your shell environment.
131-
With `-i` you can additionally source data from YAML, JSON or TOML files.
131+
With `-i` you can additionally source data from YAML, JSON or TOML files.
132+
With `-s` you can override variables directly on the command line.
132133

133-
**Example:**
134+
Both flags can be given multiple times.
135+
136+
**Examples:**
134137

135138
```bash
136-
# Override an env-var via YAML file
137-
$ export FOO=bar
138-
$ echo "foo: batz" >demo.yaml
139+
# Override vars with YAML file
140+
$ export FOO=from_env
141+
$ echo "foo: from_file" >demo.yaml
139142
$ envcat -i env -i yaml:demo.yaml FOO
140-
{"FOO":"batz"}
141-
142-
# We can also ignore the environment
143-
# altogether and use only file sources.
144-
$ envcat -i yaml:foo.yaml -i json:bar.json FOO
145-
{"FOO":"batz"}
146-
```
147-
148-
With `-s` you can overwrite values directly.
143+
{"FOO":"from_file"}
149144

150-
**Example:**
145+
# Override a var with `-s`
146+
$ envcat -i env -i yaml:demo.yaml -s FOO=from_arg FOO
147+
{"FOO":"from_arg"}
151148

152-
```bash
153-
$ export FOO=bar
154-
$ envcat -s FOO=batz HELLO
155-
batz
149+
# Layer data from foo.yaml, the environment,
150+
# JSON from stdin and lastly override FOO
151+
$ envcat -i yaml:foo.yaml -i env -i json:- -s FOO=bar [..]
156152
```
157153

158-
`-s` takes precedence over all other data sources.
159-
160-
161154
### Input normalization
162155

163156
envcat flattens the structure of data sourced via `-i` as follows.

docs/templates/README.md.j2

+15-22
Original file line numberDiff line numberDiff line change
@@ -131,36 +131,29 @@ Python expressions are **not** supported.
131131
## Layering data from multiple sources
132132

133133
By default envcat reads variables only from your shell environment.
134-
With `-i` you can additionally source data from YAML, JSON or TOML files.
134+
With `-i` you can additionally source data from YAML, JSON or TOML files.
135+
With `-s` you can override variables directly on the command line.
135136

136-
**Example:**
137+
Both flags can be given multiple times.
138+
139+
**Examples:**
137140

138141
```bash
139-
# Override an env-var via YAML file
140-
$ export FOO=bar
141-
$ echo "foo: batz" >demo.yaml
142+
# Override vars with YAML file
143+
$ export FOO=from_env
144+
$ echo "foo: from_file" >demo.yaml
142145
$ envcat -i env -i yaml:demo.yaml FOO
143-
{"FOO":"batz"}
144-
145-
# We can also ignore the environment
146-
# altogether and use only file sources.
147-
$ envcat -i yaml:foo.yaml -i json:bar.json FOO
148-
{"FOO":"batz"}
149-
```
150-
151-
With `-s` you can overwrite values directly.
146+
{"FOO":"from_file"}
152147

153-
**Example:**
148+
# Override a var with `-s`
149+
$ envcat -i env -i yaml:demo.yaml -s FOO=from_arg FOO
150+
{"FOO":"from_arg"}
154151

155-
```bash
156-
$ export FOO=bar
157-
$ envcat -s FOO=batz HELLO
158-
batz
152+
# Layer data from foo.yaml, the environment,
153+
# JSON from stdin and lastly override FOO
154+
$ envcat -i yaml:foo.yaml -i env -i json:- -s FOO=bar [..]
159155
```
160156

161-
`-s` takes precedence over all other data sources.
162-
163-
164157
### Input normalization
165158

166159
envcat flattens the structure of data sourced via `-i` as follows.

fixtures/input/test_normalized.yaml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
NAME: John Doe
3+
AGE: "30"
4+
CITY: New York
5+
HOBBIES_0: Reading
6+
HOBBIES_1: Hiking
7+
HOBBIES_2: Cooking
8+
MATRIX_0_0: "1"
9+
MATRIX_0_1: "2"
10+
MATRIX_0_2: "3"
11+
MATRIX_1_0: "4"
12+
MATRIX_1_1: "5"
13+
MATRIX_1_2: "6"
14+
MATRIX_2_0: "7"
15+
MATRIX_2_1: "8"
16+
MATRIX_2_2: "9"
17+
PEOPLE_0_NAME: Alice
18+
PEOPLE_0_AGE: "25"
19+
PEOPLE_1_NAME: Bob
20+
PEOPLE_1_AGE: "28"
21+
ADDRESS_STREET: 123 Main St
22+
ADDRESS_CITY: Springfield
23+
ADDRESS_ZIP: "12345"
24+
EMPLOYEE_NAME: Jane Smith
25+
EMPLOYEE_DEPARTMENT: HR
26+
EMPLOYEE_CONTACT_EMAIL: [email protected]
27+
EMPLOYEE_CONTACT_PHONE: 555-123-4567
28+
EMPLOYEE_PROJECTS_0: Project A
29+
EMPLOYEE_PROJECTS_1: Project B
30+
EMPLOYEE_SKILLS_0: Skill 1
31+
EMPLOYEE_SKILLS_1: Skill 2
32+
COLORS_0: Red
33+
COLORS_1: Green
34+
COLORS_2: Blue
35+
PERSON_FIRST_NAME: Mary
36+
PERSON_LAST_NAME: Johnson
37+
DESCRIPTION: 'This is a multi-line
38+
39+
description in YAML.
40+
41+
It can span multiple lines.
42+
43+
'

spec/envcat/cli/input_spec.cr

+47-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,59 @@ require "../../../src/envcat/cli"
33
require "digest/sha256"
44

55
describe Envcat::Cli do
6-
describe "-i json:fixtures/input/test.json -s AGE=42" do
7-
it "overwrites value from json" do
6+
{% for fmt in %w[json yaml toml] %}
7+
describe "-i {{fmt.id}}:fixtures/input/test.{{fmt.id}}" do
8+
it "parses and normalizes {{fmt.id}}" do
89
expect_output(nil, nil) { |o, e, i|
9-
Envcat::Cli.invoke(%w[-f kv -i json:fixtures/input/test.json -s AGE=42 AGE], o, e, i)
10-
o.to_s.should eq("AGE=42\n")
10+
Envcat::Cli.invoke(%w[-f yaml -i {{fmt.id}}:fixtures/input/test.{{fmt.id}} *], o, e, i)
11+
fixture = YAML.parse(File.read("fixtures/input/test_normalized.yaml"))
12+
YAML.parse(o.to_s).should eq(fixture)
1113
}
1214
end
1315
end
1416

15-
describe "-s AGE=42 -i json:fixtures/input/test.json" do
16-
it "overwrites value from json" do
17-
expect_output(nil, nil) { |o, e, i|
18-
Envcat::Cli.invoke(%w[-f kv -s AGE=42 -i json:fixtures/input/test.json AGE], o, e, i)
19-
o.to_s.should eq("AGE=42\n")
17+
describe "-i {{fmt.id}}:fixtures/input/test.invalid" do
18+
it "prints error and exits with code 11 if parsing fails" do
19+
expect_output(nil, /Malformed input.*is not valid {{fmt.id.upcase}}/) { |o, e, i|
20+
expect_raises(Exit, "11") {
21+
Envcat::Cli.invoke(%w[-f yaml -i {{fmt.id}}:fixtures/input/test.invalid *], o, e, i)
22+
}
23+
}
24+
end
25+
end
26+
27+
describe "-i {{fmt.id}}:fixtures/input/test.notfound" do
28+
it "prints error and exits with code 7 if input file doesn't exist" do
29+
expect_output(nil, /No such file or directory/) { |o, e, i|
30+
expect_raises(Exit, "7") {
31+
Envcat::Cli.invoke(%w[-f yaml -i {{fmt.id}}:fixtures/input/test.notfound *], o, e, i)
32+
}
33+
}
34+
end
35+
end
36+
{% end %}
37+
38+
{% for fmt in %w[env- derp] %}
39+
describe "-i {{fmt.id}}" do
40+
it "prints error and exits with code 3 if argument to -i is invalid" do
41+
expect_output(nil, /Unknown input type/) { |o, e, i|
42+
expect_raises(Exit, "3") {
43+
Envcat::Cli.invoke(%w[-i {{fmt.id}} *], o, e, i)
44+
}
45+
}
46+
end
47+
end
48+
{% end %}
49+
50+
{% for fmt in %w[yaml yaml: json json: toml toml:] %}
51+
describe "-i {{fmt.id}}" do
52+
it "prints error and exits with code 3 if argument to -i misses path" do
53+
expect_output(nil, /Path is required/) { |o, e, i|
54+
expect_raises(Exit, "3") {
55+
Envcat::Cli.invoke(%w[-i {{fmt.id}} *], o, e, i)
56+
}
2057
}
2158
end
2259
end
60+
{% end %}
2361
end

spec/envcat/cli/set_spec.cr

+9-46
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,21 @@ require "../../../src/envcat/cli"
33
require "digest/sha256"
44

55
describe Envcat::Cli do
6-
{% for fmt in %w[json yaml toml] %}
7-
describe "-i {{fmt.id}}:fixtures/input/test.{{fmt.id}}" do
8-
it "parses and normalizes {{fmt.id}}" do
6+
describe "-i json:fixtures/input/test.json -s AGE=42" do
7+
it "overwrites value from json" do
98
expect_output(nil, nil) { |o, e, i|
10-
Envcat::Cli.invoke(%w[-f yaml -i {{fmt.id}}:fixtures/input/test.{{fmt.id}} *], o, e, i)
11-
Digest::SHA256.hexdigest(o.to_s.split("\n").sort.join("\n")).should eq("66dc73717712ef4ed7ac7fa0cd1ccad48c67b718c038fb9c2f852c10e52e77d6")
9+
Envcat::Cli.invoke(%w[-f kv -i json:fixtures/input/test.json -s AGE=42 AGE], o, e, i)
10+
o.to_s.should eq("AGE=42\n")
1211
}
1312
end
1413
end
1514

16-
describe "-i {{fmt.id}}:fixtures/input/test.invalid" do
17-
it "prints error and exits with code 11 if parsing fails" do
18-
expect_output(nil, /Malformed input.*is not valid {{fmt.id.upcase}}/) { |o, e, i|
19-
expect_raises(Exit, "11") {
20-
Envcat::Cli.invoke(%w[-f yaml -i {{fmt.id}}:fixtures/input/test.invalid *], o, e, i)
21-
}
22-
}
23-
end
24-
end
25-
26-
describe "-i {{fmt.id}}:fixtures/input/test.notfound" do
27-
it "prints error and exits with code 7 if input file doesn't exist" do
28-
expect_output(nil, /No such file or directory/) { |o, e, i|
29-
expect_raises(Exit, "7") {
30-
Envcat::Cli.invoke(%w[-f yaml -i {{fmt.id}}:fixtures/input/test.notfound *], o, e, i)
31-
}
32-
}
33-
end
34-
end
35-
{% end %}
36-
37-
{% for fmt in %w[env- derp] %}
38-
describe "-i {{fmt.id}}" do
39-
it "prints error and exits with code 3 if argument to -i is invalid" do
40-
expect_output(nil, /Unknown input type/) { |o, e, i|
41-
expect_raises(Exit, "3") {
42-
Envcat::Cli.invoke(%w[-i {{fmt.id}} *], o, e, i)
43-
}
44-
}
45-
end
46-
end
47-
{% end %}
48-
49-
{% for fmt in %w[yaml yaml: json json: toml toml:] %}
50-
describe "-i {{fmt.id}}" do
51-
it "prints error and exits with code 3 if argument to -i misses path" do
52-
expect_output(nil, /Path is required/) { |o, e, i|
53-
expect_raises(Exit, "3") {
54-
Envcat::Cli.invoke(%w[-i {{fmt.id}} *], o, e, i)
55-
}
15+
describe "-s AGE=42 -i json:fixtures/input/test.json" do
16+
it "overwrites value from json" do
17+
expect_output(nil, nil) { |o, e, i|
18+
Envcat::Cli.invoke(%w[-f kv -s AGE=42 -i json:fixtures/input/test.json AGE], o, e, i)
19+
o.to_s.should eq("AGE=42\n")
5620
}
5721
end
5822
end
59-
{% end %}
6023
end

0 commit comments

Comments
 (0)