-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
200 additions
and
97 deletions.
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
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
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,101 +1,6 @@ | ||
require "grip" | ||
require "uri" | ||
require "json" | ||
require "colorize" | ||
require "option_parser" | ||
require "./mockgpt/*" | ||
require "./mockgpt/struct/*" | ||
|
||
homePath = "#{Path.home}/mocker.json" | ||
exePath = "#{File.dirname Process.executable_path.not_nil!}/mocker.json" | ||
confPath = File.exists?(exePath) ? exePath : homePath | ||
if File.file?(confPath) | ||
mocker = JSON.parse(File.read(confPath)) | ||
Mocker.host = mocker["host"].as_s if mocker["host"]? | ||
Mocker.port = mocker["port"].as_i if mocker["port"]? | ||
Mocker.model = mocker["model"].as_s if mocker["model"]? | ||
Mocker.gpt = mocker["gpt"].as_s if mocker["gpt"]? | ||
end | ||
|
||
OptionParser.parse do |parser| | ||
parser.banner = "Usage: mockgpt <subcommand>/<options> <arguments>" | ||
parser.on("config", "Display the configuration in effect") do | ||
parser.on("-h", "--help", "Show config help") do | ||
puts CONFIG_HELP | ||
exit | ||
end | ||
parser.unknown_args do |args, options| | ||
if args.size > 2 | ||
STDERR.puts "Too many arguments: #{args}", parser | ||
exit(1) | ||
end | ||
case args[0]? || "all" | ||
when "all" | ||
puts "Host \t: #{Mocker.host}" | ||
puts "Port \t: #{Mocker.port}" | ||
puts "Model\t: #{Mocker.model}" | ||
puts "GPT \t: #{Mocker.gpt}" | ||
when "host" | ||
puts Mocker.host | ||
when "port" | ||
puts Mocker.port | ||
when "model" | ||
puts Mocker.model | ||
when "gpt" | ||
puts Mocker.gpt | ||
when "init" | ||
puts "Config file initialized." | ||
when "rm" | ||
args[1]? && case args[1] | ||
when "host" | ||
puts Mocker.host | ||
when "port" | ||
puts Mocker.port | ||
when "model" | ||
puts Mocker.model | ||
when "gpt" | ||
puts Mocker.gpt | ||
else | ||
STDERR.puts "Undefined option: #{args[1]}", CONFIG_HELP | ||
end | ||
else | ||
STDERR.puts "Undefined option: #{args[1]}", CONFIG_HELP | ||
end | ||
exit | ||
end | ||
end | ||
parser.on("upgrade", "Upgrade to the latest version") do | ||
url = "https://github.com/yanecc/MockGPT/tags" | ||
pattern = /href="\/yanecc\/MockGPT\/releases\/tag\/([0-9.]+)"/ | ||
latestVersion = Utils.getLatestVersion(url, pattern) | ||
if latestVersion == VERSION | ||
puts "Already up to date." | ||
else | ||
Commands.upgrade if Utils.confirmUpgrade latestVersion | ||
end | ||
exit | ||
end | ||
parser.on("version", "Print the version") do | ||
puts COMMANDS_VERSION | ||
exit | ||
end | ||
parser.on("-b HOST", "--binding HOST", "Bind to the specified host") { |_host| Mocker.host = _host } | ||
parser.on("-p PORT", "--port PORT", "Run on the specified port") { |_port| Mocker.port = _port.to_i } | ||
parser.on("-m MODEL", "--mocker MODEL", "Employ the specified model") { |_model| Mocker.model = _model } | ||
parser.on("-h", "--help", "Show this help") do | ||
puts parser | ||
exit | ||
end | ||
parser.on("-v", "--version", "Print the version") do | ||
puts COMMANDS_VERSION | ||
exit | ||
end | ||
parser.invalid_option do |flag| | ||
STDERR.puts "ERROR: #{flag} is not a valid option." | ||
STDERR.puts parser | ||
exit 1 | ||
end | ||
end | ||
|
||
mockgpt = Application.new(Mocker.host, Mocker.port) | ||
mockgpt.run |
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,6 +1,73 @@ | ||
require "colorize" | ||
|
||
module Mocker | ||
class_property host : String = "localhost" | ||
class_property port : Int32 = 3000 | ||
class_property model : String = "llama3" | ||
class_property gpt : String = "gpt-4" | ||
class_property executable = Path[Process.executable_path.not_nil!].stem | ||
end | ||
|
||
module Config | ||
extend self | ||
|
||
def init(path : Path) | ||
config = self.parse | ||
if File.exists? path | ||
puts "Config file already exists at #{path}" | ||
else | ||
File.write(path, config.to_pretty_json) | ||
puts "Config file created at #{path}" | ||
end | ||
end | ||
|
||
def parse | ||
return { | ||
"host" => Mocker.host, | ||
"port" => Mocker.port, | ||
"model" => Mocker.model, | ||
"gpt" => Mocker.gpt, | ||
} | ||
end | ||
|
||
def parse(path : Path) | ||
if File.file? path | ||
configHash = Hash(String, String | Int32).from_json File.read(path) | ||
else | ||
self.parse | ||
end | ||
end | ||
|
||
def display | ||
puts "Host \t: #{Mocker.host}" | ||
puts "Port \t: #{Mocker.port}" | ||
puts "Model\t: #{Mocker.model}" | ||
puts "GPT \t: #{Mocker.gpt}" | ||
end | ||
|
||
def display(key : String) | ||
config = self.parse | ||
puts "#{key} \t: #{config[key]}" | ||
end | ||
|
||
def remove(path : Path, keys : Array(String)) | ||
config = self.parse path | ||
if File.exists? path | ||
File.write(path, config.reject!(keys).to_pretty_json) | ||
puts "#{keys} got reset." | ||
else | ||
puts "Please create a config file first.", ">> #{Mocker.executable} config init".colorize.bright.light_cyan | ||
end | ||
end | ||
|
||
def set(path : Path, key : String, value : String | Int32) | ||
config = self.parse path | ||
if File.exists? path | ||
config[key] = value | ||
File.write(path, config.to_pretty_json) | ||
puts "#{key} got set to #{value}." | ||
else | ||
puts "Please create a config file first.", ">> #{Mocker.executable} config init".colorize.bright.light_cyan | ||
end | ||
end | ||
end |
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
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,88 @@ | ||
require "option_parser" | ||
|
||
homePath = Path.home.join "mocker.json" | ||
exePath = Path[File.dirname Process.executable_path.not_nil!].join "mocker.json" | ||
confPath = File.exists?(exePath) ? exePath : homePath | ||
if File.file?(confPath) | ||
mocker = JSON.parse(File.read(confPath)) | ||
Mocker.host = mocker["host"].as_s if mocker["host"]? | ||
Mocker.port = mocker["port"].as_i if mocker["port"]? | ||
Mocker.model = mocker["model"].as_s if mocker["model"]? | ||
Mocker.gpt = mocker["gpt"].as_s if mocker["gpt"]? | ||
end | ||
|
||
OptionParser.parse do |parser| | ||
parser.banner = "Usage: #{Mocker.executable} <subcommand>/<options> <arguments>" | ||
parser.on("config", "Display the configuration in effect") do | ||
parser.on("-h", "--help", "Show config help") do | ||
puts CONFIG_HELP | ||
exit | ||
end | ||
parser.on("init", "Generate the configuration file") do | ||
Config.init confPath | ||
exit | ||
end | ||
parser.on("rm", "Reset a configuration to the default value") do | ||
parser.unknown_args do |args, _| | ||
Config.remove(confPath, args) | ||
exit | ||
end | ||
end | ||
parser.unknown_args do |args, options| | ||
if args.size > 2 | ||
STDERR.puts "Too many arguments: #{args}", parser | ||
exit(1) | ||
end | ||
case args[0]? || "all" | ||
when "all" | ||
Config.display | ||
when "host", "model", "gpt" | ||
if args[1]? | ||
Config.set confPath, args[0], args[1] | ||
else | ||
Config.display args[0] | ||
end | ||
when "port" | ||
if args[1]? | ||
Config.set confPath, args[0], args[1].to_i | ||
else | ||
Config.display args[0] | ||
end | ||
else | ||
STDERR.puts "Undefined option: #{args[0]}", CONFIG_HELP | ||
end | ||
exit | ||
end | ||
end | ||
parser.on("upgrade", "Upgrade to the latest version") do | ||
url = "https://github.com/yanecc/MockGPT/tags" | ||
pattern = /href="\/yanecc\/MockGPT\/releases\/tag\/([0-9.]+)"/ | ||
latestVersion = Utils.getLatestVersion(url, pattern) | ||
if latestVersion == VERSION | ||
puts "Already up to date." | ||
else | ||
Commands.upgrade if Utils.confirmUpgrade latestVersion | ||
end | ||
exit | ||
end | ||
parser.on("version", "Print the version") do | ||
puts COMMANDS_VERSION | ||
exit | ||
end | ||
parser.on("-b HOST", "--binding HOST", "Bind to the specified host") { |_host| Mocker.host = _host } | ||
parser.on("-p PORT", "--port PORT", "Run on the specified port") { |_port| Mocker.port = _port.to_i } | ||
parser.on("-m MODEL", "--mocker MODEL", "Employ the specified model") { |_model| Mocker.model = _model } | ||
parser.on("-h", "--help", "Show this help") do | ||
puts parser | ||
exit | ||
end | ||
parser.on("-v", "--version", "Print the version") do | ||
puts COMMANDS_VERSION | ||
exit | ||
end | ||
parser.invalid_option do |flag| | ||
STDERR.puts "ERROR: #{flag} is not a valid option." | ||
STDERR.puts parser | ||
exit 1 | ||
end | ||
end |