duniter_rb_cli 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ # DuniterRbCli
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/duniter_rb_cli`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'duniter_rb_cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install duniter_rb_cli
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/duniter_rb_cli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the DuniterRbCli project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/duniter_rb_cli/blob/master/CODE_OF_CONDUCT.md).
40
+
41
+ ## Copyright
42
+
43
+ Copyright (c) 2019 Vincent REHM. See [GNU General Public License v3.0](LICENSE.txt) for further details.
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "rake/extensiontask"
7
+
8
+ task :build => :compile
9
+
10
+ Rake::ExtensionTask.new("duniter_rb_cli") do |ext|
11
+ ext.lib_dir = "lib/duniter_rb_cli"
12
+ end
13
+
14
+ task :default => [:clobber, :compile, :spec]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "duniter_rb_cli"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,62 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "duniter_rb_cli/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "duniter_rb_cli"
8
+ spec.license = "GPL-3.0"
9
+ spec.version = DuniterRbCli::VERSION
10
+ spec.authors = ["Vincent REHM"]
11
+ spec.email = ["vincentrehm@protonmail.com"]
12
+
13
+ spec.summary = %q{Lightweight CLI in Ruby manipulating Duniter's BMA API.}
14
+ spec.description = %q{Manipulate easily BMA endpoints in Duniter's nodes.}
15
+ spec.homepage = "https://github.com/vrehm/duniter-rb-cli"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+ spec.extensions = ["ext/duniter_rb_cli/extconf.rb"]
33
+
34
+ spec.add_dependency "thor", "~> 0.20.0"
35
+ spec.add_dependency "tty-command", "~> 0.8.0"
36
+ spec.add_dependency "tty-config", "~> 0.3.0"
37
+ spec.add_dependency "tty-prompt", "~> 0.18.0"
38
+ spec.add_dependency "tty-tree", "~> 0.2.0"
39
+ # spec.add_dependency "tty-box", "~> 0.3.0"
40
+ # spec.add_dependency "tty-color", "~> 0.4"
41
+ # spec.add_dependency "tty-cursor", "~> 0.6"
42
+ # spec.add_dependency "tty-editor", "~> 0.5.0"
43
+ # spec.add_dependency "tty-file", "~> 0.7.0"
44
+ # spec.add_dependency "tty-font", "~> 0.2.0"
45
+ # spec.add_dependency "tty-markdown", "~> 0.5.0"
46
+ # spec.add_dependency "tty-pager", "~> 0.12.0"
47
+ # spec.add_dependency "tty-pie", "~> 0.1.0"
48
+ # spec.add_dependency "tty-platform", "~> 0.2.0"
49
+ # spec.add_dependency "tty-progressbar", "~> 0.16.0"
50
+ # spec.add_dependency "tty-screen", "~> 0.6"
51
+ # spec.add_dependency "tty-spinner", "~> 0.9.0"
52
+ # spec.add_dependency "tty-table", "~> 0.10.0"
53
+ # spec.add_dependency "tty-which", "~> 0.4"
54
+ # spec.add_dependency "pastel", "~> 0.7.2"
55
+ spec.add_dependency "httparty", "~> 0.13.7"
56
+
57
+ spec.add_development_dependency "bundler", "~> 1.16"
58
+ spec.add_development_dependency "rake", "~> 10.0"
59
+ spec.add_development_dependency "rake-compiler"
60
+ spec.add_development_dependency "rspec", "~> 3.0"
61
+ spec.add_development_dependency "pry-byebug", "~> 3.6"
62
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib_path = File.expand_path('../lib', __dir__)
5
+ $:.unshift(lib_path) if !$:.include?(lib_path)
6
+ require 'duniter_rb_cli/cli'
7
+
8
+ Signal.trap('INT') do
9
+ warn("\n#{caller.join("\n")}: interrupted")
10
+ exit(1)
11
+ end
12
+
13
+ begin
14
+ DuniterRbCli::CLI.start
15
+ rescue DuniterRbCli::CLI::Error => err
16
+ puts "ERROR: #{err.message}"
17
+ exit 1
18
+ end
@@ -0,0 +1,9 @@
1
+ #include "duniter_rb_cli.h"
2
+
3
+ VALUE rb_mDuniterRbCli;
4
+
5
+ void
6
+ Init_duniter_rb_cli(void)
7
+ {
8
+ rb_mDuniterRbCli = rb_define_module("DuniterRbCli");
9
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef DUNITER_RB_CLI_H
2
+ #define DUNITER_RB_CLI_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* DUNITER_RB_CLI_H */
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("duniter_rb_cli/duniter_rb_cli")
@@ -0,0 +1,6 @@
1
+ require "duniter_rb_cli/version"
2
+ require "duniter_rb_cli/duniter_rb_cli"
3
+
4
+ module DuniterRbCli
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ module DuniterRbCli
6
+ # Handle the application command line parsing
7
+ # and the dispatch to various command objects
8
+ #
9
+ # @api public
10
+ class CLI < Thor
11
+ # Error raised by this runner
12
+ Error = Class.new(StandardError)
13
+
14
+ desc 'version', 'duniter_rb_cli version'
15
+ def version
16
+ require_relative 'version'
17
+ puts "v#{DuniterRbCli::VERSION}"
18
+ end
19
+ map %w(--version -v) => :version
20
+
21
+ desc 'wot_list', 'List all UIDs of WOT members for your network setting'
22
+ method_option :help, aliases: '-h', type: :boolean,
23
+ desc: 'Display usage information'
24
+ def wot_list(*)
25
+ if options[:help]
26
+ invoke :help, ['wot_list']
27
+ else
28
+ require_relative 'commands/wot_list'
29
+ DuniterRbCli::Commands::WotList.new(options).execute
30
+ end
31
+ end
32
+
33
+ desc 'wot_count', 'Count all WOT members for your network setting'
34
+ method_option :help, aliases: '-h', type: :boolean,
35
+ desc: 'Display usage information'
36
+ def wot_count(*)
37
+ if options[:help]
38
+ invoke :help, ['wot_count']
39
+ else
40
+ require_relative 'commands/wot_count'
41
+ DuniterRbCli::Commands::WotCount.new(options).execute
42
+ end
43
+ end
44
+
45
+ require_relative 'commands/config'
46
+ register DuniterRbCli::Commands::Config, 'config', 'config [SUBCOMMAND]', 'Set and get network option'
47
+ end
48
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module DuniterRbCli
6
+ class Command
7
+ extend Forwardable
8
+
9
+ def_delegators :command, :run
10
+
11
+ # Execute this command
12
+ #
13
+ # @api public
14
+ def execute(*)
15
+ raise(
16
+ NotImplementedError,
17
+ "#{self.class}##{__method__} must be implemented"
18
+ )
19
+ end
20
+
21
+ # The external commands runner
22
+ #
23
+ # @see http://www.rubydoc.info/gems/tty-command
24
+ #
25
+ # @api public
26
+ def command(**options)
27
+ require 'tty-command'
28
+ TTY::Command.new(options)
29
+ end
30
+
31
+ # The cursor movement
32
+ #
33
+ # @see http://www.rubydoc.info/gems/tty-cursor
34
+ #
35
+ # @api public
36
+ def cursor
37
+ require 'tty-cursor'
38
+ TTY::Cursor
39
+ end
40
+
41
+ # Open a file or text in the user's preferred editor
42
+ #
43
+ # @see http://www.rubydoc.info/gems/tty-editor
44
+ #
45
+ # @api public
46
+ def editor
47
+ require 'tty-editor'
48
+ TTY::Editor
49
+ end
50
+
51
+ # File manipulation utility methods
52
+ #
53
+ # @see http://www.rubydoc.info/gems/tty-file
54
+ #
55
+ # @api public
56
+ def generator
57
+ require 'tty-file'
58
+ TTY::File
59
+ end
60
+
61
+ # Terminal output paging
62
+ #
63
+ # @see http://www.rubydoc.info/gems/tty-pager
64
+ #
65
+ # @api public
66
+ def pager(**options)
67
+ require 'tty-pager'
68
+ TTY::Pager.new(options)
69
+ end
70
+
71
+ # Terminal platform and OS properties
72
+ #
73
+ # @see http://www.rubydoc.info/gems/tty-pager
74
+ #
75
+ # @api public
76
+ def platform
77
+ require 'tty-platform'
78
+ TTY::Platform.new
79
+ end
80
+
81
+ # The interactive prompt
82
+ #
83
+ # @see http://www.rubydoc.info/gems/tty-prompt
84
+ #
85
+ # @api public
86
+ def prompt(**options)
87
+ require 'tty-prompt'
88
+ TTY::Prompt.new(options)
89
+ end
90
+
91
+ # The interactive config
92
+ #
93
+ # @see http://www.rubydoc.info/gems/tty-config
94
+ #
95
+ # @api public
96
+ def config(**options)
97
+ require 'tty-config'
98
+ TTY::Config.new(options)
99
+ end
100
+
101
+ # Get terminal screen properties
102
+ #
103
+ # @see http://www.rubydoc.info/gems/tty-screen
104
+ #
105
+ # @api public
106
+ def screen
107
+ require 'tty-screen'
108
+ TTY::Screen
109
+ end
110
+
111
+ # The unix which utility
112
+ #
113
+ # @see http://www.rubydoc.info/gems/tty-which
114
+ #
115
+ # @api public
116
+ def which(*args)
117
+ require 'tty-which'
118
+ TTY::Which.which(*args)
119
+ end
120
+
121
+ # Check if executable exists
122
+ #
123
+ # @see http://www.rubydoc.info/gems/tty-which
124
+ #
125
+ # @api public
126
+ def exec_exist?(*args)
127
+ require 'tty-which'
128
+ TTY::Which.exist?(*args)
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ module DuniterRbCli
6
+ module Commands
7
+ class Config < Thor
8
+
9
+ namespace :config
10
+
11
+ desc 'network', 'Set and get network option'
12
+ method_option :help, aliases: '-h', type: :boolean,
13
+ desc: 'Display usage information'
14
+ def network(*)
15
+ if options[:help]
16
+ invoke :help, ['network']
17
+ else
18
+ require_relative 'config/network'
19
+ DuniterRbCli::Commands::Config::Network.new(options).execute
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../command'
4
+
5
+ module DuniterRbCli
6
+ module Commands
7
+ class Config
8
+ class Network < DuniterRbCli::Command
9
+ def initialize(options)
10
+ @options = options
11
+ @config = config
12
+ @config.filename = 'network'
13
+ @config.extname = '.yml'
14
+ @config.append_path Dir.pwd
15
+ @config.append_path Dir.home
16
+ @config.read if @config.exist?
17
+ @actual_config = @config.fetch(:name)
18
+ end
19
+
20
+ def execute(input: $stdin, output: $stdout)
21
+ output.puts "Config value: #{@actual_config}" unless @actual_config.nil?
22
+ user_response = prompt.select("Set G1 or G1-test:", %w(G1-test G1))
23
+ @config.set :name, value: user_response
24
+ user_response == "G1" ?
25
+ @config.set(:base_url, value: "https://g1.duniter.org/") :
26
+ @config.set(:base_url, value: "https://g1-test.duniter.org/")
27
+ @config.write(force: true)
28
+ output.puts "Value successfully set to: #{@config.fetch(:name)}"
29
+ output.puts "Base url: #{@config.fetch(:base_url)}"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative './config/network'
5
+ require "httparty"
6
+
7
+ module DuniterRbCli
8
+ module Commands
9
+ class WotCount < DuniterRbCli::Command
10
+ def initialize(options)
11
+ @options = options
12
+ @config = config
13
+ @config.filename = 'network'
14
+ @config.extname = '.yml'
15
+ @config.append_path Dir.pwd
16
+ @config.append_path Dir.home
17
+ if @config.exist?
18
+ @actual_config = @config.read
19
+ else
20
+ puts 'Missing some config'
21
+ DuniterRbCli::Commands::Config::Network.new(options).execute
22
+ end
23
+ end
24
+
25
+ def execute(input: $stdin, output: $stdout)
26
+ endpoint = @config.fetch(:base_url) + "wot/members"
27
+ response = HTTParty.get endpoint
28
+ wot_count = response['results'].count
29
+ puts "Wot Members count for #{@config.fetch(:name)}: #{wot_count}"
30
+ output.puts "OK"
31
+ end
32
+ end
33
+ end
34
+ end