"bundle exec twenty connect" works

This commit is contained in:
0x1eef 2023-12-09 18:37:39 -03:00
parent b23d7fbe4d
commit ba559e1dbc
2 changed files with 47 additions and 3 deletions

View file

@ -1,4 +1,36 @@
module Twenty
module DB
require "json"
DEFAULTS = { connections: [] }
##
# FIXME:
# The following methods are required by
# both twenty-cli, and twenty-backend. Eventually
# they should be moved into their own package.
extend self
def home
File.join Dir.home, ".local", "share", "twenty"
end
def database
Ryo.from DEFAULTS.dup.merge(JSON.parse(database_path))
rescue
Ryo.from(DEFAULTS.dup)
end
def database_path
File.join(home, "database.json")
end
def save!(db)
File.binwrite database_path,
JSON.dump(Ryo.table_of(db, recursive: true))
end
end
require "twenty/backend"
require "twenty/frontend"
require_relative "command"

View file

@ -1,15 +1,27 @@
class Twenty::Command::Connect < Twenty::Command
require "fileutils"
include Twenty::DB
include FileUtils
set_banner usage: "twenty connect [OPTIONS]",
description: "Connect a project to twenty"
def run
options = parse_options(argv)
options.help ? show_help : run_command
options.help ? show_help : run_command(options)
end
private
def run_command
warn "[twenty] connect..."
def run_command(options)
mkdir_p File.dirname(database_path)
path = Dir.getwd
name = File.basename(path)
db.connections.push({name:, path:})
save!(db)
end
def db
@db ||= database
end
end