Skip to content

Commit

Permalink
cli: Move generate-config to cli
Browse files Browse the repository at this point in the history
To avoid installation of lot's of small tools for setting up statbus.
  • Loading branch information
jhf committed Jan 15, 2025
1 parent 2c2618a commit 31129ff
Show file tree
Hide file tree
Showing 7 changed files with 513 additions and 349 deletions.
Empty file added .statbus
Empty file.
12 changes: 12 additions & 0 deletions cli/shard.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
version: 2.0
shards:
bindata:
git: https://github.com/spider-gazelle/bindata.git
version: 2.1.0

commander:
git: https://github.com/mrrooijen/commander.git
version: 0.4.0
Expand All @@ -8,6 +12,14 @@ shards:
git: https://github.com/crystal-lang/crystal-db.git
version: 0.12.0

jwt:
git: https://github.com/crystal-community/jwt.git
version: 1.6.1

openssl_ext:
git: https://github.com/spider-gazelle/openssl_ext.git
version: 2.4.4

pg:
git: https://github.com/17dec/crystal-pg.git
version: 0.27.0+git.commit.ed26146a226f31acf77c1949dea6d3e7c6b5674e
Expand Down
2 changes: 2 additions & 0 deletions cli/shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ crystal: ">= 1.10.1"
license: MIT

dependencies:
jwt:
github: crystal-community/jwt
pg:
github: 17dec/crystal-pg
branch: copyout
Expand Down
60 changes: 39 additions & 21 deletions cli/spec/dotenv_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private def with_tempfile(content : String)
tempfile = File.tempfile(".env-test")
begin
File.write(tempfile.path, content)
yield tempfile.path
yield Path.new(tempfile.path)
ensure
tempfile.delete
end
Expand Down Expand Up @@ -43,8 +43,8 @@ describe Dotenv do

with_tempfile(content) do |path|
dotenv = Dotenv.from_file(path)
dotenv.env_file.lines[0].should be_a(Dotenv::CommentLine)
dotenv.env_file.lines[2].should be_a(Dotenv::CommentLine)
dotenv.dotenv_content.lines[0].should be_a(Dotenv::CommentLine)
dotenv.dotenv_content.lines[2].should be_a(Dotenv::CommentLine)
end
end

Expand All @@ -57,7 +57,7 @@ describe Dotenv do

with_tempfile(content) do |path|
dotenv = Dotenv.from_file(path)
dotenv.env_file.lines[1].should be_a(Dotenv::BlankLine)
dotenv.dotenv_content.lines[1].should be_a(Dotenv::BlankLine)
end
end

Expand All @@ -66,7 +66,7 @@ describe Dotenv do

with_tempfile(content) do |path|
dotenv = Dotenv.from_file(path)
line = dotenv.env_file.lines[0].as(Dotenv::KeyValueLine)
line = dotenv.dotenv_content.lines[0].as(Dotenv::KeyValueLine)
line.key.should eq("KEY")
line.value.should eq("value")
line.inline_comment.should eq(" # inline comment")
Expand All @@ -77,7 +77,7 @@ describe Dotenv do

with_tempfile(content) do |path|
dotenv = Dotenv.from_file(path)
line = dotenv.env_file.lines[0].as(Dotenv::KeyValueLine)
line = dotenv.dotenv_content.lines[0].as(Dotenv::KeyValueLine)
line.key.should eq("KEY")
line.value.should eq("value")
line.inline_comment.should eq(" # inline comment")
Expand Down Expand Up @@ -204,32 +204,32 @@ describe Dotenv do
dotenv = Dotenv.from_file(path)

# Verify specific aspects
dotenv.env_file.lines[0].should be_a(Dotenv::CommentLine)
dotenv.env_file.lines[1].should be_a(Dotenv::BlankLine)
dotenv.dotenv_content.lines[0].should be_a(Dotenv::CommentLine)
dotenv.dotenv_content.lines[1].should be_a(Dotenv::BlankLine)

empty = dotenv.env_file.lines[4].as(Dotenv::KeyValueLine)
empty = dotenv.dotenv_content.lines[4].as(Dotenv::KeyValueLine)
empty.key.should eq("EMPTY")
empty.value.should eq("")

spaces = dotenv.env_file.lines[5].as(Dotenv::KeyValueLine)
spaces = dotenv.dotenv_content.lines[5].as(Dotenv::KeyValueLine)
spaces.key.should eq("SPACES")
spaces.value.should eq(" spaced value")
spaces.inline_comment.should eq(" # With trailing comment")

quotes = dotenv.env_file.lines[6].as(Dotenv::KeyValueLine)
quotes = dotenv.dotenv_content.lines[6].as(Dotenv::KeyValueLine)
quotes.key.should eq("QUOTES")
quotes.value.should eq("quoted value")
quotes.quote.should eq('"')

escaped = dotenv.env_file.lines[7].as(Dotenv::KeyValueLine)
escaped = dotenv.dotenv_content.lines[7].as(Dotenv::KeyValueLine)
escaped.key.should eq("ESCAPED")
escaped.value.should eq("escaped\\\"quote")

newlines = dotenv.env_file.lines[8].as(Dotenv::KeyValueLine)
newlines = dotenv.dotenv_content.lines[8].as(Dotenv::KeyValueLine)
newlines.key.should eq("NEWLINES")
newlines.value.should eq("multi\\nline")

inline = dotenv.env_file.lines[9].as(Dotenv::KeyValueLine)
inline = dotenv.dotenv_content.lines[9].as(Dotenv::KeyValueLine)
inline.key.should eq("KEY")
inline.value.should eq("value")
inline.inline_comment.should eq(" # inline comment")
Expand All @@ -242,36 +242,54 @@ describe Dotenv do
reloaded = Dotenv.from_file(path)

# Verify file contents are identical
reloaded.env_file.to_s.should eq(content)
reloaded.dotenv_content.to_s.should eq(content)
end
end
end

describe ".using" do
it "automatically saves changes" do
it "automatically saves changes when using Path" do
with_tempfile("KEY=value") do |path|
initial_content = File.read(path)

Dotenv.using(path) do |dotenv|
dotenv.set("KEY", "new_value")
end

File.read(path).should_not eq(initial_content)
File.read(path).should contain("KEY=new_value")
end
end

it "doesn't save if no changes" do
it "doesn't save if no changes when using Path" do
with_tempfile("KEY=value") do |path|
initial_content = File.read(path)

Dotenv.using(path) do |dotenv|
# Just read, no changes
dotenv.get("KEY")
end

File.read(path).should eq(initial_content)
end
end

it "works with string content" do
content = "KEY=value"
result = Dotenv.using(content) do |dotenv|
dotenv.get("KEY").should eq("value")
dotenv.set("NEW_KEY", "new_value")
"return value"
end
result.should eq("return value")
end

it "doesn't try to save when using string content" do
content = "KEY=value"
Dotenv.using(content) do |dotenv|
dotenv.set("KEY", "new_value")
# No file should be created/modified
end
end
end
end
Loading

0 comments on commit 31129ff

Please sign in to comment.