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.
@@ -0,0 +1,83 @@
1
+ module DalbenCli
2
+ # Packs a folder as a tar.gz for `deploy:source`: its files and symlinks
3
+ # with their modes, without .git and without what git would ignore: the
4
+ # .gitignore files in the folder and, inside a repository, those above
5
+ # it up to the top and .git/info/exclude.
6
+ class SourceArchive
7
+ MAX_BYTES = 200 * 1024 * 1024
8
+
9
+ attr_reader :files
10
+
11
+ def initialize(root)
12
+ @root = File.expand_path(root)
13
+ raise Error, "#{root} is not a folder" unless File.directory?(@root)
14
+ @ignore = Gitignore.new
15
+ @files = 0
16
+ load_repository_rules
17
+ end
18
+
19
+ # Writes the archive to a temporary file and yields it, rewound; the
20
+ # file is removed afterwards.
21
+ def pack
22
+ Tempfile.create([ "dalben-source", ".tar.gz" ], binmode: true) do |file|
23
+ # Written through its own handle: closing the gzip closes that one.
24
+ Zlib::GzipWriter.wrap(File.open(file.path, "wb")) do |gzip|
25
+ Gem::Package::TarWriter.new(gzip) { add_folder(_1, "") }
26
+ end
27
+ raise Error, "#{@root} has no files to deploy" if @files.zero?
28
+ raise Error, "the archive is #{file.size / 1024 / 1024} MB, over the #{MAX_BYTES / 1024 / 1024} MB limit" if file.size > MAX_BYTES
29
+ yield file
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ # Rules are kept relative to the repository's top, so @prefix leads
36
+ # from there to the packed folder.
37
+ def load_repository_rules
38
+ top = @root
39
+ top = File.dirname(top) until File.exist?(File.join(top, ".git")) || File.dirname(top) == top
40
+ return @prefix = "" unless File.exist?(File.join(top, ".git"))
41
+
42
+ @prefix = @root.delete_prefix(top).delete_prefix("/")
43
+ read_rules("", File.join(top, ".git", "info", "exclude"))
44
+ folders = @prefix.split("/")
45
+ folders.each_index { |i| read_rules(folders.first(i).join("/"), File.join(top, *folders.first(i), ".gitignore")) }
46
+ end
47
+
48
+ def read_rules(base, file) = File.file?(file) && @ignore.add(base, File.read(file))
49
+ def in_repository(path) = @prefix.empty? ? path : "#{@prefix}/#{path}"
50
+
51
+ def add_folder(tar, folder)
52
+ absolute = File.join(@root, folder)
53
+ read_rules(folder.empty? ? @prefix : in_repository(folder), File.join(absolute, ".gitignore"))
54
+
55
+ Dir.children(absolute).sort.each do |name|
56
+ path = folder.empty? ? name : "#{folder}/#{name}"
57
+ add_entry(tar, path, File.lstat(File.join(@root, path)))
58
+ end
59
+ end
60
+
61
+ # Sockets, pipes and devices are left out, as git leaves them out.
62
+ def add_entry(tar, path, stat)
63
+ return if path == ".git" || @ignore.ignored?(in_repository(path), directory: stat.directory?)
64
+
65
+ if stat.directory? then add_folder(tar, path)
66
+ elsif stat.symlink? then add_symlink(tar, path, stat)
67
+ elsif stat.file? then add_file(tar, path, stat)
68
+ end
69
+ end
70
+
71
+ def add_symlink(tar, path, stat)
72
+ tar.add_symlink(path, File.readlink(File.join(@root, path)), stat.mode & 0o777)
73
+ @files += 1
74
+ end
75
+
76
+ def add_file(tar, path, stat)
77
+ tar.add_file_simple(path, stat.mode & 0o777, stat.size) do |out|
78
+ File.open(File.join(@root, path), "rb") { IO.copy_stream(_1, out) }
79
+ end
80
+ @files += 1
81
+ end
82
+ end
83
+ end
data/lib/dalben_cli.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "json"
2
+ require "net/http"
3
+ require "uri"
4
+ require "optparse"
5
+ require "socket"
6
+ require "fileutils"
7
+ require "tempfile"
8
+ require "zlib"
9
+ require "rubygems/package"
10
+
11
+ require_relative "dalben_cli/config"
12
+ require_relative "dalben_cli/api"
13
+ require_relative "dalben_cli/gitignore"
14
+ require_relative "dalben_cli/source_archive"
15
+ require_relative "dalben_cli/main"
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dalben-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gerrit Heiko Schimpf
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Create apps, deploy Docker images, add Postgres and Redis, set config
13
+ vars and domains on Dalben (https://dalben.cloud) from the terminal or an AI agent.
14
+ email:
15
+ - support@dalben.cloud
16
+ executables:
17
+ - dalben
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - exe/dalben
24
+ - lib/dalben_cli.rb
25
+ - lib/dalben_cli/api.rb
26
+ - lib/dalben_cli/config.rb
27
+ - lib/dalben_cli/gitignore.rb
28
+ - lib/dalben_cli/main.rb
29
+ - lib/dalben_cli/source_archive.rb
30
+ homepage: https://dalben.cloud
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://dalben.cloud
35
+ documentation_uri: https://dalben.cloud/docs/cli.md
36
+ rubygems_mfa_required: 'true'
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '3.1'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 4.0.20
52
+ specification_version: 4
53
+ summary: Command line client for Dalben, EU hosting for small Rails apps
54
+ test_files: []