-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Connection handling with Gazer (#184)
* feat: connection cat - get a json of a connection * feat: added connection import|rm|test commands * fix: needed chomp to remove new line for --prompt mode with connection import
- Loading branch information
1 parent
482c00f
commit 346c9b7
Showing
6 changed files
with
410 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# The MIT License (MIT) | ||
|
||
# Copyright (c) 2023 Mike DeAngelo Google, Inc. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require_relative '../../command' | ||
require_relative '../../modules/connection' | ||
require_relative '../../modules/filehelper' | ||
|
||
module Gzr | ||
module Commands | ||
class Connection | ||
class Cat < Gzr::Command | ||
include Gzr::Connection | ||
include Gzr::FileHelper | ||
def initialize(connection_id,options) | ||
super() | ||
@connection_id = connection_id | ||
@options = options | ||
end | ||
|
||
def execute(*args, input: $stdin, output: $stdout) | ||
say_warning("options: #{@options.inspect}") if @options[:debug] | ||
with_session do | ||
data = cat_connection(@connection_id) | ||
if data.nil? | ||
say_warning "Connection #{@connection_id} not found" | ||
return | ||
end | ||
data = trim_connection(data) if @options[:trim] | ||
|
||
outputJSON = JSON.pretty_generate(data) | ||
|
||
file_name = if @options[:dir] | ||
@options[:simple_filename] ? "Connection_#{data[:name]}.json" : "Connection_#{data[:name]}_#{data[:dialect_name]}.json" | ||
else | ||
nil | ||
end | ||
write_file(file_name, @options[:dir], nil, output) do |f| | ||
f.puts outputJSON | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# The MIT License (MIT) | ||
|
||
# Copyright (c) 2023 Mike DeAngelo Google, Inc. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
# frozen_string_literal: true | ||
|
||
require_relative '../../../gzr' | ||
require_relative '../../command' | ||
require_relative '../../modules/connection' | ||
require_relative '../../modules/filehelper' | ||
|
||
module Gzr | ||
module Commands | ||
class Connection | ||
class Import < Gzr::Command | ||
include Gzr::Connection | ||
include Gzr::FileHelper | ||
def initialize(file, options) | ||
super() | ||
@file = file | ||
@options = options | ||
end | ||
|
||
def execute(input: $stdin, output: $stdout) | ||
say_warning("options: #{@options.inspect}", output: output) if @options[:debug] | ||
with_session do | ||
connection = nil | ||
|
||
if @options[:prompt] | ||
reader = TTY::Reader.new | ||
@secret = reader.read_line("Enter your connection password:", echo: false) | ||
@secret.chomp! | ||
end | ||
|
||
read_file(@file) do |data| | ||
if !!cat_connection(data[:name]) | ||
name = data[:name] | ||
if !@options[:force] | ||
raise Gzr::CLI::Error, "Connection #{name} already exists\nUse --force if you want to overwrite it" | ||
end | ||
data.select! do |k,v| | ||
keys_to_keep('update_connection').include? k | ||
end | ||
data[:password] = @secret if @secret | ||
connection = update_connection(name, data) | ||
else | ||
data.select! do |k,v| | ||
keys_to_keep('create_connection').include? k | ||
end | ||
data[:password] = @secret if @secret | ||
connection = create_connection(data) | ||
end | ||
output.puts "Imported connection #{connection[:name]}" unless @options[:plain] | ||
output.puts connection[:id] if @options[:name] | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# The MIT License (MIT) | ||
|
||
# Copyright (c) 2023 Mike DeAngelo Google, Inc. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require_relative '../../command' | ||
require_relative '../../modules/connection' | ||
require_relative '../../modules/filehelper' | ||
|
||
module Gzr | ||
module Commands | ||
class Connection | ||
class Rm < Gzr::Command | ||
include Gzr::Connection | ||
include Gzr::FileHelper | ||
def initialize(connection_name,options) | ||
super() | ||
@connection_name = connection_name | ||
@options = options | ||
end | ||
|
||
def execute(*args, input: $stdin, output: $stdout) | ||
say_warning("options: #{@options.inspect}") if @options[:debug] | ||
with_session do | ||
delete_connection(@connection_name) | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# The MIT License (MIT) | ||
|
||
# Copyright (c) 2018 Mike DeAngelo Looker Data Sciences, Inc. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
# frozen_string_literal: true | ||
|
||
require_relative '../../command' | ||
require_relative '../../modules/connection' | ||
require 'tty-table' | ||
|
||
module Gzr | ||
module Commands | ||
class Connection | ||
class Test < Gzr::Command | ||
include Gzr::Connection | ||
def initialize(connection_name,options) | ||
super() | ||
@connection_name = connection_name | ||
@options = options | ||
end | ||
|
||
def execute(input: $stdin, output: $stdout) | ||
say_warning(@options) if @options[:debug] | ||
with_session do | ||
data = test_connection(@connection_name) | ||
|
||
table_hash = Hash.new | ||
fields = field_names(@options[:fields]) | ||
table_hash[:header] = fields unless @options[:plain] | ||
expressions = fields.collect { |fn| field_expression(fn) } | ||
table_hash[:rows] = data.map do |row| | ||
expressions.collect do |e| | ||
eval "row.#{e}" | ||
end | ||
end | ||
table = TTY::Table.new(table_hash) | ||
alignments = fields.collect do |k| | ||
(k =~ /id$/) ? :right : :left | ||
end | ||
begin | ||
if @options[:csv] then | ||
output.puts render_csv(table) | ||
else | ||
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, multiline: if @options[:plain] then false else true end, width: @options[:width] || TTY::Screen.width) | ||
end | ||
end if table | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.