Skip to main content

sql_fun_core/args/
init_args.rs

1use std::path::PathBuf;
2
3use clap::{CommandFactory, ValueHint};
4
5use url::Url;
6
7/// Initialize `sql-fun` environments
8#[derive(clap::Parser, Debug, Clone)]
9pub struct InitializeArgs {
10    /// Disable prompts (assume yes)
11    ///
12    /// Run non-interactively. Assume "yes" to all prompts, including creating
13    /// directories and overwriting files when required.
14    ///
15    /// Use with care.
16    ///
17    #[arg(long = "yes", short = 'y')]
18    assume_yes: bool,
19
20    /// `sql-fun` Home directory path. (optional)
21    ///
22    /// Path to the sql-fun home directory.
23    ///
24    /// This is where catalogs, extensions, and configuration files are stored.
25    /// If not specified, defaults to "${HOME}/.sql-fun".
26    ///
27    /// [default: ${HOME}/.sql-fun]
28    ///
29    #[arg(long, short = 'H', value_hint = ValueHint::DirPath)]
30    sql_fun_home: Option<PathBuf>,
31
32    /// Database connection URL (e.g., <postgres://user:pass@host:5432/dbname>).
33    ///
34    /// If provided, `sql-fun` will connect to the database and build the
35    /// built-in object catalog directly from the live server.
36    ///
37    /// This bypasses artifact download (see --github-repo / --sql-fun-version).
38    ///
39    /// [default: download `sql-fun` release artifact and use it. ]
40    ///
41    #[arg(long, conflicts_with_all = ["github_repo", "sql_fun_version"])]
42    database_url: Option<Url>,
43
44    /// Source repository (owner/repo) for release artifacts containing
45    ///   prebuilt catalogs and extension metadata.
46    #[arg(long, default_value = "kazuk/sql-fun")]
47    github_repo: String,
48
49    /// `sql-fun` version to use when downloading artifacts (semver).
50    ///
51    /// If omitted, the latest stable release is used.
52    ///
53    /// [default: latest stable release]
54    #[arg(long)]
55    sql_fun_version: Option<String>,
56
57    /// Limit initialization to the specified database engine versions.
58    ///
59    /// Accepts multiple values (repeatable) or comma-separated lists.
60    ///
61    /// [default: install ALL engine versions in artifact/source]
62    ///
63    #[arg(long, value_delimiter = ',', num_args = 1..)]
64    engine_versions: Option<Vec<String>>,
65
66    /// Limit initialization to the specified `PostgreSQL` extensions.
67    ///
68    /// Accepts multiple values (repeatable) or comma-separated lists.
69    ///
70    /// [default: install ALL extensions in artifact/source]
71    ///
72    #[arg(long, value_delimiter = ',', num_args = 1..)]
73    postgres_extensions: Option<Vec<String>>,
74}
75
76impl InitializeArgs {
77    /// returns `assume_yes` value
78    #[must_use]
79    pub fn assume_yes(&self) -> bool {
80        self.assume_yes
81    }
82
83    /// returns `sql_fun_home` value
84    #[must_use]
85    pub fn sql_fun_home(&self) -> &Option<PathBuf> {
86        &self.sql_fun_home
87    }
88
89    /// returns `engine_versions` values
90    #[must_use]
91    pub fn engine_versions(&self) -> Vec<String> {
92        if let Some(versions) = &self.engine_versions {
93            versions.clone()
94        } else {
95            vec![String::from("16"), String::from("17"), String::from("18")]
96        }
97    }
98
99    /// print long help
100    ///
101    /// # Errors
102    ///
103    /// [`std::io::Error`] : IO error
104    ///
105    pub fn print_long_help() -> Result<(), std::io::Error> {
106        Self::command().print_long_help()
107    }
108}