Hitcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 411230019f3723d05e63778315585d6322e9cc0c4a993e7b511a34ad52308d47
4
+ data.tar.gz: 38ad9fbba5717a918d47fd46dcad40c443c72e3a6ecec4b1555a0e3c8d939565
5
+ SHA512:
6
+ metadata.gz: 4620b51e90ca16093a34789b3124dcf8c0d95a604bb85ad50f09adb261e3b4f163fed818fa20b5025dd64b7eeb29f674a636b4abf10d6194a20c50cb0c26d0ed
7
+ data.tar.gz: ab955dbda244b81935da8e1994d847cb9c9ee64c5414822b80411b492f39aab5534d03dadfca8058a7626169d9bbd36dae4e70557bd16a424e1a4cd5d1839a83
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in Hitcher.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
8
+
9
+ #gem "tlogger", path: "../incu-2020/tlogger"
10
+
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Chris Liaw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Hitcher
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/Hitcher`. 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 'Hitcher'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install Hitcher
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 test` 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]/Hitcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/Hitcher/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Hitcher project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/Hitcher/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ require 'devops_helper'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hitcher"
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
data/cspec ADDED
@@ -0,0 +1,27 @@
1
+
2
+ version "1.0"
3
+ container :docker
4
+
5
+ image_name "ti"
6
+ container_name "tc"
7
+
8
+ interactive true
9
+ tty true
10
+
11
+ dockerfile do_plain
12
+ FROM ruby-2_7_1
13
+ RUN apt-get update && apt-get install -y yarn
14
+ CMD ["/bin/bash","--login"]
15
+ #CMD ["/usr/share/rvm/rubies/ruby-2.7.1/bin/irb"]
16
+ end
17
+
18
+ #expose 3000
19
+ expose 3008
20
+
21
+ mount do_plain
22
+ $PWD:/opt/raising/raising
23
+ ../rails6:/opt/raising/rails6
24
+ ../../stateful_gem:/opt/dev-deps/stateful_gem
25
+ ../lib/acts_as_tree:/opt/dev-deps/acts_as_tree
26
+ end
27
+
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'hitcher'
4
+ require 'colorize'
5
+ require_relative '../lib/hitcher/global'
6
+
7
+ #
8
+ # Hitcher run
9
+ #
10
+
11
+ global = Hitcher::Global.instance
12
+ cs = File.join(Dir.getwd, global.config.spec_filename)
13
+
14
+ if File.exist?(cs)
15
+
16
+ cli = Hitcher::Cli.new
17
+ inst = cli.parse(cs)
18
+
19
+ if ARGV.length > 0
20
+
21
+ cmd = ARGV[0]
22
+ case cmd
23
+ when "run","r"
24
+ cli.run(inst)
25
+ when "run_prompt","rp"
26
+ cli.run_prompt(inst)
27
+ when "run_new_prompt", "rnp"
28
+ cli.run_new_prompt(inst)
29
+ when "-v"
30
+ STDOUT.puts "Hitcher version #{Hitcher::VERSION}"
31
+ else
32
+ STDERR.puts "Unsupported command '#{cmd}'".red
33
+ exit(-1)
34
+ end
35
+
36
+ else
37
+ STDOUT.puts "Hitcher version #{Hitcher::VERSION}"
38
+ STDOUT.puts "> Hitcher < run | r | run_prompt | rp | run_new_prompt | rnp >"
39
+ exit(-1)
40
+ end
41
+
42
+ else
43
+ STDERR.puts "cspec not found. Please provide one at #{Dir.getwd} or pass in as parameter"
44
+ exit(-1)
45
+ end
46
+
@@ -0,0 +1,41 @@
1
+ require_relative 'lib/hitcher/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "Hitcher"
5
+ spec.version = Hitcher::VERSION
6
+ spec.authors = ["Chris Liaw"]
7
+ spec.email = ["chrisliaw@antrapol.com"]
8
+
9
+ spec.summary = %q{ }
10
+ spec.description = %q{ }
11
+ spec.homepage = ""
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ #spec.metadata["homepage_uri"] = spec.homepage
18
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "activesupport"
31
+
32
+ spec.add_dependency "toolrack", ">= 0.5.3"
33
+ spec.add_dependency "tlogger", ">= 0.25"
34
+
35
+ spec.add_dependency "tty-prompt"
36
+ spec.add_dependency "tty-command"
37
+ spec.add_dependency "colorize"
38
+ spec.add_dependency 'ptools'
39
+
40
+ spec.add_development_dependency 'devops_helper'
41
+ end
@@ -0,0 +1,41 @@
1
+ require "hitcher/version"
2
+
3
+ require_relative 'hitcher/dsl'
4
+ require_relative 'hitcher/run_ops'
5
+
6
+ module Hitcher
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+
10
+ CONF_PATH = File.join(Dir.home,".hitcher","hitcher_conf.yml")
11
+
12
+ class Cli
13
+ include Hitcher::Dsl
14
+ include Hitcher::CliOps::RunOps
15
+
16
+ # DSL start here!
17
+ def parse(spec)
18
+ if File.exist?(spec)
19
+ File.open(spec,"r") do |f|
20
+ @cont = f.read
21
+ end
22
+
23
+ begin
24
+ @inst = start(@cont.strip.lines)
25
+ @inst
26
+ rescue Exception => ex
27
+ Hitcher::Global.instance.glog.error ex.message
28
+ Hitcher::Global.instance.glog.error ex.backtrace.join("\n")
29
+ raise ex
30
+ end
31
+ #logger.debug "instance_eval after parse"
32
+
33
+ else
34
+ raise Error, "Given spec file '#{spec}' does not exist"
35
+ end
36
+ end # parse
37
+
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,50 @@
1
+
2
+ require 'toolrack'
3
+ require 'tempfile'
4
+
5
+ module Hitcher
6
+ module CommandRunner
7
+ include Antrapol::ToolRack::ConditionUtils
8
+ # functions to assist in running a command line app
9
+
10
+ class TerminalNotDefined < StandardError; end
11
+
12
+ def run_in_new_terminal(cmd, &block)
13
+ term = Hitcher::Global.instance.config.terminal
14
+ if not_empty?(term)
15
+ puts cmd
16
+ #tf = Tempfile.new
17
+ #File.open(tf,"w") do |f|
18
+ # f.write "#!/bin/sh"
19
+ # f.write cmd
20
+ # f.write "read -p \"Any key\""
21
+ #end
22
+ `#{term} -x "#{cmd}"`
23
+ #`#{term} -x sh -c "./#{tf}; bash"`
24
+ term
25
+ else
26
+ raise TerminalNotDefined, "Terminal is not defined in the config file"
27
+ end
28
+ end # run_in_new_terminal
29
+
30
+ def run_command(cmd, &block)
31
+
32
+ out = []
33
+ err = []
34
+ c = TTY::Command.new
35
+ # raise TTY::Command::ExitError if command exit code is non-zero
36
+ c.run(cmd) do |o,e|
37
+ out << o if not_empty?(o)
38
+ err << e if not_empty?(e)
39
+ end
40
+
41
+ if not_empty?(err)
42
+ [false, out, err]
43
+ else
44
+ [true, out, err]
45
+ end
46
+
47
+ end # run_command
48
+
49
+ end # module CommandRunner
50
+ end
@@ -0,0 +1,78 @@
1
+
2
+ require 'yaml'
3
+ require 'tty-prompt'
4
+
5
+ require_relative 'terminal_utils'
6
+
7
+ module Hitcher
8
+ class Config
9
+ extend Hitcher::TerminalUtils
10
+
11
+ attr_accessor :terminal, :spec_filename
12
+ def initialize
13
+ @spec_filename = "cspec"
14
+ end
15
+
16
+ def save(path)
17
+ File.open(path,"w") do |f|
18
+ f.write YAML.dump(self)
19
+ end
20
+ end
21
+
22
+ def self.load(path)
23
+ File.open(path,"r") do |f|
24
+ @cont = f.read
25
+ end
26
+ YAML.load(@cont)
27
+ end
28
+
29
+ def self.init_config
30
+
31
+ tty = TTY::Prompt.new
32
+ if not File.exist?(Hitcher::CONF_PATH)
33
+
34
+ FileUtils.mkdir_p(File.dirname(Hitcher::CONF_PATH)) if not File.exist?(File.dirname(Hitcher::CONF_PATH))
35
+
36
+ c = Config.new
37
+ found = detect_known_terminal
38
+ if not found.empty?
39
+ if found.length > 1
40
+ begin
41
+ sel = tty.select("There are more than 1 terminal program found during config file initialization. Please select 1 for default:") do |m|
42
+ found.each do |k,v|
43
+ m.choice "#{k} - #{v}",v
44
+ end
45
+
46
+ m.choice "Quit",:quit
47
+ end
48
+
49
+ if sel != :quit
50
+ c.terminal = sel
51
+ else
52
+ STDERR.puts "\nUser quit."
53
+ exit(-1)
54
+ end
55
+ rescue TTY::Reader::InputInterrupt
56
+ STDERR.puts "\n\nUser aborted"
57
+ exit(-1)
58
+ end
59
+ else
60
+ c.terminal = found.first.value
61
+ end
62
+ else
63
+ tty.error "Hitcher requires configuration file with terminal program set. However there is none found from the system."
64
+ tty.error "System shall proceed to create config file with default value at '#{Hitcher::CONF_PATH}'"
65
+ e.terminal = ""
66
+ end
67
+
68
+ c.save(CONF_PATH)
69
+ tty.ok "Config file successfully created!"
70
+ c
71
+
72
+ else
73
+ Config.load(CONF_PATH)
74
+ end
75
+ end # init_config
76
+
77
+ end
78
+ end
@@ -0,0 +1,108 @@
1
+
2
+ require_relative "global"
3
+ require "active_support/core_ext/string"
4
+
5
+
6
+ module Hitcher
7
+ module Dsl
8
+
9
+ def logger
10
+ Hitcher::Global.instance.glog
11
+ end # logger
12
+
13
+ def start(cont)
14
+ cont.each do |c|
15
+ c = c.strip
16
+ if not c =~ /^#/ and not c.empty?
17
+ # ignoring comment
18
+ eeval(c)
19
+ end
20
+ end
21
+
22
+ @inst
23
+ #logger.debug "Start ends"
24
+ #if not @inst.nil?
25
+ # @inst.gen
26
+ #end
27
+ end
28
+
29
+ def container(type)
30
+ logger.debug "Container #{type}"
31
+ case type
32
+ when :docker
33
+ contDir = File.join(File.dirname(__FILE__),"providers",type.to_s)
34
+ logger.debug "Provider loaded from '#{contDir}'"
35
+ if File.exist?(contDir)
36
+ require_relative File.join(contDir,"dsl")
37
+ # blend into this module
38
+ #self.class.send(:include, eval("Hitcher::#{type.to_s.classify}::Dsl"))
39
+ clsName = type.to_s.classify
40
+ @inst = eval("Hitcher::#{clsName}::#{clsName}Dsl.new")
41
+ else
42
+ raise Hitcher::Error, "Provider '#{type}' not found"
43
+ end
44
+ else
45
+ raise Hitcher::Error, "Unknown container type '#{type}'"
46
+ end
47
+
48
+
49
+ end
50
+
51
+ def method_missing(mtd,*args,&block)
52
+ @inst.send(mtd,*args,&block)
53
+ end # missing_method
54
+
55
+ private
56
+ def eeval(ln)
57
+ if @inst.nil?
58
+ ll = ln.split(" ")
59
+ if ll[0] == "version"
60
+ @version = ll[1].strip
61
+ elsif ll[0] == "container"
62
+ eval(ln)
63
+ else
64
+ raise Error, "Container needs to be defined before proceeding."
65
+ end
66
+ elsif ln =~ /end/
67
+ raise Error, "No block start keyword do_plain or do defined but end keyword is found" if @blc.nil?
68
+ @blc << ln
69
+ if @mode == :cblock
70
+ @inst.handle_block(@kw, @blc)
71
+ else
72
+ self.instance_eval(@blc.join("\n"))
73
+ end
74
+ @mode = :linear
75
+ @kw = nil
76
+ @blc.clear
77
+ @blc = nil
78
+
79
+ elsif ln =~ /do_plain/ or @mode == :cblock
80
+ if @mode != :cblock
81
+ kw = ln.split(" ")[0]
82
+ if @inst.block_kw?(kw)
83
+ @blc = []
84
+ @mode = :cblock
85
+ @kw = kw
86
+ else
87
+ raise Error, "Unknown block method #{kw}"
88
+ end
89
+ end
90
+
91
+ @blc << ln
92
+
93
+ elsif ln =~ /do/ or @mode == :block
94
+ if @mode != :block
95
+ @blc = []
96
+ @kw = ln.split(" ")[0]
97
+ @mode = :block
98
+ end
99
+
100
+ @blc << ln
101
+
102
+ else
103
+ @inst.instance_eval(ln)
104
+ end
105
+ end
106
+
107
+ end
108
+ end