Skip to content

Commit

Permalink
Move ame projectsrc to separate file
Browse files Browse the repository at this point in the history
Issue: #121

This is a part of the CLI refactor to move subcommands to separate
files. This commit creates a separate module for the projectsrc
subcommand.

Extra:

Write doc comments for projetsrc and secret sub commands.
remove web/tree
  • Loading branch information
Jessie Chatham Spencer committed Mar 18, 2023
1 parent e8a938d commit d3a6dd1
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 837 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
toolchain: nightly
- uses: Swatinem/rust-cache@v2
with:
shared-key: ame-ci-cache
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ serial_test = "0.9.0"
temp-env = { version = "0.3.1", features = ["async_closure"] }
rstest = "0.16.0"
k8s-openapi = { version = "0.17.0", features = ["v1_23", "schemars" ] }
ame = { path = "../lib" }
12 changes: 11 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod project;

use ame_client::client_builder::AmeServiceClientCfg;
use ame_client::client_builder::{build_ame_client, AmeClient, AmeServiceClientCfg};
use envconfig::Envconfig;

use http::uri::InvalidUri;
Expand Down Expand Up @@ -55,6 +55,7 @@ pub enum Error {

pub type Result<T, E = Error> = std::result::Result<T, E>;

pub mod projectsrc;
pub mod secrets;

#[derive(Clone, Default, Deserialize, Serialize, Envconfig, PartialEq, Debug)]
Expand Down Expand Up @@ -103,6 +104,15 @@ impl CliConfiguration {
..CliConfiguration::default()
}
}

pub async fn ame_client(&self) -> Result<AmeClient> {
Ok(build_ame_client(AmeServiceClientCfg {
disable_tls_cert_check: true, // TODO: the CLI needs some configuration for this.
endpoint: self.endpoint.parse().unwrap(),
id_token: self.id_token.clone(),
})
.await?)
}
}

