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

Feature/cli update config and login #22

Merged
merged 15 commits into from
Apr 13, 2022
Merged
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN gem install bundler -v 2.3.9

COPY lib/uffizzi/version.rb ./lib/uffizzi/
COPY uffizzi.gemspec .
COPY Gemfile* .
COPY Gemfile* ./
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
RUN bundle install --jobs 4

COPY . .
Expand Down
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ If you specify the following environment variables, the Docker image's
entrypoint script can log you into Uffizzi before executing your command.

- `UFFIZZI_USER`
- `UFFIZZI_HOSTNAME`
- `UFFIZZI_SERVER`
- `UFFIZZI_PASSWORD`
- `UFFIZZI_PROJECT` (optional)

Expand Down Expand Up @@ -84,19 +84,19 @@ Run single test
### login

```
$ uffizzi login --user your@email.com --hostname localhost:8080
$ uffizzi login --user your@email.com --server localhost:8080
```

Logging you into the app which you set in the hostname option.
Logging you into the app which you set in the server option or config file

### login options

| Option | Aliase | Description |
| ------------ | ------ | ------------------------- |
| `--user` | `-u` | Your email for logging in |
| `--hostname` | | Adress of your app |
| Option | Aliase | Description |
| ------------ | ------ | ----------------------------------- |
| `--username` | `-u` | Your email for logging in(optional) |
| `--server` | `-s` | Adress of your app(optional) |

If hostname uses basic authentication you can specify options for it by setting `basic_auth_user` and `basic_auth_password` via `config set` command.
If server uses basic authentication you can specify options for it by setting `basic_auth_user` and `basic_auth_password` via `config set` command.

### project

Expand Down Expand Up @@ -150,7 +150,17 @@ You need to set project before use any of these commands via `uffizzi config set

### config

Use this command to configure your cli app. This command has 4 subcommands `list`, `get`, `set`, and `delete`.
Use this command to configure your cli app.

```
$ uffizzi config
```

Launching interactive setup guide that sets the values for `server`, `username` and `project`

### config subcommands

This command has 4 subcommands `list`, `get`, `set`, and `delete`.

```
$ uffizzi config list
Expand All @@ -159,7 +169,7 @@ $ uffizzi config list
Shows all options and their values from the config file.

```
$ uffizzi config get OPTION
$ uffizzi config get-value OPTION
```

Shows the value of the specified option.
Expand All @@ -171,10 +181,10 @@ $ uffizzi config set OPTION VALUE
Sets specified value for specified option. If a specified option already exists and has value it will be overwritten.

```
$ uffizzi config delete OPTION
$ uffizzi config unset OPTION
```

Deletes specified option.
Deletes value of specified option.

### disconnect ###

Expand Down
3 changes: 2 additions & 1 deletion config/uffizzi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ def self.configure
end

configure do |config|
config.hostname = 'http://web:7000'
config.server = 'http://web:7000'
config.credential_types = {
dockerhub: 'UffizziCore::Credential::DockerHub',
azure: 'UffizziCore::Credential::Azure',
google: 'UffizziCore::Credential::Google',
amazon: 'UffizziCore::Credential::Amazon',
github_container_registry: 'UffizziCore::Credential::GithubContainerRegistry',
}
config.default_server = 'app.uffizzi.com'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this value should be https://app.uffizzi.com/

end
end
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
- ./:/gem:cached
- ~/.ssh:/root/.ssh
- ~/.bash_history:/root/.bash_history
- ~/.config/uffizzi:/root/.config/uffizzi
- bundle_cache:/bundle_cache
environment:
- BUNDLE_PATH=/bundle_cache
Expand Down
6 changes: 3 additions & 3 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ set -e # Exit immediately if anything below exits with non-zero status.

if
[ $UFFIZZI_USER ] &&
[ $UFFIZZI_HOSTNAME ] &&
[ $UFFIZZI_SERVER ] &&
[ $UFFIZZI_PASSWORD ]
then
uffizzi login --username "${UFFIZZI_USER}" --hostname "${UFFIZZI_HOSTNAME}"
uffizzi login --username "${UFFIZZI_USER}" --server "${UFFIZZI_SERVER}"
if [ $UFFIZZI_PROJECT ]
then
uffizzi config set project "${UFFIZZI_PROJECT}"
fi
else
echo "Specify environment variables to login before executing Uffizzi CLI."
echo "UFFIZZI_USER, UFFIZZI_HOSTNAME, UFFIZZI_PASSWORD, and optionally UFFIZZI_PROJECT"
echo "UFFIZZI_USER, UFFIZZI_SERVER, UFFIZZI_PASSWORD, and optionally UFFIZZI_PROJECT"
fi

