Skip to content

Commit fcd4fef

Browse files
author
CloudHero
committed
Updating history
1 parent 52c8b2e commit fcd4fef

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

hero

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from lib.interactive import interactive_shell
1515
from lib.pyhero import Client
1616
from lib.prompter import *
1717
from lib.utils import (write_to_file, set_keys_to_empty_values,
18-
get_docker_ip_for_environment)
18+
get_docker_ip_for_environment, update_history)
1919

2020
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
2121

@@ -619,4 +619,5 @@ def register(**kwargs):
619619

620620

621621
if __name__ == '__main__':
622+
update_history(cloud_hero)
622623
hero_cli()

lib/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
import constants
2-
import interactive
3-
import pyhero
4-
import prompter
5-
import utils

lib/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
VERBOSE = False
2-
CLI_VERSION = '0.3'
2+
CLI_VERSION = '0.3.1'
33

44
# API-related constants.
55
# https://docs.cloudhero.io
@@ -11,6 +11,7 @@
1111
CLOUD_HERO_DIR = '~/.herorc/'
1212
CLOUD_HERO_SSH_KEY = CLOUD_HERO_DIR + '.ssh/id_rsa_cloudhero'
1313
CLOUD_HERO_TOKEN = CLOUD_HERO_DIR + 'token'
14+
CLOUD_HERO_HISTORY = CLOUD_HERO_DIR + 'history.json'
1415
CLOUD_HERO_CACHE_NODES = CLOUD_HERO_DIR + 'cache/nodes'
1516
CLOUD_HERO_CACHE_ENVIRONMENTS = CLOUD_HERO_DIR + 'cache/enviornments'
1617
CLOUD_HERO_CACHE_OPTIONS = CLOUD_HERO_DIR + 'cache/options'

lib/pyhero.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
'environments': '/environments',
2525
'applications': '/applications',
2626
'swarm': '/swarm',
27-
'feedback': '/feedback'
27+
'feedback': '/feedback',
28+
'history': '/history'
2829
}
2930

3031

@@ -172,6 +173,14 @@ def send_feedback(self, data):
172173
return self._result(self.post_json(ENDPOINTS['feedback'],
173174
data=data))
174175

176+
def send_history(self, data):
177+
try:
178+
return self._result(self.post_json(ENDPOINTS['history'],
179+
data=data))
180+
except:
181+
# Silently continue if /history endpoint is not available.
182+
pass
183+
175184
@cache_to_file(CLOUD_HERO_CACHE_OPTIONS, ttl=CLIENT_CACHE_OPTIONS_TTL)
176185
def show_options(self, endpoint):
177186
"""

lib/utils.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import functools
33
import json
44
import os
5+
import sys
56
import time
67

7-
from constants import CLOUD_HERO_CACHE_OPTIONS, NotFound
8+
from lib.constants import CLOUD_HERO_CACHE_OPTIONS, CLOUD_HERO_HISTORY, NotFound
89

910

1011
def write_to_file(content, file_path, is_json=False):
@@ -68,14 +69,6 @@ def invalidate_cache(*file_paths):
6869
def wrap(func):
6970
@functools.wraps(func)
7071
def wrapper(*args, **kwargs):
71-
for file_path in file_paths:
72-
expanded_file_path = os.path.expanduser(file_path)
73-
try:
74-
os.remove(expanded_file_path)
75-
except OSError as exc:
76-
if exc.errno == errno.ENOENT:
77-
continue
78-
raise
7972
return func(*args, **kwargs)
8073
return wrapper
8174
return wrap
@@ -124,3 +117,20 @@ def get_docker_ip_for_environment(node_details, environment_id):
124117

125118
if environment_found is False:
126119
raise NotFound('Environment {} not found!'.format(environment_id))
120+
121+
122+
def update_history(cloud_hero):
123+
user_command = ' '.join(sys.argv)
124+
timestamp = int(time.time())
125+
content = read_from_file(CLOUD_HERO_HISTORY, is_json=True)
126+
127+
command_history = (user_command, timestamp)
128+
if not content:
129+
content = [command_history]
130+
else:
131+
content.append(command_history)
132+
if len(content) > 10:
133+
cloud_hero.send_history(content)
134+
content = None
135+
136+
write_to_file(content, CLOUD_HERO_HISTORY, is_json=True)

0 commit comments

Comments
 (0)