diff --git a/bin/is b/bin/is index 0890c48ab9f8dd52137dcefe7bda7fa5a07fe3fb..e1e0a5826f1148fc9809f1554cd9f3d4f7c6a58b 100755 --- a/bin/is +++ b/bin/is @@ -327,11 +327,10 @@ def c_install(args): # select image to install repoman = load_repositories(args) image, repo = next(get_images([args.pattern], repoman, min=1, max=1)) - if repo: - # Print repo MOTD - arrow("Repository %s MOTD" % repo.config.name) - repo.motd() - # Print setup information + if repo is not None: + arrow("Repository message") + out(repo.motd, endl="") + # print setup information arrow(u"Installing %s v%s" % (image.name, image.version)) # let's go dt = image.run(args.parser, subparser, run_setup=not args.dry_run) @@ -354,25 +353,23 @@ def c_list(args): def c_motd(args): ''' - Display (and edit) repository's MOTD + Show and set repository's message ''' repoman = load_repositories(args) - repo = repoman[args.repository] - arrow("Current MOTD for %s:" % repo.config.name) - repo.motd() - if args.edit: - # check local repository - if not repo.local: - raise ISError(u"Repository must be local") - arrow("New MOTD (end with a blank line):") - motd = "" - # raw_input return bytestring - data = raw_input().decode(sys.stdin.encoding) - motd += data - while data: - data = raw_input().decode(sys.stdin.encoding) - motd += u"\n%s" % data - repo.motd(motd[:-1]) + if args.file: + args.set = open(args.file, "rb").read() + elif args.remove: + args.set = "" + arrowlevel(1) + for reponame in repoman.select_repositories(args.repository): + arrow(reponame, -1) + try: + if args.set is not None: + repoman[reponame].setmotd(args.set) + else: + out(repoman[reponame].motd) + except IndexError as e: + raise ISError(e) def c_move(args): ''' @@ -675,9 +672,11 @@ def arg_parser_init(): p.set_defaults(func=c_list) # motd command parser p = subparser.add_parser("motd", help=c_motd.__doc__.lower()) - p.add_argument("repository", help="repository to display/edit MOTD") - p.add_argument("--edit", action="store_true", - help="edit repository's MOTD") + g = p.add_mutually_exclusive_group() + g.add_argument("-r", "--remove", action="store_true", help="remove the motd") + g.add_argument("-s", "--set", help="set the motd from command line") + g.add_argument("-f", "--file", help="set the motd from a file") + p.add_argument("repository", nargs="*", default=[u"*"], help="image repository") p.set_defaults(func=c_motd) # move command parser p = subparser.add_parser("move", help=c_move.__doc__.lower()) diff --git a/doc/is.1.rst b/doc/is.1.rst index cd908f6a52e283a43f3ecfcbd3833be18a4c5d20..236a28bf4cf130edde38786491e9c41b055b8c8a 100644 --- a/doc/is.1.rst +++ b/doc/is.1.rst @@ -267,11 +267,20 @@ list [-h] [-A] [-d] [-D] [-f] [-j] [-i] [-l] [-m] [-s] [-u] [...] display image url -motd [-h] [--edit] *repository* - Display MOTD of a repository +motd [-h] [-f] [-r] [-s] *repository* + Show or set the message of the day (MOTD) of repository. + This message is only displayed during installation of an image from this repository. - --edit - edit the MOTD of the repository + -f, --file *FILE* + set the repository MOTD from file *FILE*. + + -s, --set *MESSAGE* + set the repository MOTD from command line argument *MESSAGE*. + + -r, --remove + remove the repository MOTD. + + MOTD are supported by repository version >= 2.0. move [-h] [-f] ... *repository* diff --git a/installsystems/repository.py b/installsystems/repository.py index e5970c0d699133a281444e3297750264b20441c2..e9b2da3db0a2bbcfdd2a50872cd78417dcc46dbc 100644 --- a/installsystems/repository.py +++ b/installsystems/repository.py @@ -586,25 +586,24 @@ class Repository(object): finally: # Remove dummy repository shutil.rmtree(tmpdir) + @property + def motd(self): + ''' + Return repository message of the day + ''' + motd = self.db.ask("SELECT motd FROM repository").fetchone()[0] + return None if len(motd) == 0 else motd - def motd(self, new_motd=None): + def setmotd(self, value=""): ''' - Display and edit repository motd + Set repository message of the day ''' - if new_motd is None: - try: - motd = self.db.ask("SELECT motd FROM repository").fetchone() - except: - motd = None - if motd is None: - raise ISError("Unable to retrieve %s's MOTD" % self.config.name) - out(motd[0]) - else: - # check local repository - if not self.local: - raise ISError(u"Repository must be local") - self.db.ask("UPDATE repository SET motd = ?", (new_motd,)) - self.update_last() + # check local repository + if not self.local: + raise ISError(u"Repository must be local") + arrow("Updating motd") + self.db.ask("UPDATE repository SET motd = ?", (value,)) + self.update_last() class Repository_v1(Repository): @@ -671,14 +670,21 @@ class Repository_v1(Repository): images.append(d) return images - def motd(self, new_motd=None): + @property + def motd(self): ''' - Display and edit repository motd + Return repository message of the day. + Repository v1 don't have message of day ''' - out('') - if new_motd: - warn("[%s] repository v1, unable to edit MOTD." % self.config.name) + return None + def setmotd(self, value=""): + ''' + Don't set repository message of the day. Not supported by v1. + ''' + # check local repository + warn(u"Repository v1 doesn't support motd. Unable to set") + return class RepositoryManager(object): ''' diff --git a/misc/bash-completion b/misc/bash-completion index 70cda20c6ab8af9440bde6e791f68c452c38de8a..1dd07853c8ec063ad376951a7866c71164cde2f1 100644 --- a/misc/bash-completion +++ b/misc/bash-completion @@ -157,7 +157,7 @@ _is() { _remote_image ;; motd) - [[ "$cur" == -* ]] && _opt '-h --help --edit' && return 0 + [[ "$cur" == -* ]] && _opt '-h --help -r --remove -f --files -s --set' && return 0 _repo ;; move) diff --git a/misc/zsh-completion b/misc/zsh-completion index 968470127c19b225f4648ceb1e02b4064d12386f..d0fc472e749c39d2e6ae485e70ef5bce6597e487 100644 --- a/misc/zsh-completion +++ b/misc/zsh-completion @@ -264,8 +264,10 @@ _is() { ;; (motd) args+=( - '1:repository:_installsystems_repo' - "--edit[edit repository's MOTD]" + '(-f --files)'{-f,--files}'[set the motd from a file]: motd:_files' + '(-s --set)'{-s,--set}'[set the motd from command line]' + '(-r --remove)'{-r,--remove}'[remove the motd]' + '*:repository:_installsystems_local_repo' ) ;; (move)