summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md8
-rw-r--r--components/storage.rb44
-rw-r--r--controllers/cartridges.rb16
-rw-r--r--ports/dsl/nano-bots/cartridges.rb4
-rw-r--r--spec/components/storage_spec.rb53
5 files changed, 104 insertions, 21 deletions
diff --git a/README.md b/README.md
index 7134b28..e1e86d7 100644
--- a/README.md
+++ b/README.md
@@ -269,8 +269,8 @@ For credentials and configurations, relevant environment variables can be set in
export NANO_BOTS_ENCRYPTION_PASSWORD=UNSAFE
export NANO_BOTS_END_USER=your-user
-# export NANO_BOTS_STATE_DIRECTORY=/home/user/.local/state/nano-bots
-# export NANO_BOTS_CARTRIDGES_DIRECTORY=/home/user/.local/share/nano-bots/cartridges
+# export NANO_BOTS_STATE_PATH=/home/user/.local/state/nano-bots
+# export NANO_BOTS_CARTRIDGES_PATH=/home/user/.local/share/nano-bots/cartridges
```
Alternatively, if your current directory has a `.env` file with the environment variables, they will be automatically loaded:
@@ -279,8 +279,8 @@ Alternatively, if your current directory has a `.env` file with the environment
NANO_BOTS_ENCRYPTION_PASSWORD=UNSAFE
NANO_BOTS_END_USER=your-user
-# NANO_BOTS_STATE_DIRECTORY=/home/user/.local/state/nano-bots
-# NANO_BOTS_CARTRIDGES_DIRECTORY=/home/user/.local/share/nano-bots/cartridges
+# NANO_BOTS_STATE_PATH=/home/user/.local/state/nano-bots
+# NANO_BOTS_CARTRIDGES_PATH=/home/user/.local/share/nano-bots/cartridges
```
### Cohere Command
diff --git a/components/storage.rb b/components/storage.rb
index 839cd02..270cd81 100644
--- a/components/storage.rb
+++ b/components/storage.rb
@@ -38,6 +38,7 @@ module NanoBot
def self.build_path_and_ensure_state_file!(key, cartridge, environment: {})
path = [
Logic::Helpers::Hash.fetch(cartridge, %i[state directory]),
+ ENV.fetch('NANO_BOTS_STATE_PATH', nil),
ENV.fetch('NANO_BOTS_STATE_DIRECTORY', nil)
].find do |candidate|
!candidate.nil? && !candidate.empty?
@@ -66,11 +67,31 @@ module NanoBot
path
end
- def self.cartridges_path
- [
- ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil),
- "#{user_home!.sub(%r{/$}, '')}/.local/share/nano-bots/cartridges"
- ].compact.uniq.filter { |path| File.directory?(path) }.compact.first
+ def self.cartridges_path(components: {})
+ components[:directory?] = ->(path) { File.directory?(path) } unless components.key?(:directory?)
+ components[:ENV] = ENV unless components.key?(:ENV)
+
+ default = "#{user_home!(components:).sub(%r{/$}, '')}/.local/share/nano-bots/cartridges"
+
+ from_environment = [
+ components[:ENV].fetch('NANO_BOTS_CARTRIDGES_PATH', nil),
+ components[:ENV].fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil)
+ ].compact
+
+ elected = [
+ from_environment.empty? ? nil : from_environment.join(':'),
+ default
+ ].compact.uniq.filter do |path|
+ path.split(':').any? { |candidate| components[:directory?].call(candidate) }
+ end.compact.first
+
+ return default unless elected
+
+ elected = elected.split(':').filter do |path|
+ components[:directory?].call(path)
+ end.compact
+
+ elected.size.positive? ? elected.join(':') : default
end
def self.cartridge_path(path)
@@ -82,9 +103,14 @@ module NanoBot
candidates << "#{partial}.#{extension}"
end
- unless ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil).nil?
- directory = ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY').sub(%r{/$}, '')
+ directories = [
+ ENV.fetch('NANO_BOTS_CARTRIDGES_PATH', nil),
+ ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil)
+ ].compact.map do |directory|
+ directory.split(':')
+ end.flatten.map { |directory| directory.sub(%r{/$}, '') }
+ directories.each do |directory|
partial = File.join(File.dirname(partial), File.basename(partial, File.extname(partial)))
partial = partial.sub(%r{^\.?/}, '')
@@ -115,7 +141,9 @@ module NanoBot
end
end
- def self.user_home!
+ def self.user_home!(components: {})
+ return components[:home] if components[:home]
+
[Dir.home, `echo ~`.strip, '~'].find do |candidate|
!candidate.nil? && !candidate.empty?
end
diff --git a/controllers/cartridges.rb b/controllers/cartridges.rb
index ca1e8f0..3be8d53 100644
--- a/controllers/cartridges.rb
+++ b/controllers/cartridges.rb
@@ -12,16 +12,18 @@ module NanoBot
Logic::Cartridge::Parser.parse(File.read(path), format: File.extname(path))
end
- def self.all
+ def self.all(components: {})
files = {}
- path = Components::Storage.cartridges_path
+ paths = Components::Storage.cartridges_path(components:)
- Dir.glob("#{path}/**/*.{yml,yaml,markdown,mdown,mkdn,md}").each do |file|
- files[Pathname.new(file).realpath] = {
- base: path,
- path: Pathname.new(file).realpath
- }
+ paths.split(':').each do |path|
+ Dir.glob("#{path}/**/*.{yml,yaml,markdown,mdown,mkdn,md}").each do |file|
+ files[Pathname.new(file).realpath] = {
+ base: path,
+ path: Pathname.new(file).realpath
+ }
+ end
end
cartridges = []
diff --git a/ports/dsl/nano-bots/cartridges.rb b/ports/dsl/nano-bots/cartridges.rb
index 40ad14d..fb23c39 100644
--- a/ports/dsl/nano-bots/cartridges.rb
+++ b/ports/dsl/nano-bots/cartridges.rb
@@ -4,8 +4,8 @@ require_relative '../../../controllers/cartridges'
module NanoBot
module Cartridges
- def self.all
- Controllers::Cartridges.all
+ def self.all(components: {})
+ Controllers::Cartridges.all(components:)
end
def self.load(path)
diff --git a/spec/components/storage_spec.rb b/spec/components/storage_spec.rb
new file mode 100644
index 0000000..99131dd
--- /dev/null
+++ b/spec/components/storage_spec.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+require_relative '../../components/storage'
+
+RSpec.describe NanoBot::Components::Storage do
+ it 'symbolizes keys' do
+ expect(
+ described_class.cartridges_path(
+ components: { home: '/home/aqua', ENV: {}, directory?: ->(_) { true } }
+ )
+ ).to eq('/home/aqua/.local/share/nano-bots/cartridges')
+
+ expect(
+ described_class.cartridges_path(
+ components: {
+ home: '/home/aqua',
+ ENV: { 'NANO_BOTS_CARTRIDGES_DIRECTORY' => '/home/aqua/my-cartridges' },
+ directory?: ->(_) { true }
+ }
+ )
+ ).to eq('/home/aqua/my-cartridges')
+
+ expect(
+ described_class.cartridges_path(
+ components: {
+ home: '/home/aqua',
+ ENV: {
+ 'NANO_BOTS_CARTRIDGES_DIRECTORY' => '/home/aqua/my-cartridges',
+ 'NANO_BOTS_CARTRIDGES_PATH' => '/home/aqua/lime/my-cartridges'
+ },
+ directory?: ->(_) { true }
+ }
+ )
+ ).to eq('/home/aqua/lime/my-cartridges:/home/aqua/my-cartridges')
+
+ expect(
+ described_class.cartridges_path(
+ components: {
+ home: '/home/aqua',
+ ENV: {
+ 'NANO_BOTS_CARTRIDGES_DIRECTORY' => '/home/aqua/my-cartridges',
+ 'NANO_BOTS_CARTRIDGES_PATH' => '/home/aqua/lime/my-cartridges:/home/aqua/ivory/my-cartridges'
+ },
+ directory?: lambda do |path|
+ { '/home/aqua/my-cartridges' => true,
+ '/home/aqua/lime/my-cartridge' => false,
+ '/home/aqua/ivory/my-cartridges' => true }[path]
+ end
+ }
+ )
+ ).to eq('/home/aqua/ivory/my-cartridges:/home/aqua/my-cartridges')
+ end
+end