gcloud_utils/init/
process.rs

1use console::style;
2use serde::{Deserialize, Serialize};
3use std::fs;
4use std::fs::File;
5use std::io;
6use std::io::Write;
7use std::path::Path;
8
9#[derive(Serialize, Deserialize, Debug)]
10pub struct GcpConfig {
11  pub project_id: String,
12  pub service_name: String,
13  pub region: String,
14}
15
16pub async fn process_init_gcp_config() {
17  println!(
18    "📝 {}",
19    style("Please input your GCP project_id:").white().bold()
20  );
21  let mut project_id = String::new();
22  io::stdin()
23    .read_line(&mut project_id)
24    .expect("Failed to read line");
25  let project_id: String = project_id
26    .trim()
27    .parse()
28    .expect("Please input your GCP project_id:");
29
30  println!(
31    "📝 {}",
32    style("Please input your GCP service_name:").white().bold()
33  );
34  let mut service_name = String::new();
35  io::stdin()
36    .read_line(&mut service_name)
37    .expect("Failed to read line");
38  let service_name: String = service_name
39    .trim()
40    .parse()
41    .expect("Please input your GCP service_name:");
42
43  println!(
44    "📝 {}",
45    style("Please input your GCP region:").white().bold()
46  );
47  let mut region = String::new();
48  io::stdin()
49    .read_line(&mut region)
50    .expect("Failed to read line");
51  let region: String = region
52    .trim()
53    .parse()
54    .expect("Please input your GCP region:");
55
56  let json_struct = build_gcp_config(project_id, service_name, region).await;
57  let result = write_gcp_config(json_struct).await;
58  match result {
59    Ok(..) => {
60      println!("✅ {}", style("Successfully Generated!").green().bold());
61      println!("🗃️ {}", style("File Path: ./gcp_config.json").white().bold());
62    }
63    Err(err) => {
64      println!("Failed to Write: {}", err)
65    }
66  }
67}
68
69async fn write_gcp_config(json_struct: GcpConfig) -> std::io::Result<()> {
70  let serialized: String = serde_json::to_string_pretty(&json_struct).unwrap();
71  let mut file = File::create("gcp_config.json")?;
72  file.write_all(serialized.as_bytes())?;
73  Ok(())
74}
75
76async fn build_gcp_config(project_id: String, service_name: String, region: String) -> GcpConfig {
77  GcpConfig {
78    project_id,
79    service_name,
80    region,
81  }
82}
83
84#[derive(Serialize, Deserialize, Debug)]
85pub struct ApiWorkflow {
86  pub name: String,
87  pub on: Push,
88  pub jobs: String,
89}
90
91#[derive(Serialize, Deserialize, Debug)]
92pub struct Push {
93  pub push: Branch,
94}
95
96#[derive(Serialize, Deserialize, Debug)]
97pub struct Branch {
98  pub branches: String,
99}
100
101pub async fn build_api_workflow(nat: bool) -> std::io::Result<()> {
102  let workflow_dir = ".github/workflows";
103  fs::create_dir_all(workflow_dir).unwrap_or_else(|why| {
104    println!("! {:?}", why.kind());
105  });
106  let workflow_template = match nat {
107    true => "./src/nat.yml",
108    false => "./src/default.yml",
109  };
110  let workflow_yml = ".github/workflows/epic_service.yml";
111  let file_exist = Path::new(workflow_yml).exists();
112  match file_exist {
113    true => {
114      panic!("File already exist!")
115    }
116    false => {
117      let copy_yaml = fs::copy(workflow_template, workflow_yml);
118      match copy_yaml {
119        Ok(_) => println!("✅ {}", style("Successfully Generated!").green().bold()),
120        Err(error) => panic!("error: {}", error),
121      }
122    }
123  }
124  Ok(())
125}
126
127async fn yml() {
128  
129}