aboutsummaryrefslogtreecommitdiff
path: root/fietsboek/updater/cli.py
blob: a6ea6c1d916800c6101ce08909677591ddc9a7e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Updater for Fietsboek.

While this script does not download and install the latest Fietsboek version
itself (as this step depends on where you got Fietsboek from), it takes care of
managing migrations between Fietsboek versions. In particular, the updater
takes care of running the database migrations, migrating the data directory and
migrating the configuration.
"""
import click

from . import Updater


# We keep this as a separate option that is added to each subcommand as Click
# (unlike argparse) cannot handle "--help" without the required arguments of
# the parent (so "fietsupdate update --help" would error out)
# See also
# https://github.com/pallets/click/issues/295
# https://github.com/pallets/click/issues/814
config_option = click.option(
    "-c", "--config",
    type=click.Path(exists=True, dir_okay=False),
    required=True,
    help="Path to the Fietsboek configuration file",
)


def user_confirm(verb):
    """Ask the user for confirmation before proceeding.

    Aborts the program if no confirmation is given.

    :param verb: The verb to use in the text.
    :type verb: str
    """
    click.secho("Warning:", fg="yellow")
    click.echo(
        f"{verb} *may* cause data loss. Make sure to have a backup of the "
        "database and the data directory!"
    )
    click.echo("For more information, please consult the documentation.")
    click.confirm("Proceed?", abort=True)


@click.group(
    help=__doc__,
    context_settings={'help_option_names': ['-h', '--help']},
)
def cli():
    """CLI main entry point."""


@cli.command("update")
@config_option
@click.option(
    "-f", "--force",
    is_flag=True,
    help="Skip the safety question and just run the update",
)
@click.argument("version", required=False)
@click.pass_context
def update(ctx, config, version, force):
    """Run the update process.

    Make sure to consult the documentation and ensure that you have a backup
    before starting the update, to prevent any data loss!

    VERSION specifies the version you want to update to. Leave empty to choose
    the latest version.
    """
    updater = Updater(config)
    updater.load()
    if version and not updater.exists(version):
        ctx.fail(f"Version {version!r} not found")

    version_str = ", ".join(updater.current_versions())
    click.echo(f"Current version: {version_str}")
    if not version:
        heads = updater.heads()
        if len(heads) != 1:
            ctx.fail("Ambiguous heads, please specify the version to update to")

        version = heads[0]

    click.echo(f"Selected version: {version}")
    if updater.has_applied(version):
        click.secho("Nothing to do", fg="green")
        ctx.exit()

    if not force:
        user_confirm("Updating")
    updater.upgrade(version)
    version_str = ", ".join(updater.current_versions())
    click.secho(f"Update succeeded, version: {version_str}", fg="green")


@cli.command("downgrade")
@config_option
@click.option(
    "-f", "--force",
    is_flag=True,
    help="Skip the safety question and just run the downgrade",
)
@click.argument("version")
@click.pass_context
def downgrade(ctx, config, version, force):
    """Run the downgrade process.

    Make sure to consult the documentation and ensure that you have a backup
    before starting the downgrade, to prevent any data loss!

    VERSION specifies the version you want to downgrade to.
    """
    updater = Updater(config)
    updater.load()
    if version and not updater.exists(version):
        ctx.fail(f"Version {version!r} not found")

    version_str = ", ".join(updater.current_versions())
    click.echo(f"Current version: {version_str}")
    click.echo(f"Downgrade to version {version}")
    if updater.has_applied(version, backward=True):
        click.secho("Nothing to do", fg="green")
        ctx.exit()
    if not force:
        user_confirm("Downgrading")
    updater.downgrade(version)
    version_str = ", ".join(updater.current_versions())
    click.secho(f"Downgrade succeeded, version: {version_str}", fg="green")


@cli.command("revision", hidden=True)
@config_option
@click.argument("revision_id", required=False)
def revision(config, revision_id):
    """Create a new revision.

    This automatically populates the revision dependencies and alembic
    versions, based on the current state.

    This command is useful for developers who work on Fietsboek.
    """
    updater = Updater(config)
    updater.load()
    current = updater.current_versions()
    heads = updater.heads()
    if not any(version in heads for version in current):
        click.secho("Warning:", fg="yellow")
        click.echo("Your current revision is not a head. This will create a branch!")
        click.echo(
            "If this is not what you intended, make sure to update to the latest "
            "version first before creating a new revision."
        )
        click.confirm("Proceed?", abort=True)
    filename = updater.new_revision(revision_id)
    click.echo(f"Revision saved to {filename}")


@cli.command("status")
@config_option
def status(config):
    """Display information about the current version and available updates."""
    updater = Updater(config)
    updater.load()
    current = updater.current_versions()
    heads = updater.heads()
    click.secho("Current versions:", fg="yellow")
    if current:
        has_unknown = False
        for i in current:
            if updater.exists(i):
                click.echo(i)
            else:
                click.echo(f"{i} [unknown]")
                has_unknown = True
        if has_unknown:
            click.echo("[*] Your version contains revisions that are unknown to me")
            click.echo("[*] This can happen if you apply an update and then downgrade the code")
            click.echo("[*] Make sure to keep your code and data in sync!")
    else:
        click.secho("No current version", fg="red")
    click.secho("Available updates:", fg="yellow")
    updates = set(heads) - set(current)
    if updates:
        for i in updates:
            click.echo(i)
    else:
        click.secho("All updates applied!", fg="green")


@cli.command("help", hidden=True)
@click.pass_context
@click.argument("subcommand", required=False)
def help_(ctx, subcommand):
    """Shows the help for a given subcommand.

    This is equivalent to using the --help option.
    """
    if not subcommand:
        click.echo(cli.get_help(ctx.parent))
        return
    cmd = cli.get_command(ctx, subcommand)
    if cmd is None:
        click.echo(f"Error: Command {subcommand} not found")
    else:
        # Create a new context so that the usage shows "fietsboek subcommand"
        # instead of "fietsboek help"
        with click.Context(cmd, ctx.parent, subcommand) as subcontext:
            click.echo(cmd.get_help(subcontext))


if __name__ == "__main__":
    cli()