Skip to content

Commit

Permalink
feat: More project and model commands (#190)
Browse files Browse the repository at this point in the history
* feat: list currently active git branch or all branches in dev mode

* feat: project chechout and project deploy

* feat: model cat

* feat: model import

* fix: Update lib/gzr/commands/model.rb

Minor typo

Co-authored-by: John Kaster <[email protected]>

---------

Co-authored-by: John Kaster <[email protected]>
  • Loading branch information
drstrangelooker and jkaster authored May 4, 2023
1 parent 32a6dcc commit 2d05d00
Show file tree
Hide file tree
Showing 9 changed files with 495 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/gzr/commands/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ def ls(*)
Gzr::Commands::Model::Ls.new(options).execute
end
end

desc 'cat MODEL_ID', 'Output json information about a model to screen or file'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :dir, type: :string,
desc: 'Directory to store output file'
method_option :trim, type: :boolean,
desc: 'Trim output to minimal set of fields for later import'
def cat(model_id)
if options[:help]
invoke :help, ['cat']
else
require_relative 'model/cat'
Gzr::Commands::Model::Cat.new(model_id,options).execute
end
end

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

end
end
end
58 changes: 58 additions & 0 deletions lib/gzr/commands/model/cat.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 '../../command'
require_relative '../../modules/model'
require_relative '../../modules/filehelper'

module Gzr
module Commands
class Model
class Cat < Gzr::Command
include Gzr::Model
include Gzr::FileHelper
def initialize(model_name,options)
super()
@model_name = model_name
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
data = cat_model(@model_name)
if data.nil?
say_warning "Model #{@model_name} not found"
return
end
data = trim_model(data) if @options[:trim]

write_file(@options[:dir] ? "Model_#{data[:name]}.json" : nil, @options[:dir],nil, output) do |f|
f.puts JSON.pretty_generate(data)
end
end
end
end
end
end
end
57 changes: 57 additions & 0 deletions lib/gzr/commands/model/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/model'
require_relative '../../modules/filehelper'

module Gzr
module Commands
class Model
class Import < Gzr::Command
include Gzr::Model
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_lookml_model').include? k
end
model = create_model(data)
output.puts "Created model #{model[:name]}" unless @options[:plain]
output.puts model[:name] if @options[:plain]
end
end
end
end
end
end
end
47 changes: 47 additions & 0 deletions lib/gzr/commands/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,53 @@ def deploy_key(project_id)
end
end

desc 'branch PROJECT_ID', 'List the active branch or all branches of PROJECT_ID'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :all, type: :boolean, default: false,
desc: 'List all branches, not just the active branch'
method_option :fields, type: :string, default: 'name,error,message',
desc: 'Fields to display'
method_option :plain, type: :boolean, default: false,
desc: 'print without any extra formatting'
method_option :csv, type: :boolean, default: false,
desc: 'output in csv format per RFC4180'

def branch(project_id)
if options[:help]
invoke :help, ['branch']
else
require_relative 'project/branch'
Gzr::Commands::Project::Branch.new(project_id, options).execute
end
end

desc 'deploy PROJECT_ID', 'Deploy the active branch of PROJECT_ID to production'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'

def deploy(project_id)
if options[:help]
invoke :help, ['deploy']
else
require_relative 'project/deploy'
Gzr::Commands::Project::Deploy.new(project_id, options).execute
end
end

desc 'checkout PROJECT_ID NAME', 'Change the active branch of PROJECT_ID to NAME'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'

def checkout(project_id,name)
if options[:help]
invoke :help, ['checkout']
else
require_relative 'project/checkout'
Gzr::Commands::Project::Checkout.new(project_id, name, options).execute
end
end

end
end
end
96 changes: 96 additions & 0 deletions lib/gzr/commands/project/branch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# The MIT License (MIT)

# Copyright (c) 2018 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 '../../command'
require_relative '../../modules/project'
require 'tty-table'

module Gzr
module Commands
class Project
class Branch < Gzr::Command
include Gzr::Project
def initialize(project_id,options)
super()
@project_id = project_id
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
if get_auth()[:workspace_id] == 'production'
say_warning %Q(
This command only works in dev mode. Use persistent sessions and
change to dev mode before running this command.
$ gzr session login --host looker.example.com
$ gzr session update dev --token_file --host looker.example.com
$ # run the command requiring dev mode here with the --token_file switch
$ gzr session logout --token_file --host looker.example.com
)
return
end

say_warning "querying git_branch(#{@project_id})" if @options[:debug]
data = [git_branch(@project_id)]
begin
say_ok "No active branch found"
return nil
end unless data && data.length > 0

if @options[:all]
say_warning "querying all_git_branches(#{@project_id})" if @options[:debug]
data += all_git_branches(@project_id).select{ |e| e.name != data[0].name }
end
begin
say_ok "No branches found"
return nil
end unless data && data.length > 0

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, width: @options[:width] || TTY::Screen.width)
end
end if table
end
end
end
end
end
end
65 changes: 65 additions & 0 deletions lib/gzr/commands/project/checkout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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 '../../command'
require_relative '../../modules/project'

module Gzr
module Commands
class Project
class Checkout < Gzr::Command
include Gzr::Project
def initialize(project_id,name,options)
super()
@project_id = project_id
@name = name
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
if get_auth()[:workspace_id] == 'production'
say_warning %Q(
This command only works in dev mode. Use persistent sessions and
change to dev mode before running this command.
$ gzr session login --host looker.example.com
$ gzr session update dev --token_file --host looker.example.com
$ # run the command requiring dev mode here with the --token_file switch
$ gzr session logout --token_file --host looker.example.com
)
return
end
data = update_git_branch(@project_id, @name )
if data.nil?
say_warning "Project #{@project_id} not found"
return
end
output.puts data
end
end
end
end
end
end
Loading

0 comments on commit 2d05d00

Please sign in to comment.