summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-02-01 17:20:22 -0700
committermo khan <mo.khan@gmail.com>2020-02-01 17:20:22 -0700
commitc8fd92b56b07090901d1c74862c19cfc1d2df166 (patch)
tree9c2f3e9a4fc7d240511b57f7a039144eee4425a5
parentc7ded689224487abdffd15b46de063f9d03d68a1 (diff)
Add command to build index
-rw-r--r--lib/spandx/cli.rb13
-rw-r--r--lib/spandx/commands/build.rb33
-rw-r--r--lib/spandx/index.rb3
-rw-r--r--lib/spandx/templates/build/.gitkeep1
-rw-r--r--spec/integration/build_spec.rb18
-rw-r--r--spec/unit/build_spec.rb20
-rw-r--r--spec/unit/index_spec.rb2
7 files changed, 88 insertions, 2 deletions
diff --git a/lib/spandx/cli.rb b/lib/spandx/cli.rb
index 98db367..e637bdc 100644
--- a/lib/spandx/cli.rb
+++ b/lib/spandx/cli.rb
@@ -4,6 +4,7 @@ require 'thor'
require 'spandx'
require 'spandx/command'
+require 'spandx/commands/build'
require 'spandx/commands/scan'
module Spandx
@@ -16,6 +17,18 @@ module Spandx
end
map %w[--version -v] => :version
+ desc 'build', 'Command description...'
+ method_option :help, aliases: '-h', type: :boolean,
+ desc: 'Display usage information'
+ def build(*)
+ if options[:help]
+ invoke :help, ['build']
+ else
+ require_relative 'commands/build'
+ Spandx::Commands::Build.new(options).execute
+ end
+ end
+
desc 'scan LOCKFILE', 'Command description...'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
diff --git a/lib/spandx/commands/build.rb b/lib/spandx/commands/build.rb
new file mode 100644
index 0000000..3376898
--- /dev/null
+++ b/lib/spandx/commands/build.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+require_relative '../command'
+
+module Spandx
+ module Commands
+ class Build < Spandx::Command
+ def initialize(options)
+ @options = options
+ end
+
+ def execute(output: $stdout)
+ index = Spandx::Index.new
+ gateways.each do |gateway|
+ index.update!(gateway)
+ end
+ output.puts 'OK'
+ end
+
+ private
+
+ def catalogue
+ Spandx::Catalogue.from_git
+ end
+
+ def gateways
+ [
+ Spandx::Gateways::Nuget.new(catalogue: catalogue)
+ ]
+ end
+ end
+ end
+end
diff --git a/lib/spandx/index.rb b/lib/spandx/index.rb
index 8860d91..4dd32e6 100644
--- a/lib/spandx/index.rb
+++ b/lib/spandx/index.rb
@@ -2,9 +2,10 @@
module Spandx
class Index
+ DEFAULT_DIR = File.expand_path(File.join(Dir.home, '.local', 'share', 'spandx'))
attr_reader :directory, :http
- def initialize(directory, http: Spandx.http)
+ def initialize(directory: DEFAULT_DIR, http: Spandx.http)
@directory = directory
@http = http
end
diff --git a/lib/spandx/templates/build/.gitkeep b/lib/spandx/templates/build/.gitkeep
new file mode 100644
index 0000000..792d600
--- /dev/null
+++ b/lib/spandx/templates/build/.gitkeep
@@ -0,0 +1 @@
+#
diff --git a/spec/integration/build_spec.rb b/spec/integration/build_spec.rb
new file mode 100644
index 0000000..691a0a4
--- /dev/null
+++ b/spec/integration/build_spec.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+RSpec.describe '`spandx build` command', type: :cli do
+ it 'executes `spandx help build` command successfully' do
+ output = `spandx help build`
+ expected_output = <<~OUT
+ Usage:
+ spandx build
+
+ Options:
+ -h, [--help], [--no-help] # Display usage information
+
+ Command description...
+ OUT
+
+ expect(output).to eq(expected_output)
+ end
+end
diff --git a/spec/unit/build_spec.rb b/spec/unit/build_spec.rb
new file mode 100644
index 0000000..5b2e9c8
--- /dev/null
+++ b/spec/unit/build_spec.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+RSpec.describe Spandx::Commands::Build do
+ describe '#execute' do
+ subject { described_class.new(options) }
+
+ let(:output) { StringIO.new }
+ let(:options) { {} }
+
+ before do
+ end
+
+ it 'executes `build` command successfully' do
+ stub_request(:get, 'https://api.nuget.org/v3/catalog0/index.json')
+ .to_return(status: 200, body: JSON.generate(items: []))
+ subject.execute(output: output)
+ expect(output.string).to eq("OK\n")
+ end
+ end
+end
diff --git a/spec/unit/index_spec.rb b/spec/unit/index_spec.rb
index 1d0a7f3..fda450e 100644
--- a/spec/unit/index_spec.rb
+++ b/spec/unit/index_spec.rb
@@ -3,7 +3,7 @@
require 'tmpdir'
RSpec.describe Spandx::Index do
- subject { described_class.new(directory) }
+ subject { described_class.new(directory: directory) }
let(:directory) { Dir.mktmpdir('spandx') }