Skip to content
Open
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
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# The GitHub editor is 127 chars wide
flake8 . --count --max-complexity=10 --max-line-length=127 --statistics
# - name: Test with pytest
# run: |
# pip install pytest
# pytest
- name: Test with pytest
run: |
pip install -r requirements-test.txt
pytest -v tests/
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ one.spec
one/__init__.pyc
one/__main__.pyc
one/one.pyc
env
.venv
one_cli.egg-info
.pytest_cache

one.yaml
.one.workspace
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
all: .venv install


.venv:
virtualenv -p /usr/bin/python3 .venv


install: .venv
.venv/bin/pip install --editable .


clean:
rm -rf .venv
rm -rf build
rm -rf dist
rm -rf one_cli.egg-info
rm -rf .pytest_cache
rm -rf __pycache__
rm -rf one.spec
find -iname "*.pyc" -delete


run: install
.venv/bin/python cli.py $(filter-out $@,$(MAKECMDGOALS))


build: .venv
.venv/bin/pip install -U PyInstaller
.venv/bin/pyinstaller --clean --hidden-import one.__main__ cli.py --onefile --noconsole -n one


.requirements-test-lint:
.venv/bin/pip install -r requirements-test.txt


test: .venv .requirements-test-lint
.venv/bin/pytest -v tests/


flake8: .venv .requirements-test-lint
.venv/bin/flake8 . --count --max-complexity=10 --max-line-length=127 --statistics --exclude .venv
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,30 @@ workspaces:

#### Dependencies
- Python 3
- Make
- virtualenv

#### Python Virtual Environment
#### Install dependencies
```bash
# Create environment
python3 -m venv env

# To activate the environment
source env/bin/activate

# When you finish you can exit typing
deactivate
make install
```

#### Install dependencies
#### Run tests and flake8
To run the test suite:
```bash
make test
```

To run the flake8 lint check:
```bash
pip3 install --editable .
make flake8
```

#### Manualy generate binary
> Notice that when the command finishes successfully two folders will be generated in the project (**build** and **dist**). The CLI binary file can be found at **dist**.

```bash
pyinstaller --clean --hidden-import one.__main__ cli.py --onefile --noconsole -n one
make build
```

## Plugin System
Expand Down
1 change: 1 addition & 0 deletions one/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

CLI_ROOT = home + '/.one'
CONFIG_FILE = './one.yaml'
DEFAULT_WORKSPACE = '.one.workspace'
35 changes: 2 additions & 33 deletions one/commands/init.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
import click
from PyInquirer import prompt
import yaml
from one.utils.prompt import style
from one.__init__ import CONFIG_FILE
from one.prompt.init import CREATION_QUESTION, IMAGE_QUESTIONS, WORKSPACE_QUESTIONS
from one.controller.init import InitController


@click.command(help='Create config file for CLI in current directory.')
def init():
create_answer = prompt(CREATION_QUESTION, style=style)
create_workspace = create_answer['create'].lower()
workspaces = {}
if create_workspace == 'y' or not create_workspace:
image_answers = prompt(IMAGE_QUESTIONS, style=style)
images = {
'terraform': image_answers['terraform'],
'gsuite': image_answers['gsuite'],
'azure': image_answers['azure']
}

while True:
workspace_answers = prompt(WORKSPACE_QUESTIONS, style=style)
if workspace_answers['assume_role'].lower() == 'y' or not workspace_answers['assume_role']:
assume_role = True
else:
assume_role = False
workspace = {
'aws-role': workspace_answers['AWS_ROLE'],
'aws-account-id': workspace_answers['AWS_ACCOUNT_ID'],
'assume-role': assume_role
}
workspaces[workspace_answers['WORKSPACE']] = workspace
if workspace_answers['new_workspace'].lower() == 'n':
break
with open(CONFIG_FILE, 'w') as file:
content = {'images': images, 'workspaces': workspaces}
yaml.dump(content, file)
InitController().init()
32 changes: 6 additions & 26 deletions one/commands/workspace.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
from one.utils.prompt import style
from PyInquirer import prompt
from one.utils.config import get_workspaces
from one.controller.workspace import WorkspaceController

workspace_controller = WorkspaceController()


@click.group(help='Manage workspaces.')
Expand All @@ -10,30 +10,10 @@ def workspace():


@workspace.command(name='list', help='List all workspaces.')
def list_workspaces():
workspaces = get_workspaces()
for workspace in workspaces:
click.echo('- ' + workspace)
def _list():
workspace_controller._list()


