Return success/failure in commands uniformly

This commit is contained in:
Roberto Alsina 2024-07-05 00:13:13 -03:00
parent 33cd8be45b
commit 6eb9c49c22
7 changed files with 53 additions and 28 deletions

View File

@ -2,7 +2,7 @@ module Faaso
module Commands
# Build images for one or more funkos from source
struct Build
def run(options, folders : Array(String))
def run(options, folders : Array(String)) : Int32
funkos = Funko::Funko.from_paths(folders)
if options["--local"]
@ -58,10 +58,11 @@ module Faaso
body = JSON.parse(ex.response.body)
Log.info { body["stdout"] }
Log.error { body["stderr"] }
exit 1
end
end
end
return 1
end
end
end
0
end
end
end

View File

@ -1,7 +1,7 @@
module Faaso
module Commands
struct Export
def run(options, source : String, destination : String)
def run(options, source : String, destination : String) : Int32
funko = Funko::Funko.from_paths([source])[0]
# Create temporary build location
dst_path = destination
@ -12,6 +12,7 @@ module Faaso
Log.info { "Exporting #{funko.path} to #{dst_path}" }
Dir.mkdir_p(dst_path)
funko.prepare_build Path[dst_path]
0
end
end
end

View File

@ -7,17 +7,31 @@ module Faaso
@@known : Array(String) = {{`find ./runtimes -type d -mindepth 1`.split('\n').reject(&.empty?)}}
@@filelist : Array(String) = {{`find ./runtimes -type f -mindepth 1`.split('\n').reject(&.empty?)}}
def run(options, folder)
def run(options, folder) : Int32
Log.debug { "@@known: #{@@known}" }
Log.debug { "@@filelist: #{@@filelist}" }
if options["-r"].as(String) == "list"
runtime = options["-r"].as(String)
# Give a list of known runtimes
if runtime == "list"
Log.info { "Crystal has some included runtimes:\n" }
@@known.each do |runtime|
Log.info { " * #{Path[runtime].basename}" }
@@known.each do |i|
Log.info { " * #{Path[i].basename}" }
end
Log.info { "\nOr if you have your own, use a folder name" }
return 0
end
# Create folder with a preconfigured funko for this runtime
if @@known.includes? "./runtimes/#{runtime}"
Log.info { "Using known runtime #{runtime}" }
elsif File.exists? runtime
Log.info { "Using directory #{runtime} as runtime" }
else
Log.error { "Can't find runtime #{runtime}" }
return 1
end
0
end
end
end

View File

@ -10,7 +10,7 @@ module Faaso
# In both cases stopped instances after the required
# scale is reached are deleted.
struct Scale
def local(options, name, scale)
def local(options, name, scale) : Int32
funko = Funko::Funko.from_names([name])[0]
# Asked about scale
if !scale
@ -20,12 +20,13 @@ module Faaso
# Asked to set scale
if funko.image_history.empty?
Log.error { "Error: no images available for #{funko.name}:latest" }
exit 1
return 1
end
funko.scale(scale.as(String).to_i)
0
end
def remote(options, name, scale)
def remote(options, name, scale) : Int32
if !scale
response = Crest.get(
"#{FAASO_SERVER}funkos/#{name}/scale/", \
@ -37,14 +38,15 @@ module Faaso
end
body = JSON.parse(response.body)
Log.info { body["output"] }
0
rescue ex : Crest::InternalServerError
Log.error { "Error scaling funko #{name}" }
body = JSON.parse(ex.response.body)
Log.info { body["output"] }
exit 1
1
end
def run(options, name, scale)
def run(options, name, scale) : Int32
if options["--local"]
return local(options, name, scale)
end

View File

@ -1,7 +1,7 @@
module Faaso
module Commands
struct Secret
def local(options, funko, name, secret)
def local(options, funko, name, secret) : Int32
if options["--add"]
dst_dir = "secrets/#{funko}"
Dir.mkdir_p(dst_dir) unless Dir.exists?(dst_dir)
@ -9,9 +9,10 @@ module Faaso
elsif options["--delete"]
File.delete("secrets/#{funko}/#{name}")
end
0
end
def remote(options, funko, name, secret)
def remote(options, funko, name, secret) : Int32
if options["--add"]
Crest.post(
"#{FAASO_SERVER}secrets/",
@ -26,12 +27,13 @@ module Faaso
"#{FAASO_SERVER}secrets/#{funko}/#{name}",
user: "admin", password: "admin")
end
0
rescue ex : Crest::RequestFailed
Log.error { "Error #{ex.response.status_code}" }
exit 1
1
end
def run(options, funko, name)
def run(options, funko, name) : Int32
if options["--add"]
Log.info { "Enter the secret, end with Ctrl-D" } if STDIN.tty?
secret = STDIN.gets_to_end

View File

@ -1,7 +1,7 @@
module Faaso
module Commands
struct Status
def local(options, name)
def local(options, name) : Int32
funko = Funko::Funko.from_names([name])[0]
status = funko.docker_status
@ -17,22 +17,24 @@ module Faaso
status.images.each do |image|
Log.info { " #{image.repo_tags} #{Time.unix(image.created)}" }
end
0
end
def remote(options, name)
def remote(options, name) : Int32
response = Crest.get(
"#{FAASO_SERVER}funkos/#{name}/status/", \
user: "admin", password: "admin")
body = JSON.parse(response.body)
Log.info { body["output"] }
0
rescue ex : Crest::InternalServerError
Log.error { "Error scaling funko #{name}" }
body = JSON.parse(ex.response.body)
Log.info { body["output"] }
exit 1
1
end
def run(options, name)
def run(options, name) : Int32
if options["--local"]
return local(options, name)
end

View File

@ -67,17 +67,20 @@ ans = Docopt.docopt(doc, ARGV)
LogFormat.setup(ans["-v"].to_s.to_i)
Log.debug { ans }
status : Int32 = 0
case ans
when .fetch("build", false)
Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String)))
status = Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String)))
when .fetch("export", false)
Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String))
status = Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String))
when .fetch("new", false)
Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0])
status = Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0])
when .fetch("scale", false)
Faaso::Commands::Scale.new.run(ans, ans["FUNKO"].as(String), ans["SCALE"])
status = Faaso::Commands::Scale.new.run(ans, ans["FUNKO"].as(String), ans["SCALE"])
when .fetch("secret", false)
Faaso::Commands::Secret.new.run(ans, ans["FUNKO"].as(String), ans["SECRET"].as(String))
status = Faaso::Commands::Secret.new.run(ans, ans["FUNKO"].as(String), ans["SECRET"].as(String))
when .fetch("status", false)
Faaso::Commands::Status.new.run(ans, ans["FUNKO"].as(String))
status = Faaso::Commands::Status.new.run(ans, ans["FUNKO"].as(String))
end
exit(status)