Skip to content

Commit

Permalink
feat: Project management and session management additions (#185)
Browse files Browse the repository at this point in the history
* feat: project ls command

* feat: added project cat command

* feat: added session get and session update commands to retrieve and set the workspace_id
  • Loading branch information
drstrangelooker authored May 2, 2023
1 parent 346c9b7 commit 0093a60
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/gzr/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,8 @@ def version

require_relative 'commands/session'
register Gzr::Commands::Session, 'session', 'session [SUBCOMMAND]', 'Commands pertaining to sessions'

require_relative 'commands/project'
register Gzr::Commands::Project, 'project', 'project [SUBCOMMAND]', 'Commands pertaining to projects'
end
end
69 changes: 69 additions & 0 deletions lib/gzr/commands/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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 'thor'

module Gzr
module Commands
class Project < Thor

namespace :project

desc 'ls', 'List all projects'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :fields, type: :string, default: 'id,name,git_production_branch_name',
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 ls(*)
if options[:help]
invoke :help, ['ls']
else
require_relative 'project/ls'
Gzr::Commands::Project::Ls.new(options).execute
end
end

desc 'cat PROJECT_ID', 'Output json information about a project 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(project_id)
if options[:help]
invoke :help, ['cat']
else
require_relative 'project/cat'
Gzr::Commands::Project::Cat.new(project_id,options).execute
end
end

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

module Gzr
module Commands
class Project
class Cat < Gzr::Command
include Gzr::Project
include Gzr::FileHelper
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
data = cat_project(@project_id)
if data.nil?
say_warning "Project #{@project_id} not found"
return
end
data = trim_project(data) if @options[:trim]

write_file(@options[:dir] ? "Project_#{project.id}.json" : nil, @options[:dir],nil, output) do |f|
f.puts JSON.pretty_generate(data)
end
end
end
end
end
end
end
73 changes: 73 additions & 0 deletions lib/gzr/commands/project/ls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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 Ls < Gzr::Command
include Gzr::Project
def initialize(options)
super()
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
say_warning "querying all_projects({ fields: '#{@options[:fields]}' })" if @options[:debug]
data = all_projects(fields: @options[:fields])
begin
say_ok "No projects 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
20 changes: 20 additions & 0 deletions lib/gzr/commands/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ def logout(*)
end
end

desc 'get', 'Get data about current session'
def get(*)
if options[:help]
invoke :help, ['get']
else
require_relative 'session/get'
Gzr::Commands::Session::Get.new(options).execute
end
end

desc 'update WORKSPACE_ID', 'change the workspace_id of the current session'
def update(workspace_id)
if options[:help]
invoke :help, ['update']
else
require_relative 'session/update'
Gzr::Commands::Session::Update.new(workspace_id,options).execute
end
end

end
end
end
47 changes: 47 additions & 0 deletions lib/gzr/commands/session/get.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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/session'

module Gzr
module Commands
class Session
class Get < Gzr::Command
include Gzr::Session
def initialize(options)
super()
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
auth = get_auth()
output.puts JSON.pretty_generate(auth)
end
end
end
end
end
end
51 changes: 51 additions & 0 deletions lib/gzr/commands/session/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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/session'

module Gzr
module Commands
class Session
class Update < Gzr::Command
include Gzr::Session
def initialize(workspace_id,options)
super()
@options = options
@workspace_id = workspace_id
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
if !@options[:token] && !@options[:token_file]
say_warning "Setting the session workspace_id only makes sense with a persistent session using --token or --token_file options"
end
with_session do
auth = update_auth(@workspace_id)
output.puts JSON.pretty_generate(auth)
end
end
end
end
end
end
60 changes: 60 additions & 0 deletions lib/gzr/modules/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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

module Gzr
module Project

def all_projects(fields: 'id')
data = []
begin
data = @sdk.all_projects({ fields: fields })
rescue LookerSDK::NotFound => e
return []
rescue LookerSDK::Error => e
say_error "Error querying all_projects(#{fields})"
say_error e
raise
end
data
end

def cat_project(project_id)
begin
return @sdk.project(project_id)&.to_attrs
rescue LookerSDK::NotFound => e
return nil
rescue LookerSDK::Error => e
say_error "Error getting project(#{project_id})"
say_error e
raise
end
end

def trim_project(data)
data.select do |k,v|
keys_to_keep('create_project').include? k
end
end

end
end
Loading

0 comments on commit 0093a60

Please sign in to comment.