dalben-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +21 -0
- data/exe/dalben +5 -0
- data/lib/dalben_cli/api.rb +65 -0
- data/lib/dalben_cli/config.rb +38 -0
- data/lib/dalben_cli/gitignore.rb +55 -0
- data/lib/dalben_cli/main.rb +1075 -0
- data/lib/dalben_cli/source_archive.rb +83 -0
- data/lib/dalben_cli.rb +15 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b49acbca6214da48adb5ab49ce8083089d6849ca24a5da113ce58d55f8d02fc9
|
|
4
|
+
data.tar.gz: 011df112624218005c09657975bcd429ff1a6f27b71404eee936fd628dd599e6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ec3a89f0b4a35692e7f2bf6497c68806a95b93bd0535eaf06d63601375fe43254abf908225b4585df382debc235d00f14506ab57dd68ce887c64e467cde28750
|
|
7
|
+
data.tar.gz: 704415149c85d063404ed9285d3472d59b89037c247933e8ab8f0fe11cdb561150c7766f5de6413f3e39c31d2669941ec405581876e8e40cf8ee30d2074f488c
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gerrit Heiko Schimpf
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# dalben
|
|
2
|
+
|
|
3
|
+
Command line client for [Dalben](https://dalben.cloud): hosting for small Rails apps on servers in the EU.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
gem install dalben-cli
|
|
7
|
+
dalben login # asks for email and password; defaults to https://dalben.cloud
|
|
8
|
+
dalben apps:create shop --server SERVER --project shop
|
|
9
|
+
dalben addons:create postgres -a shop
|
|
10
|
+
dalben deploy ghcr.io/you/shop:1 -a shop # or dalben deploy:source -a shop to upload this folder
|
|
11
|
+
dalben help
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
For scripts, CI and AI agents, set `DALBEN_URL` and `DALBEN_TOKEN` (an API token from Settings, API tokens) instead of logging in, and add `--json` to read results as JSON.
|
|
15
|
+
|
|
16
|
+
- Getting started: https://dalben.cloud/docs/getting-started.md
|
|
17
|
+
- Deploy an app: https://dalben.cloud/docs/deploy.md
|
|
18
|
+
- Every command, for AI agents: https://dalben.cloud/docs/cli.md
|
|
19
|
+
- Set up an AI agent and install the Dalben skill: https://dalben.cloud/docs/ai-agents.md
|
|
20
|
+
|
|
21
|
+
Ruby 3.1 or newer. No dependencies beyond the standard library. MIT license.
|
data/exe/dalben
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module DalbenCli
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
|
|
4
|
+
# The server refused the request; reason is its error code, such as otp_required.
|
|
5
|
+
class ApiError < Error
|
|
6
|
+
attr_reader :reason
|
|
7
|
+
|
|
8
|
+
def initialize(message, reason: nil)
|
|
9
|
+
super(message)
|
|
10
|
+
@reason = reason
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Api
|
|
15
|
+
def initialize(url, token = nil)
|
|
16
|
+
raise Error, "not logged in; run `dalben login`" if url.nil? || url.empty?
|
|
17
|
+
@base = URI(url.end_with?("/") ? url : "#{url}/")
|
|
18
|
+
@token = token
|
|
19
|
+
if @base.scheme != "https" && !%w[localhost 127.0.0.1 ::1].include?(@base.host) && ENV["DALBEN_ALLOW_HTTP"] != "1"
|
|
20
|
+
raise Error, "refusing to send credentials over plain HTTP to #{@base.host}; use https (or DALBEN_ALLOW_HTTP=1 on a trusted LAN)"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def get(path, query = {}) = request(Net::HTTP::Get, path, query: query)
|
|
25
|
+
def post(path, body = {}, query = {}) = request(Net::HTTP::Post, path, body: body, query: query)
|
|
26
|
+
def patch(path, body = {}) = request(Net::HTTP::Patch, path, body: body)
|
|
27
|
+
def put(path, body = {}) = request(Net::HTTP::Put, path, body: body)
|
|
28
|
+
def delete(path, query = {}) = request(Net::HTTP::Delete, path, query: query)
|
|
29
|
+
|
|
30
|
+
# POSTs a file as the raw body, streamed rather than read into memory.
|
|
31
|
+
def upload(path, file, content_type:) = request(Net::HTTP::Post, path, upload: [ file, content_type ])
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def request(klass, path, body: nil, query: {}, upload: nil)
|
|
36
|
+
uri = URI.join(@base, "api/v1/#{path}")
|
|
37
|
+
uri.query = URI.encode_www_form(query) unless query.empty?
|
|
38
|
+
req = klass.new(uri)
|
|
39
|
+
req["Accept"] = "application/json"
|
|
40
|
+
req["Authorization"] = "Bearer #{@token}" if @token
|
|
41
|
+
req["User-Agent"] = "dalben/0.1"
|
|
42
|
+
if body
|
|
43
|
+
req["Content-Type"] = "application/json"
|
|
44
|
+
req.body = JSON.generate(body)
|
|
45
|
+
end
|
|
46
|
+
if upload
|
|
47
|
+
file, req["Content-Type"] = upload
|
|
48
|
+
req["Content-Length"] = file.size.to_s
|
|
49
|
+
req.body_stream = file
|
|
50
|
+
end
|
|
51
|
+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 10, read_timeout: upload ? 600 : 120) do |http|
|
|
52
|
+
http.request(req)
|
|
53
|
+
end
|
|
54
|
+
data = res.body.to_s.empty? ? nil : JSON.parse(res.body)
|
|
55
|
+
return data if res.is_a?(Net::HTTPSuccess)
|
|
56
|
+
message = data.is_a?(Hash) ? (data["message"] || data["error"]) : res.message
|
|
57
|
+
raise ApiError.new("#{res.code}: #{message}", reason: (data["error"] if data.is_a?(Hash)))
|
|
58
|
+
rescue JSON::ParserError
|
|
59
|
+
raise Error, "#{uri.host} refused the request as too large" if res&.code == "413"
|
|
60
|
+
raise Error, "unexpected response (#{res&.code}) from #{uri.host}"
|
|
61
|
+
rescue SocketError, SystemCallError, Net::OpenTimeout, OpenSSL::SSL::SSLError => e
|
|
62
|
+
raise Error, "cannot reach #{@base}: #{e.message}"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module DalbenCli
|
|
2
|
+
# ~/.config/dalben/config.json (mode 0600). DALBEN_URL, DALBEN_TOKEN and
|
|
3
|
+
# DALBEN_ENV override it, which is handy in CI.
|
|
4
|
+
class Config
|
|
5
|
+
def self.path
|
|
6
|
+
File.join(ENV.fetch("XDG_CONFIG_HOME", File.join(Dir.home, ".config")), "dalben", "config.json")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@data = File.exist?(self.class.path) ? JSON.parse(File.read(self.class.path)) : {}
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def url = ENV["DALBEN_URL"] || @data["url"]
|
|
14
|
+
def token = ENV["DALBEN_TOKEN"] || @data["token"]
|
|
15
|
+
def environment = ENV["DALBEN_ENV"] || @data["environment"]
|
|
16
|
+
|
|
17
|
+
# What `dalben login` stored, without the environment overrides.
|
|
18
|
+
def saved_url = @data["url"]
|
|
19
|
+
def saved_token = @data["token"]
|
|
20
|
+
|
|
21
|
+
def save(url:, token:) = write(@data.merge("url" => url, "token" => token))
|
|
22
|
+
def use_environment(name) = write(@data.merge("environment" => name).compact)
|
|
23
|
+
|
|
24
|
+
# Logging out forgets only the token; the URL and environment stay.
|
|
25
|
+
def forget_token = write(@data.except("token"))
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def write(data)
|
|
30
|
+
@data = data
|
|
31
|
+
FileUtils.mkdir_p(File.dirname(self.class.path), mode: 0o700)
|
|
32
|
+
File.open(self.class.path, File::WRONLY | File::CREAT | File::TRUNC, 0o600) do |f|
|
|
33
|
+
f.write(JSON.pretty_generate(data))
|
|
34
|
+
end
|
|
35
|
+
File.chmod(0o600, self.class.path)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module DalbenCli
|
|
2
|
+
# Decides which paths a folder's .gitignore files leave out, close to how
|
|
3
|
+
# git does: blank lines and # comments are skipped, ! re-includes, a
|
|
4
|
+
# trailing / matches only directories, a pattern with a / inside is
|
|
5
|
+
# anchored to its .gitignore's folder and one without matches a name at
|
|
6
|
+
# any depth, and ** spans folders. The last matching rule wins, and a
|
|
7
|
+
# deeper .gitignore comes after the ones above it.
|
|
8
|
+
class Gitignore
|
|
9
|
+
Rule = Struct.new(:base, :pattern, :negate, :dir_only, :anchored)
|
|
10
|
+
FLAGS = File::FNM_PATHNAME | File::FNM_DOTMATCH
|
|
11
|
+
|
|
12
|
+
def initialize = @rules = []
|
|
13
|
+
|
|
14
|
+
# Adds the rules of a .gitignore in the folder base ("" for the root).
|
|
15
|
+
def add(base, text)
|
|
16
|
+
text.each_line { |line| (rule = parse(base, line)) && @rules << rule }
|
|
17
|
+
self
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# path is relative to the root, with / between folders.
|
|
21
|
+
def ignored?(path, directory: false)
|
|
22
|
+
@rules.reverse_each do |rule|
|
|
23
|
+
next if rule.dir_only && !directory
|
|
24
|
+
relative = relative_to(rule.base, path) or next
|
|
25
|
+
target = rule.anchored ? relative : File.basename(relative)
|
|
26
|
+
return !rule.negate if File.fnmatch?(rule.pattern, target, FLAGS)
|
|
27
|
+
end
|
|
28
|
+
false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def parse(base, line)
|
|
34
|
+
line = line.chomp.sub(/(?<!\\)\s+\z/, "")
|
|
35
|
+
return if line.empty? || line.start_with?("#")
|
|
36
|
+
|
|
37
|
+
negate = line.start_with?("!")
|
|
38
|
+
line = line.delete_prefix("!").sub(/\A\\([#!])/, '\1')
|
|
39
|
+
dir_only = line.end_with?("/")
|
|
40
|
+
line = line.delete_suffix("/")
|
|
41
|
+
anchored = line.include?("/")
|
|
42
|
+
line = line.delete_prefix("/")
|
|
43
|
+
return if line.empty?
|
|
44
|
+
|
|
45
|
+
# "dir/**" means everything inside dir.
|
|
46
|
+
line = "#{line}/*" if line.end_with?("/**")
|
|
47
|
+
Rule.new(base, line, negate, dir_only, anchored)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def relative_to(base, path)
|
|
51
|
+
return path if base.empty?
|
|
52
|
+
path.delete_prefix("#{base}/") if path.start_with?("#{base}/")
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|