diff options
| author | mo khan <mo.khan@gmail.com> | 2020-03-03 10:18:07 -0700 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-03-03 10:18:07 -0700 |
| commit | b10053c7c14c3312f79a6d476b676d0d647d66cb (patch) | |
| tree | b76e1f5a3d0727e0a72b021d7fa1131af85afce5 | |
| parent | 9273188c9abceb4675a32cfbdc40760a454b479d (diff) | |
Extract cli namespace
21 files changed, 215 insertions, 263 deletions
diff --git a/lib/spandx/cli.rb b/lib/spandx/cli.rb index adc582b..ddc7e62 100644 --- a/lib/spandx/cli.rb +++ b/lib/spandx/cli.rb @@ -2,9 +2,9 @@ require 'thor' require 'spandx' -require 'spandx/command' -require 'spandx/commands/index' -require 'spandx/commands/scan' +require 'spandx/cli/command' +require 'spandx/cli/commands/index' +require 'spandx/cli/commands/scan' module Spandx class CLI < Thor @@ -16,7 +16,7 @@ module Spandx end map %w[--version -v] => :version - register Spandx::Commands::Index, 'index', 'index [SUBCOMMAND]', 'Command description...' + register Spandx::Cli::Commands::Index, 'index', 'index [SUBCOMMAND]', 'Command description...' desc 'scan LOCKFILE', 'Scan a lockfile and list dependencies/licenses' method_option :help, aliases: '-h', type: :boolean, @@ -25,7 +25,7 @@ module Spandx if options[:help] invoke :help, ['scan'] else - Spandx::Commands::Scan.new(lockfile, options).execute + Spandx::Cli::Commands::Scan.new(lockfile, options).execute end end end diff --git a/lib/spandx/cli/command.rb b/lib/spandx/cli/command.rb new file mode 100644 index 0000000..7d0981b --- /dev/null +++ b/lib/spandx/cli/command.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +module Spandx + module Cli + class Command + extend Forwardable + + def_delegators :command, :run + + def execute(*) + raise(NotImplementedError, "#{self.class}##{__method__} must be implemented") + end + + def command(**options) + require 'tty-command' + TTY::Command.new(options) + end + + def cursor + require 'tty-cursor' + TTY::Cursor + end + + def editor + require 'tty-editor' + TTY::Editor + end + + def generator + require 'tty-file' + TTY::File + end + + def pager(**options) + require 'tty-pager' + TTY::Pager.new(options) + end + + def platform + require 'tty-platform' + TTY::Platform.new + end + + def prompt(**options) + require 'tty-prompt' + TTY::Prompt.new(options) + end + + def screen + require 'tty-screen' + TTY::Screen + end + + def which(*args) + require 'tty-which' + TTY::Which.which(*args) + end + + def exec_exist?(*args) + require 'tty-which' + TTY::Which.exist?(*args) + end + end + end +end diff --git a/lib/spandx/commands/.gitkeep b/lib/spandx/cli/commands/.gitkeep index e69de29..e69de29 100644 --- a/lib/spandx/commands/.gitkeep +++ b/lib/spandx/cli/commands/.gitkeep diff --git a/lib/spandx/cli/commands/index.rb b/lib/spandx/cli/commands/index.rb new file mode 100644 index 0000000..b3af263 --- /dev/null +++ b/lib/spandx/cli/commands/index.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Spandx + module Cli + module Commands + class Index < Thor + require 'spandx/cli/commands/index/build' + require 'spandx/cli/commands/index/update' + + namespace :index + + desc 'build', 'Build a package index' + method_option :help, aliases: '-h', type: :boolean, desc: 'Display usage information' + method_option :directory, aliases: '-d', type: :string, desc: 'Directory to build index in', default: '.index' + def build(*) + if options[:help] + invoke :help, ['build'] + else + Spandx::Commands::Index::Build.new(options).execute + end + end + + desc 'update', 'Update the offline indexes' + method_option :help, aliases: '-h', type: :boolean, + desc: 'Display usage information' + def update(*) + if options[:help] + invoke :help, ['update'] + else + Spandx::Commands::Index::Update.new(options).execute + end + end + end + end + end +end diff --git a/lib/spandx/cli/commands/index/build.rb b/lib/spandx/cli/commands/index/build.rb new file mode 100644 index 0000000..5a7df74 --- /dev/null +++ b/lib/spandx/cli/commands/index/build.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module Spandx + module Cli + module Commands + class Index + class Build < Spandx::Cli::Command + def initialize(options) + @options = options + end + + def execute(output: $stdout) + index = Spandx::Dotnet::Index.new(directory: @options[:directory]) + gateways.each do |gateway| + gateway.update!(index) + end + output.puts 'OK' + end + + private + + def catalogue + Spandx::Catalogue.from_git + end + + def gateways + [ + Spandx::Dotnet::NugetGateway.new(catalogue: catalogue) + ] + end + end + end + end + end +end diff --git a/lib/spandx/cli/commands/index/update.rb b/lib/spandx/cli/commands/index/update.rb new file mode 100644 index 0000000..ddc16cf --- /dev/null +++ b/lib/spandx/cli/commands/index/update.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Spandx + module Cli + module Commands + class Index + class Update < Spandx::Cli::Command + def initialize(options) + @options = options + end + + def execute(output: $stdout) + [ + 'rubygems' + ].each do |package_manager| + Spandx::Database + .new(url: "https://github.com/mokhan/spandx-#{package_manager}.git") + .update! + end + output.puts 'OK' + end + end + end + end + end +end diff --git a/lib/spandx/cli/commands/scan.rb b/lib/spandx/cli/commands/scan.rb new file mode 100644 index 0000000..9ac96aa --- /dev/null +++ b/lib/spandx/cli/commands/scan.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Spandx + module Cli + module Commands + class Scan < Spandx::Cli::Command + attr_reader :lockfile + + def initialize(lockfile, options) + @lockfile = lockfile ? ::Pathname.new(File.expand_path(lockfile)) : nil + @options = options + end + + def execute(output: $stdout) + if lockfile.nil? + output.puts 'OK' + else + report = Report.new + Parsers.for(lockfile).parse(lockfile).each do |dependency| + report.add(dependency) + end + output.puts report.to_json + end + end + end + end + end +end diff --git a/lib/spandx/templates/.gitkeep b/lib/spandx/cli/templates/.gitkeep index e69de29..e69de29 100644 --- a/lib/spandx/templates/.gitkeep +++ b/lib/spandx/cli/templates/.gitkeep diff --git a/lib/spandx/templates/build/.gitkeep b/lib/spandx/cli/templates/index/build/.gitkeep index 792d600..792d600 100644 --- a/lib/spandx/templates/build/.gitkeep +++ b/lib/spandx/cli/templates/index/build/.gitkeep diff --git a/lib/spandx/templates/index/build/.gitkeep b/lib/spandx/cli/templates/index/update/.gitkeep index 792d600..792d600 100644 --- a/lib/spandx/templates/index/build/.gitkeep +++ b/lib/spandx/cli/templates/index/update/.gitkeep diff --git a/lib/spandx/templates/index/update/.gitkeep b/lib/spandx/cli/templates/scan/.gitkeep index 792d600..792d600 100644 --- a/lib/spandx/templates/index/update/.gitkeep +++ b/lib/spandx/cli/templates/scan/.gitkeep diff --git a/lib/spandx/command.rb b/lib/spandx/command.rb deleted file mode 100644 index 7c3a7b1..0000000 --- a/lib/spandx/command.rb +++ /dev/null @@ -1,119 +0,0 @@ -# frozen_string_literal: true - -module Spandx - class Command - extend Forwardable - - def_delegators :command, :run - - # Execute this command - # - # @api public - def execute(*) - raise( - NotImplementedError, - "#{self.class}##{__method__} must be implemented" - ) - end - - # The external commands runner - # - # @see http://www.rubydoc.info/gems/tty-command - # - # @api public - def command(**options) - require 'tty-command' - TTY::Command.new(options) - end - - # The cursor movement - # - # @see http://www.rubydoc.info/gems/tty-cursor - # - # @api public - def cursor - require 'tty-cursor' - TTY::Cursor - end - - # Open a file or text in the user's preferred editor - # - # @see http://www.rubydoc.info/gems/tty-editor - # - # @api public - def editor - require 'tty-editor' - TTY::Editor - end - - # File manipulation utility methods - # - # @see http://www.rubydoc.info/gems/tty-file - # - # @api public - def generator - require 'tty-file' - TTY::File - end - - # Terminal output paging - # - # @see http://www.rubydoc.info/gems/tty-pager - # - # @api public - def pager(**options) - require 'tty-pager' - TTY::Pager.new(options) - end - - # Terminal platform and OS properties - # - # @see http://www.rubydoc.info/gems/tty-pager - # - # @api public - def platform - require 'tty-platform' - TTY::Platform.new - end - - # The interactive prompt - # - # @see http://www.rubydoc.info/gems/tty-prompt - # - # @api public - def prompt(**options) - require 'tty-prompt' - TTY::Prompt.new(options) - end - - # Get terminal screen properties - # - # @see http://www.rubydoc.info/gems/tty-screen - # - # @api public - def screen - require 'tty-screen' - TTY::Screen - end - - # The unix which utility - # - # @see http://www.rubydoc.info/gems/tty-which - # - # @api public - def which(*args) - require 'tty-which' - TTY::Which.which(*args) - end - - # Check if executable exists - # - # @see http://www.rubydoc.info/gems/tty-which - # - # @api public - def exec_exist?(*args) - require 'tty-which' - TTY::Which.exist?(*args) - end - end -end diff --git a/lib/spandx/commands/index.rb b/lib/spandx/commands/index.rb deleted file mode 100644 index 077aed1..0000000 --- a/lib/spandx/commands/index.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -module Spandx - module Commands - class Index < Thor - require 'spandx/commands/index/build' - require 'spandx/commands/index/update' - - namespace :index - - desc 'build', 'Build a package index' - method_option :help, aliases: '-h', type: :boolean, desc: 'Display usage information' - method_option :directory, aliases: '-d', type: :string, desc: 'Directory to build index in', default: '.index' - def build(*) - if options[:help] - invoke :help, ['build'] - else - Spandx::Commands::Index::Build.new(options).execute - end - end - - desc 'update', 'Update the offline indexes' - method_option :help, aliases: '-h', type: :boolean, - desc: 'Display usage information' - def update(*) - if options[:help] - invoke :help, ['update'] - else - Spandx::Commands::Index::Update.new(options).execute - end - end - end - end -end diff --git a/lib/spandx/commands/index/build.rb b/lib/spandx/commands/index/build.rb deleted file mode 100644 index 3f7094b..0000000 --- a/lib/spandx/commands/index/build.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -module Spandx - module Commands - class Index - class Build < Spandx::Command - def initialize(options) - @options = options - end - - def execute(output: $stdout) - index = Spandx::Dotnet::Index.new(directory: @options[:directory]) - gateways.each do |gateway| - gateway.update!(index) - end - output.puts 'OK' - end - - private - - def catalogue - Spandx::Catalogue.from_git - end - - def gateways - [ - Spandx::Dotnet::NugetGateway.new(catalogue: catalogue) - ] - end - end - end - end -end diff --git a/lib/spandx/commands/index/update.rb b/lib/spandx/commands/index/update.rb deleted file mode 100644 index be3680f..0000000 --- a/lib/spandx/commands/index/update.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module Spandx - module Commands - class Index - class Update < Spandx::Command - def initialize(options) - @options = options - end - - def execute(output: $stdout) - [ - 'rubygems' - ].each do |package_manager| - Spandx::Database - .new(url: "https://github.com/mokhan/spandx-#{package_manager}.git") - .update! - end - output.puts 'OK' - end - end - end - end -end diff --git a/lib/spandx/commands/scan.rb b/lib/spandx/commands/scan.rb deleted file mode 100644 index f40561e..0000000 --- a/lib/spandx/commands/scan.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -module Spandx - module Commands - class Scan < Spandx::Command - attr_reader :lockfile - - def initialize(lockfile, options) - @lockfile = lockfile ? ::Pathname.new(File.expand_path(lockfile)) : nil - @options = options - end - - def execute(output: $stdout) - if lockfile.nil? - output.puts 'OK' - else - report = Report.new - Parsers.for(lockfile).parse(lockfile).each do |dependency| - report.add(dependency) - end - output.puts report.to_json - end - end - end - end -end diff --git a/lib/spandx/templates/scan/.gitkeep b/lib/spandx/templates/scan/.gitkeep deleted file mode 100644 index 792d600..0000000 --- a/lib/spandx/templates/scan/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/spec/unit/index/build_spec.rb b/spec/unit/cli/index/build_spec.rb index 7a375a1..e1c282f 100644 --- a/spec/unit/index/build_spec.rb +++ b/spec/unit/cli/index/build_spec.rb @@ -1,13 +1,11 @@ # frozen_string_literal: true -require 'spandx/commands/index/build' +RSpec.describe Spandx::Cli::Commands::Index::Build do + subject { described_class.new(options) } + let(:options) { {} } -RSpec.describe Spandx::Commands::Index::Build do describe '#execute' do - subject { described_class.new(options) } - let(:output) { StringIO.new } - let(:options) { {} } it 'executes `build` command successfully' do stub_request(:get, 'https://api.nuget.org/v3/catalog0/index.json') diff --git a/spec/unit/cli/index/update_spec.rb b/spec/unit/cli/index/update_spec.rb new file mode 100644 index 0000000..6c174d6 --- /dev/null +++ b/spec/unit/cli/index/update_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +RSpec.describe Spandx::Cli::Commands::Index::Update do + subject { described_class.new(options) } + let(:options) { {} } + + describe "#execute" do + let(:output) { StringIO.new } + + it 'executes `index update` command successfully' do + subject.execute(output: output) + + expect(output.string).to eq("OK\n") + end + end +end diff --git a/spec/unit/scan_spec.rb b/spec/unit/cli/scan_spec.rb index 8d9f5ee..e79986c 100644 --- a/spec/unit/scan_spec.rb +++ b/spec/unit/cli/scan_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Spandx::Commands::Scan do +RSpec.describe Spandx::Cli::Commands::Scan do subject { described_class.new(lockfile, options) } let(:output) { StringIO.new } diff --git a/spec/unit/index/update_spec.rb b/spec/unit/index/update_spec.rb deleted file mode 100644 index 976e4be..0000000 --- a/spec/unit/index/update_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -require 'spandx/commands/index/update' - -RSpec.describe Spandx::Commands::Index::Update do - it 'executes `index update` command successfully' do - output = StringIO.new - options = {} - command = described_class.new(options) - - command.execute(output: output) - - expect(output.string).to eq("OK\n") - end -end |
