scyllax_cli/
opt.rs

1use std::ops::{Deref, Not};
2
3use clap::{Args, Parser};
4use clap_complete::Shell;
5
6/// Parses options for the CLI.
7#[derive(Parser, Debug)]
8#[clap(version, about, author)]
9pub struct Opt {
10    /// The parsed command
11    #[clap(subcommand)]
12    pub command: Command,
13}
14
15#[derive(Parser, Debug)]
16pub enum Command {
17    #[clap(alias = "mig")]
18    Migrate(MigrateOpt),
19
20    /// Generate shell completions for the specified shell
21    Completions { shell: Shell },
22}
23
24/// Group of commands for creating and running migrations.
25#[derive(Parser, Debug)]
26pub struct MigrateOpt {
27    #[clap(subcommand)]
28    pub command: MigrateCommand,
29}
30
31#[derive(Parser, Debug)]
32pub enum MigrateCommand {
33    /// Create a new migration with the given description.
34    ///
35    /// A version number will be automatically assigned to the migration.
36    ///
37    /// Example: scyllax-cli mig add create users table
38    Add {
39        description: String,
40
41        #[clap(flatten)]
42        source: Source,
43    },
44
45    /// Creates the `scyllax_migrations` keyspace.
46    Init {
47        #[clap(flatten)]
48        connect_opts: ConnectOpts,
49    },
50
51    /// Run all pending migrations.
52    Run {
53        #[clap(flatten)]
54        source: Source,
55
56        /// Run only the next pending migration
57        #[clap(long)]
58        next: bool,
59
60        #[clap(flatten)]
61        connect_opts: ConnectOpts,
62    },
63
64    /// Revert the latest migration with a down file.
65    Revert {
66        #[clap(flatten)]
67        source: Source,
68
69        #[clap(flatten)]
70        connect_opts: ConnectOpts,
71    },
72
73    /// List all available migrations.
74    Info {
75        #[clap(flatten)]
76        source: Source,
77
78        #[clap(flatten)]
79        connect_opts: ConnectOpts,
80    },
81}
82
83/// Argument for the migration scripts source.
84#[derive(Args, Debug)]
85pub struct Source {
86    /// Path to folder containing migrations.
87    #[clap(long, default_value = "migrations")]
88    source: String,
89}
90
91impl Deref for Source {
92    type Target = String;
93
94    fn deref(&self) -> &Self::Target {
95        &self.source
96    }
97}
98
99/// Argument for the database URL.
100#[derive(Args, Debug)]
101pub struct ConnectOpts {
102    /// Location of the scylla nodes, by default will be read from the SCYLLA_NODES env var
103    #[clap(long, short = 'N', env)]
104    pub scylla_nodes: String,
105
106    /// The keyspace to store migrations information.
107    #[clap(long, short = 'K', default_value = "scyllax_migrations")]
108    pub keyspace: String,
109
110    /// The maximum time, in seconds, to try connecting to the database server before
111    /// returning an error.
112    #[clap(long, default_value = "10")]
113    pub connect_timeout: u64,
114}
115
116/// Argument for ignoring applied migrations that were not resolved.
117#[derive(Args, Copy, Clone, Debug)]
118pub struct IgnoreMissing {
119    /// Ignore applied migrations that are missing in the resolved migrations
120    #[clap(long)]
121    ignore_missing: bool,
122}
123
124impl Deref for IgnoreMissing {
125    type Target = bool;
126
127    fn deref(&self) -> &Self::Target {
128        &self.ignore_missing
129    }
130}
131
132impl Not for IgnoreMissing {
133    type Output = bool;
134
135    fn not(self) -> Self::Output {
136        !self.ignore_missing
137    }
138}