From a35abdf37e9a6032c2f6e76d71c1d8c4beab3eb0 Mon Sep 17 00:00:00 2001 From: shagi Date: Sat, 7 Nov 2020 10:30:29 +0100 Subject: [PATCH] Use configuration file --- negromate/web/commands/build.py | 18 +++++++++++------ negromate/web/commands/ipfs.py | 30 +++++++++++++++++++++-------- negromate/web/commands/rsync.py | 34 ++++++++++++++++++++------------- negromate/web/commands/run.py | 28 ++++++++++++++++++--------- 4 files changed, 74 insertions(+), 36 deletions(-) mode change 100755 => 100644 negromate/web/commands/ipfs.py mode change 100755 => 100644 negromate/web/commands/rsync.py diff --git a/negromate/web/commands/build.py b/negromate/web/commands/build.py index eddaf88..c514b69 100644 --- a/negromate/web/commands/build.py +++ b/negromate/web/commands/build.py @@ -6,16 +6,22 @@ name = 'build' help_text = 'Generate static website' -def options(parser): +def options(parser, config, **kwargs): parser.add_argument( - 'song_folder', help="Folder with the song database.") + '-s', '--song_folder', type=Path, + default=config['global']['song_folder'], + help="Folder with the song database, defaults to {}".format( + config['global']['song_folder'])) parser.add_argument( - 'lyrics_file', help="File with the lyrics of the songs.") + '-l', '--lyrics_file', type=Path, + default=config['global']['lyrics_file'], + help="File with the lyrics of the songs, defaults to {}".format( + config['global']['lyrics_file'])) -def run(args): +def run(args, **kwargs): builder = Builder( - Path(args.song_folder), - Path(args.lyrics_file), + Path(args.song_folder).expanduser(), + Path(args.lyrics_file).expanduser(), ) builder.build() diff --git a/negromate/web/commands/ipfs.py b/negromate/web/commands/ipfs.py old mode 100755 new mode 100644 index d711115..8c06254 --- a/negromate/web/commands/ipfs.py +++ b/negromate/web/commands/ipfs.py @@ -5,21 +5,28 @@ import urllib.request name = 'ipfs' help_text = 'Upload the web to IPFS' +initial_config = { + 'api': 'http://negromate.rocks:5001', + 'pinfile': '~/.negromate/ipfs.hash', +} -def options(parser): +def options(parser, config, **kwargs): parser.add_argument( - 'song_folder', type=Path, - help="Folder with the song database.") + '-s', '--song_folder', type=Path, + default=config['global']['song_folder'], + help="Folder with the song database, defaults to {}".format( + config['global']['song_folder'])) parser.add_argument( - '-a', '--api', default='http://localhost:5001', - help="IPFS API server, defaults to http://localhost:5001.") + '-a', '--api', default=config[name]['api'], + help="IPFS API server, defaults to {}.".format(config[name]['api'])) parser.add_argument( - '-p', '--pinfile', default='~/.negromate/ipfs.hash', type=Path, - help="file to store the current ipfs hash, defaults to ~/.negromate/ipfs.") + '-p', '--pinfile', default=config[name]['pinfile'], type=Path, + help="file to store the current ipfs hash, defaults to {}".format( + config[name]['pinfile'])) -def run(args): +def run(args, **kwargs): # add to local command = [ "ipfs", @@ -37,6 +44,13 @@ def run(args): ) urllib.request.urlopen(url) + # update ipns on server + url = "{}/api/v0/name/publish?arg={}&resolve=yes".format( + args.api, + final_hash, + ) + urllib.request.urlopen(url) + # read previous hash and update value pinfile = args.pinfile.expanduser() if pinfile.exists(): diff --git a/negromate/web/commands/rsync.py b/negromate/web/commands/rsync.py old mode 100755 new mode 100644 index 45579e3..bdc47fe --- a/negromate/web/commands/rsync.py +++ b/negromate/web/commands/rsync.py @@ -3,28 +3,36 @@ from pathlib import Path name = 'rsync' help_text = 'Sincronize the static web with the server' +initial_config = { + 'host': 'negromate.rocks', + 'user': 'root', + 'port': '22', + 'destination': "/var/www/html", +} -def options(parser): +def options(parser, config, **kwargs): parser.add_argument( - 'song_folder', type=Path, - help="Folder with the song database.") + '-s', '--song_folder', type=Path, + default=config['global']['song_folder'], + help="Folder with the song database, defaults to {}".format( + config['global']['song_folder'])) parser.add_argument( - 'host', - help="Target server.") + '-H', '--host', default=config[name]['host'], + help="Target server, defaults to {}.".format(config[name]['host'])) parser.add_argument( - '-u', '--user', default="root", - help="User in the server, defaults to root.") + '-u', '--user', default=config[name]['user'], + help="User in the server, defaults to {}.".format(config[name]['user'])) parser.add_argument( - '-p', '--port', default=22, type=int, - help="Port of the ssh server, defaults to 22.") + '-p', '--port', default=config[name]['port'], type=int, + help="Port of the ssh server, defaults to {}.".format(config[name]['port'])) parser.add_argument( - '-d', '--destination', default="/var/www/html", - help="Folder of the server, defaults to /var/www/html") + '-d', '--destination', default=config[name]['destination'], + help="Folder of the server, defaults to {}".format(config[name]['destination'])) -def run(args): - contents = str(args.song_folder) + '/' +def run(args, **kwargs): + contents = str(args.song_folder.expanduser()) + '/' destination = "{user}@{host}:{folder}".format( user=args.user, host=args.host, diff --git a/negromate/web/commands/run.py b/negromate/web/commands/run.py index 6bf76f5..1d18563 100644 --- a/negromate/web/commands/run.py +++ b/negromate/web/commands/run.py @@ -1,26 +1,36 @@ -from http.server import test, SimpleHTTPRequestHandler from functools import partial +from http.server import test, SimpleHTTPRequestHandler +from pathlib import Path name = 'run' help_text = 'Start web server to test the website' +initial_config = { + 'port': '8000', + 'bind': '', +} -def options(parser): +def options(parser, config, **kwargs): parser.add_argument( - 'song_folder', help="Folder with the song database.") + '-s', '--song_folder', type=Path, + default=config['global']['song_folder'], + help="Folder with the song database, defaults to {}".format( + config['global']['song_folder'])) parser.add_argument( - 'port', action='store', default=8000, type=int, nargs='?', - help='Specify alternate port [default: 8000]') + '-p', '--port', default=config[name]['port'], type=int, + help='Specify alternate port, defaults to {}'.format(config[name]['port'])) parser.add_argument( - '--bind', '-b', default='', metavar='ADDRESS', - help='Specify alternate bind address [default: all interfaces]') + '--bind', '-b', default=config[name]['bind'], metavar='ADDRESS', + help='Specify alternate bind address, defaults to {}'.format( + config[name]['bind'] or 'all interfaces', + )) -def run(args): +def run(args, **kwargs): Handler = partial( SimpleHTTPRequestHandler, - directory=args.song_folder, + directory=str(args.song_folder.expanduser()), ) test(HandlerClass=Handler, port=args.port, bind=args.bind)