exec uffizzi "$@"
11 changes: 5 additions & 6 deletions lib/uffizzi/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ module AuthHelper
class << self
def signed_in?
ConfigFile.exists? &&
ConfigFile.option_exists?(:account_id) &&
ConfigFile.option_exists?(:cookie) &&
ConfigFile.option_exists?(:hostname)
ConfigFile.option_has_value?(:account_id) &&
ConfigFile.option_has_value?(:cookie) &&
ConfigFile.option_has_value?(:server)
end

def project_set?
ConfigFile.exists? &&
ConfigFile.option_exists?(:project)
def project_set?(options)
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
!options[:project].nil? || (ConfigFile.exists? && ConfigFile.option_has_value?(:project))
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions lib/uffizzi/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

module Uffizzi
class CLI < Thor
require_relative 'cli/common'

class_option :help, type: :boolean, aliases: HELP_MAPPINGS
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
class_option :project, type: :string

def self.exit_on_failure?
true
end

desc 'version', 'show version'
desc 'version', 'Show Version'
def version
require_relative 'version'
Uffizzi.ui.say(Uffizzi::VERSION)
end

desc 'login [OPTIONS]', 'Login into Uffizzi'
method_option :user, required: true, aliases: '-u'
method_option :hostname, required: true
method_option :server, required: false, aliases: '-s'
method_option :username, required: false, aliases: '-u'
def login
require_relative 'cli/login'
Login.new(options).run
Expand Down
48 changes: 40 additions & 8 deletions lib/uffizzi/cli/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ module Uffizzi
class CLI::Config < Thor
include ApiClient

class << self
def help(_shell, _subcommand)
Cli::Common.show_manual(:config)
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
end
end

desc 'list', 'list'
def list
run('list')
end

desc 'get [PROPERTY]', 'get'
def get(property)
def get_value(property)
run('get', property)
end

Expand All @@ -22,11 +28,20 @@ def set(property, value)
run('set', property, value)
end

desc 'delete [PROPERTY]', 'delete'
def delete(property)
run('delete', property)
desc 'unset [PROPERTY]', 'unset'
def unset(property)
run('unset', property)
end

desc 'setup', 'setup'
def setup
run('setup')
end

map('get-value' => :get_value)

default_task :setup

private

def run(command, property = nil, value = nil)
Expand All @@ -37,11 +52,26 @@ def run(command, property = nil, value = nil)
handle_get_command(property)
when 'set'
handle_set_command(property, value)
when 'delete'
handle_delete_command(property)
when 'unset'
handle_unset_command(property)
when 'setup'
handle_setup_command
end
end

def handle_setup_command
Uffizzi.ui.say("Configure the default properties that will be used to authenticate with your \
\nUffizzi API service and manage previews.\n")
server = Uffizzi.ui.ask('Server: ', default: Uffizzi.configuration.default_server.to_s)
username = Uffizzi.ui.ask('Username: ')
project = Uffizzi.ui.ask('Project: ')
ConfigFile.delete
ConfigFile.write_option(:server, server)
ConfigFile.write_option(:username, username)
ConfigFile.write_option(:project, project)
Uffizzi.ui.say('To login, run: uffizzi login')
end

def handle_list_command
ConfigFile.list
end
Expand All @@ -55,10 +85,12 @@ def handle_get_command(property)

def handle_set_command(property, value)
ConfigFile.write_option(property.to_sym, value)
Uffizzi.ui.say("Updated property [#{property}]")
end

def handle_delete_command(property)
ConfigFile.delete_option(property.to_sym)
def handle_unset_command(property)
ConfigFile.unset_option(property.to_sym)
Uffizzi.ui.say("Unset property [#{property}]")
end
end
end
16 changes: 8 additions & 8 deletions lib/uffizzi/cli/connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def handle_docker_hub
type: Uffizzi.configuration.credential_types[:dockerhub],
}

