From c4c57f5e172519185df7149245789b08a752fd65 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 5 Sep 2024 21:46:16 +0200 Subject: [PATCH] feat(cli): init command --- .gitignore | 6 +- CHANGELOG.md | 3 +- Cargo.lock | 421 +++++++++++++++++++++++++ Cargo.toml | 1 + autoi18n.json | 3 + packages/autoi18n-config/src/error.rs | 7 +- packages/autoi18n-config/src/lib.rs | 14 +- packages/autoi18n/Cargo.toml | 2 + packages/autoi18n/src/commands/init.rs | 7 + packages/autoi18n/src/commands/mod.rs | 28 ++ packages/autoi18n/src/config/mod.rs | 1 + packages/autoi18n/src/error/init.rs | 33 ++ packages/autoi18n/src/error/mod.rs | 13 + packages/autoi18n/src/init/mod.rs | 22 ++ packages/autoi18n/src/main.rs | 21 +- 15 files changed, 569 insertions(+), 13 deletions(-) create mode 100644 Cargo.lock create mode 100644 autoi18n.json create mode 100644 packages/autoi18n/src/commands/init.rs create mode 100644 packages/autoi18n/src/commands/mod.rs create mode 100644 packages/autoi18n/src/config/mod.rs create mode 100644 packages/autoi18n/src/error/init.rs create mode 100644 packages/autoi18n/src/init/mod.rs diff --git a/.gitignore b/.gitignore index d01bd1a..6e771f8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,6 @@ debug/ target/ -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - # These are backup files generated by rustfmt **/*.rs.bk @@ -18,4 +14,4 @@ Cargo.lock # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ diff --git a/CHANGELOG.md b/CHANGELOG.md index f935724..366f854 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,10 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### Unreleased +- feat(config): json schema [`#5`](https://github.com/autoi18n/cli/pull/5) +- feat: setup config [`#4`](https://github.com/autoi18n/cli/pull/4) - chore: setup dependabot [`#3`](https://github.com/autoi18n/cli/pull/3) - chore: validation workflow [`#2`](https://github.com/autoi18n/cli/pull/2) - build: setup publishing [`#1`](https://github.com/autoi18n/cli/pull/1) - chore: setup workspace [`11b6f6c`](https://github.com/autoi18n/cli/commit/11b6f6c749b6ff69b7cf84a9131c05be73bde525) -- feat: setup config crate [`e89b17f`](https://github.com/autoi18n/cli/commit/e89b17fa9c6c04215ab21b04f6e967d726957552) - Initial commit [`d96cebc`](https://github.com/autoi18n/cli/commit/d96cebc79c1f5243aa1ed05b2e68aaf5e380c61a) diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d9a5724 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,421 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + +[[package]] +name = "autoi18n" +version = "0.0.0" +dependencies = [ + "autoi18n-config", + "clap", + "serde_json", +] + +[[package]] +name = "autoi18n-config" +version = "0.0.0" +dependencies = [ + "schemars", + "serde", + "serde_json", + "tempfile", +] + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "dyn-clone" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustix" +version = "0.38.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "schemars" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92" +dependencies = [ + "dyn-clone", + "schemars_derive", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1eee588578aff73f856ab961cd2f79e36bc45d7ded33a7562adba4667aecc0e" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn", +] + +[[package]] +name = "serde" +version = "1.0.209" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.209" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_derive_internals" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" +dependencies = [ + "cfg-if", + "fastrand", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index f209ea4..d6a3222 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ categories = ["command-line-utilities", "development-tools"] [workspace.dependencies] autoi18n-config = { version = "0.0.0", path = "./packages/autoi18n-config" } +clap = { version = "4.5.17", features = ["derive"] } schemars = "0.8.21" serde = { version = "1.0.209", features = ["derive"] } serde_json = "1.0.128" diff --git a/autoi18n.json b/autoi18n.json new file mode 100644 index 0000000..58c69df --- /dev/null +++ b/autoi18n.json @@ -0,0 +1,3 @@ +{ + "$schema": "https://raw.githubusercontent.com/autoi18n/cli/main/schemas/v0.0.0/autoi18n.schema.json" +} diff --git a/packages/autoi18n-config/src/error.rs b/packages/autoi18n-config/src/error.rs index b68954f..9ee270e 100644 --- a/packages/autoi18n-config/src/error.rs +++ b/packages/autoi18n-config/src/error.rs @@ -2,6 +2,7 @@ pub enum ConfigError { IoError(std::io::Error), ParseError(serde_json::Error), + NotFound, } impl std::error::Error for ConfigError {} @@ -12,6 +13,7 @@ impl core::fmt::Display for ConfigError { match self { Self::IoError(e) => e.fmt(f), Self::ParseError(e) => e.fmt(f), + Self::NotFound => write!(f, "Config not found"), } } } @@ -19,7 +21,10 @@ impl core::fmt::Display for ConfigError { impl From for ConfigError { #[inline] fn from(value: std::io::Error) -> Self { - Self::IoError(value) + match value.kind() { + std::io::ErrorKind::NotFound => Self::NotFound, + _ => Self::IoError(value), + } } } diff --git a/packages/autoi18n-config/src/lib.rs b/packages/autoi18n-config/src/lib.rs index 33eda84..89197c2 100644 --- a/packages/autoi18n-config/src/lib.rs +++ b/packages/autoi18n-config/src/lib.rs @@ -2,7 +2,7 @@ use error::ConfigError; pub mod error; -#[derive(serde::Serialize, serde::Deserialize, Default)] +#[derive(serde::Serialize, serde::Deserialize)] #[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] #[cfg_attr(test, derive(Debug, PartialEq, Eq))] pub struct CliConfig { @@ -10,6 +10,15 @@ pub struct CliConfig { pub schema: String, } +impl Default for CliConfig { + #[inline] + fn default() -> Self { + Self { + schema: Self::default_schema_location(), + } + } +} + impl CliConfig { #[inline] pub fn load(path: impl AsRef) -> Result { @@ -21,7 +30,7 @@ impl CliConfig { } #[inline] - fn default_schema_location() -> String { + pub fn default_schema_location() -> String { let package_version = env!("CARGO_PKG_VERSION"); format!( @@ -32,7 +41,6 @@ impl CliConfig { #[cfg(test)] mod test_config { - use crate::CliConfig; #[test] diff --git a/packages/autoi18n/Cargo.toml b/packages/autoi18n/Cargo.toml index 787d0b2..77a5c51 100644 --- a/packages/autoi18n/Cargo.toml +++ b/packages/autoi18n/Cargo.toml @@ -20,3 +20,5 @@ eula = false [dependencies] autoi18n-config = { workspace = true } +clap = { workspace = true } +serde_json = { workspace = true } diff --git a/packages/autoi18n/src/commands/init.rs b/packages/autoi18n/src/commands/init.rs new file mode 100644 index 0000000..f3c8676 --- /dev/null +++ b/packages/autoi18n/src/commands/init.rs @@ -0,0 +1,7 @@ +use clap::Args; + +#[derive(Args, Debug)] +pub struct InitCommandArguments { + #[arg(long, default_value_t = false)] + pub overwrite: bool, +} diff --git a/packages/autoi18n/src/commands/mod.rs b/packages/autoi18n/src/commands/mod.rs new file mode 100644 index 0000000..043d8bc --- /dev/null +++ b/packages/autoi18n/src/commands/mod.rs @@ -0,0 +1,28 @@ +use clap::{Parser, Subcommand}; +use init::InitCommandArguments; + +pub mod init; + +const HELP_TEMPLATE: &str = "\ +{before-help}{name} {version} +{about-with-newline}{author-with-newline} +{usage-heading} {usage} + +{all-args}{after-help} +"; + +#[derive(Debug, Parser)] +#[command(author, version, about, long_about = None, propagate_version = true, help_template = HELP_TEMPLATE)] +pub struct Cli { + #[command(subcommand)] + pub command: CliCommand, +} + +#[derive(Debug, Subcommand)] +pub enum CliCommand { + /// Synchronize locales + Sync, + + /// Create new autoi18n config + Init(InitCommandArguments), +} diff --git a/packages/autoi18n/src/config/mod.rs b/packages/autoi18n/src/config/mod.rs new file mode 100644 index 0000000..e129b30 --- /dev/null +++ b/packages/autoi18n/src/config/mod.rs @@ -0,0 +1 @@ +pub const CONFIG_PATH: &str = "autoi18n.json"; diff --git a/packages/autoi18n/src/error/init.rs b/packages/autoi18n/src/error/init.rs new file mode 100644 index 0000000..3635268 --- /dev/null +++ b/packages/autoi18n/src/error/init.rs @@ -0,0 +1,33 @@ +#[derive(Debug)] +pub enum InitError { + Io(std::io::Error), + Serialization(serde_json::Error), + ConfigAlreadyExists, +} + +impl std::error::Error for InitError {} + +impl core::fmt::Display for InitError { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(e) => e.fmt(f), + Self::Serialization(e) => e.fmt(f), + Self::ConfigAlreadyExists => write!(f, "A configuration file already exists"), + } + } +} + +impl From for InitError { + #[inline] + fn from(value: std::io::Error) -> Self { + Self::Io(value) + } +} + +impl From for InitError { + #[inline] + fn from(value: serde_json::Error) -> Self { + Self::Serialization(value) + } +} diff --git a/packages/autoi18n/src/error/mod.rs b/packages/autoi18n/src/error/mod.rs index c3076f6..2727cdf 100644 --- a/packages/autoi18n/src/error/mod.rs +++ b/packages/autoi18n/src/error/mod.rs @@ -1,6 +1,11 @@ +use init::InitError; + +pub mod init; + #[derive(Debug)] pub enum CliError { Config(autoi18n_config::error::ConfigError), + Init(InitError), } impl std::error::Error for CliError {} @@ -10,6 +15,7 @@ impl core::fmt::Display for CliError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Config(e) => e.fmt(f), + Self::Init(e) => e.fmt(f), } } } @@ -20,3 +26,10 @@ impl From for CliError { Self::Config(value) } } + +impl From for CliError { + #[inline] + fn from(value: InitError) -> Self { + Self::Init(value) + } +} diff --git a/packages/autoi18n/src/init/mod.rs b/packages/autoi18n/src/init/mod.rs new file mode 100644 index 0000000..c411fdf --- /dev/null +++ b/packages/autoi18n/src/init/mod.rs @@ -0,0 +1,22 @@ +use autoi18n_config::CliConfig; + +use crate::{commands::init::InitCommandArguments, config::CONFIG_PATH, error::init::InitError}; + +#[inline] +pub fn run(arguments: &InitCommandArguments) -> Result<(), InitError> { + if !arguments.overwrite && std::fs::exists(CONFIG_PATH)? { + return Err(InitError::ConfigAlreadyExists); + } + + let config = CliConfig::default(); + + let mut json = serde_json::to_string_pretty(&config)?; + + json.push('\n'); + + std::fs::write(CONFIG_PATH, json)?; + + println!("Configuration file has been created"); + + Ok(()) +} diff --git a/packages/autoi18n/src/main.rs b/packages/autoi18n/src/main.rs index 70b6417..f58a37e 100644 --- a/packages/autoi18n/src/main.rs +++ b/packages/autoi18n/src/main.rs @@ -1,16 +1,31 @@ use autoi18n_config::CliConfig; +use clap::Parser; +use commands::{Cli, CliCommand}; +use config::CONFIG_PATH; +use error::CliError; +mod commands; +mod config; mod error; +mod init; #[inline] -fn _main() -> Result<(), error::CliError> { - let _config = CliConfig::load("autoi18n.json")?; +fn _main() -> Result<(), CliError> { + match Cli::parse().command { + CliCommand::Init(arguments) => init::run(&arguments).map_err(CliError::from), + CliCommand::Sync => { + // TODO + let _config = CliConfig::load(CONFIG_PATH)?; - Ok(()) + Ok(()) + } + } } fn main() { if let Err(error) = _main() { eprintln!("{error}"); + + std::process::exit(1) } }