@workspace.command(help='Change environment variables to another workspace.')
def change():
workspaces_obj = []
workspaces = get_workspaces()
for workspace in workspaces:
workspaces_obj.append({'name': workspace})

questions = [
{
'type': 'list',
'message': 'Select workspace',
'name': 'workspace',
'choices': workspaces_obj
}
]

answers = prompt(questions, style=style)

f = open('.one.workspace', 'w')
f.write('WORKSPACE=' + answers['workspace'] + '\n')
f.close()
workspace_controller.change()
59 changes: 59 additions & 0 deletions one/controller/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import yaml
from PyInquirer import prompt
from one.__init__ import CONFIG_FILE
from one.prompt.init import CREATION_QUESTION, IMAGE_QUESTIONS, WORKSPACE_QUESTIONS
from one.utils.prompt import style


class InitController():

workspaces = {}
images = {}
create_workspace = 'y'

def __init__(self):
pass

def init(self):
try:
self.prompt_create_workspace_questions()

if self.create_workspace == 'y':
self.prompt_image_questions()
self.prompt_credential_questions()
self.write_config()
except KeyError:
raise SystemExit

def prompt_create_workspace_questions(self):
answer = prompt(CREATION_QUESTION, style=style)
self.create_workspace = answer['create'].lower()

def prompt_image_questions(self):
image_answers = prompt(IMAGE_QUESTIONS, style=style)
self.images = {
'terraform': image_answers['terraform'],
'azure': image_answers['azure'],
'gsuite': image_answers['gsuite']
}

def prompt_credential_questions(self):
while True:
workspace_answers = prompt(WORKSPACE_QUESTIONS, style=style)
if workspace_answers['assume_role'].lower() == 'y':
assume_role = True
else:
assume_role = False
workspace = {
'aws-role': workspace_answers['AWS_ROLE'],
'aws-account-id': workspace_answers['AWS_ACCOUNT_ID'],
'assume-role': assume_role
}
self.workspaces[workspace_answers['WORKSPACE']] = workspace
if workspace_answers['new_workspace'].lower() == 'n':
break

def write_config(self):
with open(CONFIG_FILE, 'w') as file:
content = {'images': self.images, 'workspaces': self.workspaces}
yaml.dump(content, file)
63 changes: 63 additions & 0 deletions one/controller/workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import yaml
import click
from PyInquirer import prompt
from os import path
from one.__init__ import CONFIG_FILE, DEFAULT_WORKSPACE
from one.utils.prompt import style


class WorkspaceController():

workspaces = []

def __init__(self):
pass

def _list(self):
self.get_workspaces()
for workspace in self.workspaces:
click.echo('- ' + workspace)

def prompt_workspaces_list(self, workspaces_obj):
questions = [
{
'type': 'list',
'message': 'Select workspace',
'name': 'workspace',
'choices': workspaces_obj
}
]

answers = prompt(questions, style=style)
return answers['workspace']

def change(self):
workspaces_obj = self.format_workspace_list()
if workspaces_obj:
try:
selected_workspace = self.prompt_workspaces_list(workspaces_obj)
content = 'WORKSPACE=%s\n' % (selected_workspace)
self.write_default_workspace(content)
except IndexError:
raise SystemExit

def get_workspaces(self):
if path.exists(CONFIG_FILE):
with open(CONFIG_FILE) as file:
docs = yaml.load(file, Loader=yaml.BaseLoader)
for workspace_key in docs['workspaces'].keys():
self.workspaces.append(workspace_key)
file.close()

return self.workspaces

def format_workspace_list(self):
workspaces_obj = []
self.get_workspaces()
for workspace in self.workspaces:
workspaces_obj.append({'name': workspace})
return workspaces_obj

def write_default_workspace(self, content):
with open(DEFAULT_WORKSPACE, 'w') as file:
file.write(content)
12 changes: 0 additions & 12 deletions one/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ def get_config_value(key, default=None):
return value


def get_workspaces():
workspaces = []
if path.exists(CONFIG_FILE):
with open(CONFIG_FILE) as file:
docs = yaml.load(file, Loader=yaml.BaseLoader)
for workspace_key in docs['workspaces'].keys():
workspaces.append(workspace_key)
file.close()

return workspaces


def get_workspace_value(workspace_name, variable, default=None):
value = default
if path.exists(CONFIG_FILE):
Expand Down
Loading