changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/bin/alik/main.rs

changeset 698: 96958d3eb5b0
parent: 94d358919982
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 //! app/cli/alik/main.rs --- Alik
2 
3 /// Code:
4 // use logger::log;
5 use alik::*;
6 use clap::{Parser, Subcommand};
7 use logger::{debug, error, trace, warn, Logger};
8 use std::path::PathBuf;
9 use util::{cli::log_level_str_from_cli, Result};
10 #[derive(Debug, Parser)]
11 #[command(name="alik",author, version, about, long_about = None)]
12 struct Cli {
13  /// Command to run
14  #[command(subcommand)]
15  cmd: Option<Cmd>,
16  /// Set the default config file
17  #[arg(short, long, env = "ALIK_CONFIG_FILE")]
18  cfg: Option<PathBuf>,
19  /// Set log level
20  #[arg(short, long, action = clap::ArgAction::Count)]
21  level: u8,
22 }
23 
24 #[derive(Debug, Subcommand)]
25 enum Cmd {
26  /// start the Alik service
27  Start { service: Option<String> },
28  /// Show Alik info
29  Show {
30  /// What to show
31  kind: Option<String>,
32  },
33  /// Ping remote services
34  Ping {},
35 }
36 
37 #[tokio::main]
38 async fn main() -> Result<()> {
39  // parse args
40  let args = Cli::parse();
41  // init logger
42  Logger::try_with_str(log_level_str_from_cli(args.level))?.start()?;
43  trace!("{:?}", args);
44  // load config
45  let cfg = if let Some(path) = args.cfg {
46  match AlikConfig::load(path.clone()) {
47  // FIXME
48  Ok(c) => c,
49  Err(e) => {
50  warn!("{path:?}: {e}, using default config");
51  AlikConfig::default()
52  }
53  }
54  } else {
55  AlikConfig::default()
56  };
57  // initialize
58  let alik = Alik::with_config(&cfg);
59  debug!("{:?}", cfg);
60  debug!("{:?}", alik);
61  // run cmd
62  if let Some(cmd) = args.cmd {
63  match cmd {
64  Cmd::Start { service: srv } => {
65  // start_service().await;
66  if let Some(s) = srv {
67  match s.as_str() {
68  "graphiql" => {
69  graphql::start_graphiql("127.0.0.1:0").await;
70  Ok(())
71  }
72  "http-proxy" => {
73  start_http_proxy("127.0.0.1:0").await;
74  Ok(())
75  }
76  _ => {
77  error!("invalid service name");
78  Ok(())
79  }
80  }
81  } else {
82  let http_proxy =
83  tokio::spawn(async move { start_http_proxy("127.0.0.1:0").await });
84  let graphiql = tokio::spawn(async move {
85  graphql::start_graphiql("127.0.0.1:0").await
86  });
87 
88  tokio::try_join!(graphiql, http_proxy)?;
89  Ok(())
90  }
91  }
92  Cmd::Ping {} => Ok(()),
93  Cmd::Show { kind: _ } => Ok(()),
94  }
95  } else {
96  Ok(())
97  }
98 }