1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use console::style;
use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::Path;

#[derive(Serialize, Deserialize, Debug)]
pub struct GcpConfig {
  pub project_id: String,
  pub service_name: String,
  pub region: String,
}

pub async fn process_init_gcp_config() {
  println!(
    "📝 {}",
    style("Please input your GCP project_id:").white().bold()
  );
  let mut project_id = String::new();
  io::stdin()
    .read_line(&mut project_id)
    .expect("Failed to read line");
  let project_id: String = project_id
    .trim()
    .parse()
    .expect("Please input your GCP project_id:");

  println!(
    "📝 {}",
    style("Please input your GCP service_name:").white().bold()
  );
  let mut service_name = String::new();
  io::stdin()
    .read_line(&mut service_name)
    .expect("Failed to read line");
  let service_name: String = service_name
    .trim()
    .parse()
    .expect("Please input your GCP service_name:");

  println!(
    "📝 {}",
    style("Please input your GCP region:").white().bold()
  );
  let mut region = String::new();
  io::stdin()
    .read_line(&mut region)
    .expect("Failed to read line");
  let region: String = region
    .trim()
    .parse()
    .expect("Please input your GCP region:");

  let json_struct = build_gcp_config(project_id, service_name, region).await;
  let result = write_gcp_config(json_struct).await;
  match result {
    Ok(..) => {
      println!("✅ {}", style("Successfully Generated!").green().bold());
      println!("🗃️ {}", style("File Path: ./gcp_config.json").white().bold());
    }
    Err(err) => {
      println!("Failed to Write: {}", err)
    }
  }
}

async fn write_gcp_config(json_struct: GcpConfig) -> std::io::Result<()> {
  let serialized: String = serde_json::to_string_pretty(&json_struct).unwrap();
  let mut file = File::create("gcp_config.json")?;
  file.write_all(serialized.as_bytes())?;
  Ok(())
}

async fn build_gcp_config(project_id: String, service_name: String, region: String) -> GcpConfig {
  GcpConfig {
    project_id,
    service_name,
    region,
  }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ApiWorkflow {
  pub name: String,
  pub on: Push,
  pub jobs: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Push {
  pub push: Branch,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Branch {
  pub branches: String,
}

pub async fn build_api_workflow(nat: bool) -> std::io::Result<()> {
  let workflow_template = match nat {
    true => "./src/init/nat.yml",
    false => "./src/init/default.yml",
  };
  let workflow_yml = ".github/workflows/epic_service.yml";
  let file_exist = Path::new(workflow_yml).exists();
  match file_exist {
    true => {
      panic!("File already exist!")
    }
    false => {
      let _ = fs::copy(workflow_template, workflow_yml);
    }
  }
  Ok(())
}