-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change cli #8
Open
TechMagister
wants to merge
4
commits into
samueleaton:master
Choose a base branch
from
TechMagister:change_cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Change cli #8
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,57 @@ | ||
# coding: utf-8 | ||
require "net/http" | ||
require "uri" | ||
require "yaml" | ||
require 'fileutils' | ||
|
||
sentry_uri = URI.parse("https://raw.githubusercontent.com/samueleaton/sentry/master/src/sentry.cr") | ||
response = Net::HTTP.get_response(sentry_uri) | ||
|
||
if response.code.to_i > 299 | ||
puts "HTTP request error" | ||
puts response.msg | ||
exit 1 | ||
end | ||
|
||
sentry_code = response.body | ||
|
||
sentry_cli_uri = URI.parse("https://raw.githubusercontent.com/samueleaton/sentry/master/src/sentry_cli.cr") | ||
response = Net::HTTP.get_response(sentry_cli_uri) | ||
|
||
if response.code.to_i > 299 | ||
puts "HTTP request error" | ||
puts response.msg | ||
exit 1 | ||
USER = "samueleaton" | ||
BRANCH = "master" | ||
|
||
DEST_DIR = "./dev" | ||
MAIN_FILE = "sentry_cli.cr" | ||
BIN = "./sentry" | ||
|
||
REMOTE_SOURCES = [ | ||
"https://raw.githubusercontent.com/#{USER}/sentry/#{BRANCH}/src/sentry.cr", | ||
"https://raw.githubusercontent.com/#{USER}/sentry/#{BRANCH}/src/sentry_cli.cr", | ||
"https://raw.githubusercontent.com/#{USER}/sentry/#{BRANCH}/src/sentry_command.cr" | ||
] | ||
|
||
sources = {} | ||
|
||
STDOUT.write "Getting sources : " | ||
|
||
REMOTE_SOURCES.each do |source_uri| | ||
uri = URI.parse source_uri | ||
response = Net::HTTP.get_response uri | ||
if response.code.to_i > 299 | ||
STDERR.puts "HTTP request error for #{File.basename source_uri}" | ||
STDERR.puts "URL : #{source_uri}" | ||
STDERR.puts "Error message : #{response.msg}" | ||
exit 1 | ||
else | ||
STDOUT.write '.' | ||
sources[File.basename source_uri] = response.body | ||
end | ||
end | ||
puts | ||
|
||
sentry_cli_code = response.body | ||
FileUtils.mkdir_p DEST_DIR | ||
|
||
begin | ||
shard_yml = YAML.load(File.read "./shard.yml") | ||
raise "missing key in shard.yml: name" unless shard_yml.has_key? "name" | ||
rescue => e | ||
puts "Error with shard.yml" | ||
puts e | ||
exit 1 | ||
sources.each do |filename, content| | ||
File.write "#{DEST_DIR}/#{filename}", content | ||
end | ||
|
||
process_name = shard_yml["name"] | ||
sentry_code.gsub!(/\[process_name\]/, process_name) | ||
sentry_cli_code.gsub!(/\[process_name\]/, process_name) | ||
|
||
FileUtils.mkdir_p "./dev" | ||
File.write "./dev/sentry.cr", sentry_code | ||
File.write "./dev/sentry_cli.cr", sentry_cli_code | ||
|
||
puts "Compiling sentry..." | ||
system "crystal build --release ./dev/sentry_cli.cr -o ./sentry" | ||
system "crystal build --release #{DEST_DIR}/#{MAIN_FILE} -o #{BIN}" | ||
|
||
puts "🤖 sentry installed!" | ||
puts "\nTo run, do (from your app's root directory): | ||
./sentry\n" | ||
puts "\nTo see options: | ||
./sentry --help\n\n" | ||
puts " | ||
To run, do (from your app's root directory): | ||
#{BIN} | ||
" | ||
puts " | ||
To see options: | ||
#{BIN} --help | ||
|
||
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,8 @@ authors: | |
- Sam Eaton <[email protected]> | ||
|
||
license: ISC | ||
|
||
dependencies: | ||
cli: | ||
github: mosop/cli | ||
branch: master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,3 @@ | ||
require "option_parser" | ||
require "./sentry" | ||
require "./sentry_command" | ||
|
||
process_name = "[process_name]" | ||
build_command = "crystal build ./src/[process_name].cr" | ||
build_args = [] of String | ||
run_command = "./[process_name]" | ||
run_args = [] of String | ||
files = ["./src/**/*.cr", "./src/**/*.ecr"] | ||
files_cleared = false | ||
show_help = false | ||
should_build = true | ||
|
||
OptionParser.parse! do |parser| | ||
parser.banner = "Usage: ./sentry [options]" | ||
parser.on( | ||
"-n NAME", | ||
"--name=NAME", | ||
"Sets the name of the app process (current name: #{process_name})") { |name| process_name = name } | ||
parser.on( | ||
"-b COMMAND", | ||
"--build=COMMAND", | ||
"Overrides the default build command") { |command| build_command = command } | ||
parser.on( | ||
"--build-args=ARGS", | ||
"Specifies arguments for the build command") do |args| | ||
args_arr = args.strip.split(" ") | ||
build_args = args_arr if args_arr.size > 0 | ||
end | ||
parser.on( | ||
"--no-build", | ||
"Skips the build step") { should_build = false } | ||
parser.on( | ||
"-r COMMAND", | ||
"--run=COMMAND", | ||
"Overrides the default run command") { |command| run_command = command } | ||
parser.on( | ||
"--run-args=ARGS", | ||
"Specifies arguments for the run command") do |args| | ||
args_arr = args.strip.split(" ") | ||
run_args = args_arr if args_arr.size > 0 | ||
end | ||
parser.on( | ||
"-w FILE", | ||
"--watch=FILE", | ||
"Overrides default files and appends to list of watched files") do |file| | ||
unless files_cleared | ||
files.clear | ||
files_cleared = true | ||
end | ||
files << file | ||
end | ||
parser.on( | ||
"-i", | ||
"--info", | ||
"Shows the values for build/run commands, build/run args, and watched files") do | ||
puts " | ||
name: #{process_name} | ||
build: #{build_command} | ||
build args: #{build_args} | ||
run: #{run_command} | ||
run args: #{run_args} | ||
files: #{files} | ||
" | ||
end | ||
parser.on( | ||
"-h", | ||
"--help", | ||
"Show this help") do | ||
puts parser | ||
exit 0 | ||
end | ||
end | ||
|
||
process_runner = Sentry::ProcessRunner.new( | ||
process_name: process_name, | ||
build_command: build_command, | ||
run_command: run_command, | ||
build_args: build_args, | ||
run_args: run_args, | ||
should_build: should_build, | ||
files: files | ||
) | ||
|
||
process_runner.run | ||
Sentry::SentryCommand.run ARGV |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
require "yaml" | ||
require "cli" | ||
|
||
require "./sentry" | ||
|
||
module Sentry | ||
|
||
class SentryCommand < Cli::Command | ||
|
||
command_name "sentry" | ||
|
||
SHARD_YML = "shard.yml" | ||
DEFAULT_NAME = "[process_name]" | ||
|
||
class Options | ||
|
||
def self.defaults | ||
name = Options.get_name | ||
{ | ||
name: name, | ||
process_name: "./#{name}", | ||
build: "crystal build ./src/#{name}.cr", | ||
watch: ["./src/**/*.cr", "./src/**/*.ecr"] | ||
} | ||
end | ||
|
||
def self.get_name | ||
if File.exists?(SHARD_YML) && | ||
(yaml = YAML.parse(File.read SHARD_YML)) && | ||
(name = yaml["name"]?) | ||
name.as_s | ||
else | ||
DEFAULT_NAME | ||
end | ||
end | ||
|
||
string %w(-n --name), desc: "Sets the name of the app process", | ||
default: Options.defaults[:name] | ||
|
||
string %w(-b --build), desc: "Overrides the default build command", | ||
default: Options.defaults[:build] | ||
|
||
string "--build-args", desc: "Specifies arguments for the build command" | ||
|
||
bool "--no-build", desc: "Skips the build step", default: false | ||
|
||
string %w(-r --run), desc: "Overrides the default run command", | ||
default: Options.defaults[:process_name] | ||
|
||
string "--run-args", desc: "Specifies arguments for the run command" | ||
|
||
array %w(-w --watch), | ||
desc: "Overrides default files and appends to list of watched files", | ||
default: Options.defaults[:watch] | ||
|
||
bool %w(-i --info), | ||
desc: "Shows the values for build/run commands, build/run args, and watched files", | ||
default: false | ||
|
||
help | ||
|
||
end | ||
|
||
def run | ||
|
||
if options.info? | ||
puts " | ||
name: #{options.name?} | ||
build: #{options.build?} | ||
build args: #{options.build_args?} | ||
run: #{options.run?} | ||
run args: #{options.run_args?} | ||
files: #{options.watch?} | ||
" | ||
exit! code: 0 | ||
end | ||
|
||
build_args = if ba = options.build_args? | ||
ba.split " " | ||
else | ||
[] of String | ||
end | ||
run_args = if ra = options.run_args? | ||
ra.split " " | ||
else | ||
[] of String | ||
end | ||
|
||
process_runner = Sentry::ProcessRunner.new( | ||
process_name: options.name, | ||
build_command: options.build, | ||
run_command: options.run, | ||
build_args: build_args, | ||
run_args: run_args, | ||
should_build: !options.no_build?, | ||
files: options.watch | ||
) | ||
|
||
process_runner.run | ||
|
||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it is a Crystal standard to capitalize acronyms (e.g. CLI, HTTP, JSON)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but I don't know if @mosop will change the name now :/