Skip to content

Commit

Permalink
Merge pull request #158 from spencerwooo:impl-mihoro
Browse files Browse the repository at this point in the history
Separation of concerns
  • Loading branch information
spencerwooo authored Jan 3, 2024
2 parents 53c4a8e + 51cbb1a commit f9d7147
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 335 deletions.
43 changes: 22 additions & 21 deletions src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use clap::Parser;
use clap::Subcommand;
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(author, about, version)]
#[command(author, about, version, arg_required_else_help(true))]
pub struct Args {
/// Path to mihoro config file
#[clap(short, long, default_value = "~/.config/mihoro.toml")]
Expand All @@ -13,53 +12,55 @@ pub struct Args {

#[derive(Subcommand)]
pub enum Commands {
#[command(about = "Setup mihoro by downloading mihomo binary and remote config")]
/// Setup mihoro by downloading mihomo binary and remote config
Setup,
#[command(about = "Update mihomo remote config and restart mihomo.service")]
/// Update mihomo remote config and restart mihomo.service
Update,
#[command(about = "Apply mihomo config overrides and restart mihomo.service")]
/// Apply mihomo config overrides and restart mihomo.service
Apply,
#[command(about = "Start mihomo.service with systemctl")]
/// Start mihomo.service with systemctl
Start,
#[command(about = "Check mihomo.service status with systemctl")]
/// Check mihomo.service status with systemctl
Status,
#[command(about = "Stop mihomo.service with systemctl")]
/// Stop mihomo.service with systemctl
Stop,
#[command(about = "Restart mihomo.service with systemctl")]
/// Restart mihomo.service with systemctl
Restart,
#[command(about = "Check mihomo.service logs with journalctl")]
/// Check mihomo.service logs with journalctl
Log,
#[command(about = "Proxy export commands, `mihoro proxy --help` for details")]
/// Output proxy export commands
Proxy {
#[command(subcommand)]
#[clap(subcommand)]
proxy: Option<ProxyCommands>,
},
#[command(about = "Uninstall and remove mihomo and config")]
/// Uninstall and remove mihoro and config
Uninstall,
#[command(about = "Generate shell completions for mihoro")]
/// Generate shell completions for mihoro
Completions {
#[clap(subcommand)]
shell: Option<ClapShell>,
},
}

#[derive(Subcommand)]
#[command(arg_required_else_help(true))]
pub enum ProxyCommands {
#[command(about = "Output and copy proxy export shell commands")]
/// Output and copy proxy export shell commands
Export,
#[command(about = "Output and copy proxy export shell commands for LAN access")]
/// Output and copy proxy export shell commands for LAN access
ExportLan,
#[command(about = "Output and copy proxy unset shell commands")]
/// Output and copy proxy unset shell commands
Unset,
}

#[derive(Subcommand)]
#[command(arg_required_else_help(true))]
pub enum ClapShell {
#[command(about = "Generate bash completions")]
/// Generate bash completions
Bash,
#[command(about = "Generate fish completions")]
/// Generate fish completions
Fish,
#[command(about = "Generate zsh completions")]
/// Generate zsh completions
Zsh,
// #[command(about = "Generate powershell completions")]
// Powershell,
Expand Down
18 changes: 7 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use crate::utils::create_parent_dir;

use anyhow::bail;
use anyhow::Result;
use colored::Colorize;
use serde::Deserialize;
use serde::Serialize;
use std::{collections::HashMap, fs, path::Path};

use crate::utils::create_parent_dir;
use anyhow::{bail, Result};
use colored::Colorize;
use serde::{Deserialize, Serialize};

/// `mihoro` configurations.
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
pub remote_mihomo_binary_url: String,
pub remote_config_url: String,
Expand All @@ -24,7 +20,7 @@ pub struct Config {
/// `mihomo` configurations (partial).
///
/// Referenced from https://github.com/Dreamacro/mihomo/wiki/configuration
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MihomoConfig {
pub port: u16,
pub socks_port: u16,
Expand Down
Loading

0 comments on commit f9d7147

Please sign in to comment.