hostname = ConfigFile.read_option(:hostname)
response = create_credential(hostname, params)
server = ConfigFile.read_option(:server)
response = create_credential(server, params)

if ResponseHelper.created?(response)
print_success_message('DockerHub')
Expand All @@ -57,8 +57,8 @@ def handle_azure
type: Uffizzi.configuration.credential_types[:azure],
}

hostname = ConfigFile.read_option(:hostname)
response = create_credential(hostname, params)
server = ConfigFile.read_option(:server)
response = create_credential(server, params)

if ResponseHelper.created?(response)
print_success_message('ACR')
Expand All @@ -79,8 +79,8 @@ def handle_amazon
type: Uffizzi.configuration.credential_types[:amazon],
}

hostname = ConfigFile.read_option(:hostname)
response = create_credential(hostname, params)
server = ConfigFile.read_option(:server)
response = create_credential(server, params)

if ResponseHelper.created?(response)
print_success_message('ECR')
Expand All @@ -103,8 +103,8 @@ def handle_google(credential_file_path)
type: Uffizzi.configuration.credential_types[:google],
}

hostname = ConfigFile.read_option(:hostname)
response = create_credential(hostname, params)
server = ConfigFile.read_option(:server)
response = create_credential(server, params)

if ResponseHelper.created?(response)
print_success_message('GCR')
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/disconnect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def run(credential_type)
raise Uffizzi::Error.new('Unsupported credential type.')
end

response = delete_credential(ConfigFile.read_option(:hostname), connection_type)
response = delete_credential(ConfigFile.read_option(:server), connection_type)

if ResponseHelper.no_content?(response)
Uffizzi.ui.say("Successfully disconnected #{connection_name(credential_type)} connection")
Expand Down
39 changes: 29 additions & 10 deletions lib/uffizzi/cli/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,53 @@ def initialize(options)
end

def run
password = ENV['UFFIZZI_PASSWORD'] || IO::console.getpass('Enter Password: ')

params = prepare_request_params(password)
response = create_session(@options[:hostname], params)
Uffizzi.ui.say('Login to Uffizzi to your previews.')
7R41N33 marked this conversation as resolved.
Show resolved Hide resolved
server = set_server
username = set_username
password = set_password
params = prepare_request_params(username, password)
response = create_session(server, params)

if ResponseHelper.created?(response)
handle_succeed_response(response)
handle_succeed_response(response, server, username)
else
ResponseHelper.handle_failed_response(response)
end
end

private

def prepare_request_params(password)
def set_server
config_server = ConfigFile.exists? && ConfigFile.option_has_value?(:server) ? ConfigFile.read_option(:server) : nil
@options[:server] || config_server || Uffizzi.ui.ask('Server: ')
end

def set_username
config_username = ConfigFile.exists? && ConfigFile.option_has_value?(:username) ? ConfigFile.read_option(:username) : nil
@options[:username] || config_username || Uffizzi.ui.ask('Username: ')
end

def set_password
ENV['UFFIZZI_PASSWORD'] || Uffizzi.ui.ask('Password: ', echo: false)
end

def prepare_request_params(username, password)
{
user: {
email: @options[:user],
password: password.strip,
email: username,
password: password,
},
}
end

def handle_succeed_response(response)
def handle_succeed_response(response, server, username)
account = response[:body][:user][:accounts].first
return Uffizzi.ui.say('No account related to this email') unless account_valid?(account)

ConfigFile.create(account[:id], response[:headers], @options[:hostname])
ConfigFile.write_option(:server, server)
ConfigFile.write_option(:username, username)
ConfigFile.write_option(:cookie, response[:headers])
ConfigFile.write_option(:account_id, account[:id])
end

def account_valid?(account)
Expand Down
4 changes: 2 additions & 2 deletions lib/uffizzi/cli/logout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def initialize(options)
def run
return Uffizzi.ui.say('You are not logged in') unless Uffizzi::AuthHelper.signed_in?

hostname = ConfigFile.read_option(:hostname)
destroy_session(hostname)
server = ConfigFile.read_option(:server)
destroy_session(server)

ConfigFile.delete
Uffizzi.ui.say('You have been successfully logged out')
Expand Down
Loading