Skip to content
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

feat: import and update a project #186

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/gzr/commands/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ def cat(project_id)
end
end

desc 'import PROJECT_FILE', 'Import a project from a file containing json information'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
def import(project_file)
if options[:help]
invoke :help, ['import']
else
require_relative 'project/import'
Gzr::Commands::Project::Import.new(project_file,options).execute
end
end

desc 'update PROJECT_ID PROJECT_FILE', 'Update the given project from a file containing json information'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
def update(project_id,project_file)
if options[:help]
invoke :help, ['update']
else
require_relative 'project/update'
Gzr::Commands::Project::Update.new(project_id,project_file,options).execute
end
end

end
end
end
2 changes: 1 addition & 1 deletion lib/gzr/commands/project/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def execute(input: $stdin, output: $stdout)
end
data = trim_project(data) if @options[:trim]

write_file(@options[:dir] ? "Project_#{project.id}.json" : nil, @options[:dir],nil, output) do |f|
write_file(@options[:dir] ? "Project_#{data[:id]}.json" : nil, @options[:dir],nil, output) do |f|
f.puts JSON.pretty_generate(data)
end
end
Expand Down
57 changes: 57 additions & 0 deletions lib/gzr/commands/project/import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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/project'
require_relative '../../modules/filehelper'

module Gzr
module Commands
class Project
class Import < Gzr::Command
include Gzr::Project
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
read_file(@file) do |data|
data.select! do |k,v|
(keys_to_keep('create_project') - [:git_remote_url]).include? k
end
project = create_project(data)
output.puts "Created project #{project[:id]}" unless @options[:plain]
output.puts project[:id] if @options[:plain]
end
end
end
end
end
end
end
58 changes: 58 additions & 0 deletions lib/gzr/commands/project/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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/project'
require_relative '../../modules/filehelper'

module Gzr
module Commands
class Project
class Update < Gzr::Command
include Gzr::Project
include Gzr::FileHelper
def initialize(id,file, options)
super()
@id = id
@file = file
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}", output: output) if @options[:debug]
with_session do
read_file(@file) do |data|
data.select! do |k,v|
keys_to_keep('update_project').include? k
end
project = update_project(@id, data)
output.puts "Updated project #{project[:id]}" unless @options[:plain]
output.puts project[:id] if @options[:plain]
end
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions lib/gzr/modules/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,27 @@ def trim_project(data)
end
end

def create_project(body)
begin
return @sdk.create_project(body)&.to_attrs
rescue LookerSDK::Error => e
say_error "Error running create_project(#{JSON.pretty_generate(body)})"
say_error e
raise
end
end

def update_project(id,body)
begin
return @sdk.project(id,body)&.to_attrs
rescue LookerSDK::NotFound => e
return nil
rescue LookerSDK::Error => e
say_error "Error running create_project(#{id},#{JSON.pretty_generate(body)})"
say_error e
raise
end
end

end
end