Add config command

This commit is contained in:
Arinerron 2018-02-22 18:25:05 -08:00
parent 9998cf6a79
commit c87bf15146
No known key found for this signature in database
GPG Key ID: 99383627861C62F0
2 changed files with 27 additions and 5 deletions

View File

@ -27,7 +27,7 @@ def get(key, default = None):
''' '''
Gets the key from configuration, or returns `default` Gets the key from configuration, or returns `default`
''' '''
if key in get_config(): if is_set(key):
return get_config()[key] return get_config()[key]
return default return default
@ -42,6 +42,9 @@ def set(key, value = None, savefile = False):
if savefile: if savefile:
save() save()
def is_set(key):
return key in get_config() and not get_config()[key] is None
def check(): def check():
''' '''
Checks if the configuration file exists, creates it if not Checks if the configuration file exists, creates it if not
@ -64,7 +67,7 @@ def save():
check() check()
try: try:
with open(get_config_file(), 'w', encoding="utf8") as configfile: with open(get_config_file(), 'w', encoding="utf8") as configfile:
json.dump(get_config(), configfile) json.dump(get_config(), configfile, indent=2, sort_keys=True)
except: except:
logger.warn('Failed to write to configuration file.') logger.warn('Failed to write to configuration file.')

View File

@ -120,13 +120,14 @@ class Onionr:
def getCommands(self): def getCommands(self):
return { return {
'help': self.showHelp,
'version': self.version,
'config': self.configure,
'start': self.start, 'start': self.start,
'stop': self.killDaemon, 'stop': self.killDaemon,
'version': self.version, 'stats': self.showStats,
'listpeers': self.listPeers, 'listpeers': self.listPeers,
'list-peers': self.listPeers, 'list-peers': self.listPeers,
'stats': self.showStats,
'help': self.showHelp,
'': self.showHelpSuggestion, '': self.showHelpSuggestion,
'addmsg': self.addMessage, 'addmsg': self.addMessage,
'addmessage': self.addMessage, 'addmessage': self.addMessage,
@ -142,6 +143,7 @@ class Onionr:
return { return {
'help': 'Displays this Onionr help menu', 'help': 'Displays this Onionr help menu',
'version': 'Displays the Onionr version', 'version': 'Displays the Onionr version',
'config': 'Configures something and adds it to the file',
'start': 'Starts the Onionr daemon', 'start': 'Starts the Onionr daemon',
'stop': 'Stops the Onionr daemon', 'stop': 'Stops the Onionr daemon',
'stats': 'Displays node statistics', 'stats': 'Displays node statistics',
@ -152,6 +154,23 @@ class Onionr:
'gui': 'Opens a graphical interface for Onionr' 'gui': 'Opens a graphical interface for Onionr'
} }
def configure(self):
'''
Displays something from the configuration file, or sets it
'''
if len(sys.argv) >= 4:
config.reload()
config.set(sys.argv[2], sys.argv[3], True)
logger.debug('Configuration file updated.')
elif len(sys.argv) >= 3:
config.reload()
logger.info(logger.colors.bold + sys.argv[2] + ': ' + logger.colors.reset + str(config.get(sys.argv[2], logger.colors.fg.red + 'Not set.')))
else:
logger.info(logger.colors.bold + 'Get a value: ' + logger.colors.reset + sys.argv[0] + ' ' + sys.argv[1] + ' <key>')
logger.info(logger.colors.bold + 'Set a value: ' + logger.colors.reset + sys.argv[0] + ' ' + sys.argv[1] + ' <key> <value>')
def execute(self, argument): def execute(self, argument):
''' '''
Executes a command Executes a command