impl TryFrom<CliConfiguration> for AmeServiceClientCfg {
Expand Down
268 changes: 5 additions & 263 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use std::time::Duration;

use ame_client::{
auth::browser_login,
client_builder::{build_ame_client, AmeServiceClientCfg},
GitProjectSource, ProjectSourceCfg, ProjectSourceListParams, ProjectSourceState,
ProjectSrcIdRequest, ProjectSrcPatchRequest, TaskIdentifier,
TaskIdentifier,
};
use clap::{Parser, Subcommand};
use cli::{
project::Project,
projectsrc::ProjectSrcCommands,
secrets::{exec_secret_command, SecretCommand},
CliConfiguration, Result,
};
use colored::Colorize;
use futures_util::StreamExt;

use http::StatusCode;
use spinners::Spinner;

use tonic::Request;

#[derive(Parser)]
Expand Down Expand Up @@ -47,31 +44,6 @@ enum Commands {
Secret(SecretCommand),
}

#[derive(Subcommand)]
enum ProjectSrcCommands {
Create {
repository: String,
#[arg(short, long)]
secret: Option<String>,
#[arg(short, long)]
user: Option<String>,
},

Delete {
repository: Option<String>,
},

Edit {
repository: String,
#[arg(short, long)]
secret: Option<String>,
#[arg(short, long)]
user: Option<String>,
},

List,
}

#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
Expand Down Expand Up @@ -160,237 +132,7 @@ async fn main() -> Result<()> {
.await?;
Ok(())
}
Commands::Projectsrc(ProjectSrcCommands::Delete { repository }) => {
let mut client = build_ame_client(AmeServiceClientCfg {
disable_tls_cert_check: true,
endpoint: config.endpoint.parse().unwrap(),
id_token: config.id_token,
})
.await?;

let repository = repository.as_ref().unwrap();

let id = client
.get_project_src_id(Request::new(ProjectSrcIdRequest {
repo: repository.to_string(),
}))
.await?
.into_inner();

client.delete_project_src(Request::new(id)).await?;

Ok(())
}

Commands::Projectsrc(ProjectSrcCommands::List) => {
let mut client = build_ame_client(AmeServiceClientCfg {
disable_tls_cert_check: true,
endpoint: config.endpoint.parse().unwrap(),
id_token: config.id_token,
})
.await?;

let srcs = client
.list_project_srcs(Request::new(ProjectSourceListParams {}))
.await?
.into_inner();

println!("{}", "Project Sources:".bright_white().bold());

for cfg in srcs.cfgs {
if let ProjectSourceCfg {
git:
Some(GitProjectSource {
repository,
username,
secret,
..
}),
} = cfg
{
println!(
"{} {} {}",
repository,
username.unwrap_or("".to_string()),
secret.unwrap_or("".to_string())
);
}
}

Ok(())
}
Commands::Projectsrc(ProjectSrcCommands::Edit {
repository,
secret,
user,
}) => {
let mut client = build_ame_client(AmeServiceClientCfg {
disable_tls_cert_check: true,
endpoint: config.endpoint.parse().unwrap(),
id_token: config.id_token,
})
.await?;

let id = client
.get_project_src_id(Request::new(ProjectSrcIdRequest {
repo: repository.to_string(),
}))
.await?
.into_inner();

client
.update_project_src(Request::new(ProjectSrcPatchRequest {
id: Some(id.clone()),
cfg: Some(ame_client::ProjectSourceCfg {
git: Some(ame_client::GitProjectSource {
repository: repository.to_string(),
sync_interval: Some("10s".to_string()),
secret: secret.clone(),
username: user.clone(),
}),
}),
}))
.await?
.into_inner();

let mut sp = Spinner::new(
spinners::Spinners::Dots,
format!("{} project source", "Updating".bold().cyan()),
);

let mut strm = client
.watch_project_src(Request::new(id))
.await?
.into_inner();

while let Some(entry) = strm.next().await {
if let Ok(ame_client::ProjectSourceStatus { state, reason, .. }) = entry {
match ProjectSourceState::from_i32(state) {
Some(ProjectSourceState::Synchronized) => {
sp.stop_and_persist(
" ",
format!("{} project source", "Updated".bold().green()),
);
break;
}
Some(ProjectSourceState::Pending) => (),
Some(ProjectSourceState::Error) => {
sp.stop_and_persist(
" ",
format!(
"{} to synchronize project source, reason: {}",
"Failed".bold().red(),
reason.unwrap_or("no reason :(".to_string()),
),
);
std::process::exit(1);
}
_ => {
sp.stop_and_persist(
" ",
format!("{} to update project source", "Failed".bold().red()),
);
std::process::exit(1);
}
}
}
}

sp = Spinner::new(
spinners::Spinners::Dots,
format!("{} projects from source", "Creating".bold().cyan()),
);

tokio::time::sleep(Duration::from_secs(5)).await;

sp.stop_and_persist(
" ",
format!("{} projects from source", "Created".bold().green()),
);

Ok(())
}
Commands::Projectsrc(ProjectSrcCommands::Create {
repository,
secret,
user,
}) => {
let mut client = build_ame_client(AmeServiceClientCfg {
disable_tls_cert_check: true,
endpoint: config.endpoint.parse().unwrap(),
id_token: config.id_token,
})
.await?;

let id = client
.create_project_src(Request::new(ame_client::ProjectSourceCfg {
git: Some(ame_client::GitProjectSource {
repository: repository.to_string(),
sync_interval: Some("10s".to_string()),
secret: secret.clone(),
username: user.clone(),
}),
}))
.await?
.into_inner();

let mut sp = Spinner::new(
spinners::Spinners::Dots,
format!("{} project source", "Creating".bold().cyan()),
);

let mut strm = client
.watch_project_src(Request::new(id))
.await?
.into_inner();

tokio::time::sleep(Duration::from_secs(2)).await;

loop {
let entry = strm.next().await;
let Some(entry) = entry else {
sp.stop_and_persist(
" ",
format!("{} to create project source", "Failed".bold().red()),
);
std::process::exit(1);
};
if let Ok(ame_client::ProjectSourceStatus { state, reason, .. }) = entry {
match ProjectSourceState::from_i32(state) {
Some(ProjectSourceState::Synchronized) => {
sp.stop_and_persist(
" ",
format!("{} project source", "Created".bold().green()),
);
break;
}
Some(ProjectSourceState::Pending) => (),
Some(ProjectSourceState::Error) => {
sp.stop_and_persist(
" ",
format!(
"{} to synchronize project source, reason: {}",
"Failed".bold().red(),
reason.unwrap_or("no reason :(".to_string()),
),
);
std::process::exit(1);
}
_ => {
sp.stop_and_persist(
" ",
format!("{} to create project source", "Failed".bold().red()),
);
std::process::exit(1);
}
}
}
}

sp.stop_and_persist(" ", format!("{} project source", "Created".bold().green()));

Ok(())
}
Commands::Projectsrc(cmd) => cmd.run(&config).await,
Commands::Secret(cmd) => exec_secret_command(config, cmd).await,
}
}
Expand Down
Loading

0 comments on commit d3a6dd1

Please sign in to comment.