Skip to content

Commit

Permalink
Merge pull request #25 from wobcom/feature/task-builder
Browse files Browse the repository at this point in the history
vicky: Task Builder
  • Loading branch information
Kek5chen authored Apr 24, 2024
2 parents 1f01bb4 + 6b97b1a commit 0094ea7
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 218 deletions.
19 changes: 8 additions & 11 deletions vicky/src/bin/vicky/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,14 @@ pub async fn tasks_add(
) -> Result<Json<RoTask>, AppError> {
let task_uuid = Uuid::new_v4();

let task_manifest = Task {
id: task_uuid,
status: TaskStatus::NEW,
locks: task.locks.clone(),
display_name: task.display_name.clone(),
flake_ref: FlakeRef {
flake: task.flake_ref.flake.clone(),
args: task.flake_ref.args.clone(),
},
features: task.features.clone(),
};
let task_manifest = Task::builder()
.with_id(task_uuid)
.with_display_name(&task.display_name)
.with_flake(&task.flake_ref.flake)
.with_flake_args(task.flake_ref.args.clone())
.with_locks(task.locks.clone())
.requires_features(task.features.clone())
.build();

etcd.put_task(&task_manifest).await?;
global_events.send(GlobalEvent::TaskAdd)?;
Expand Down
129 changes: 129 additions & 0 deletions vicky/src/lib/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,135 @@ pub struct Task {
pub features: Vec<String>,
}

impl Task {
pub fn builder() -> TaskBuilder {
TaskBuilder::default()
}
}

impl From<TaskBuilder> for Task {
fn from(builder: TaskBuilder) -> Self {
builder.build()
}
}

pub struct TaskBuilder {
id: Option<Uuid>,
display_name: Option<String>,
status: TaskStatus,
locks: Vec<Lock>,
flake_ref: FlakeRef,
features: Vec<String>,
}

impl Default for TaskBuilder {
fn default() -> Self {
TaskBuilder {
id: None,
display_name: None,
status: TaskStatus::NEW,
locks: Vec::new(),
flake_ref: FlakeRef {
flake: "".to_string(),
args: Vec::new(),
},
features: Vec::new(),
}
}
}

impl TaskBuilder {
pub fn with_id(mut self, id: Uuid) -> Self {
self.id = Some(id);
self
}

pub fn with_display_name<S: Into<String>>(mut self, display_name: S) -> Self {
self.display_name = Some(display_name.into());
self
}

pub fn with_status(mut self, status: TaskStatus) -> Self {
self.status = status;
self
}

pub fn with_read_lock<S: Into<String>>(mut self, name: S) -> Self {
self.locks.push(Lock::READ { name: name.into() });
self
}

pub fn with_write_lock<S: Into<String>>(mut self, name: S) -> Self {
self.locks.push(Lock::WRITE { name: name.into() });
self
}

pub fn with_locks(mut self, locks: Vec<Lock>) -> Self {
self.locks = locks;
self
}

pub fn with_flake<S: Into<FlakeURI>>(mut self, flake_uri: S) -> Self {
self.flake_ref.flake = flake_uri.into();
self
}

pub fn with_flake_arg<S: Into<String>>(mut self, flake_arg: S) -> Self {
self.flake_ref.args.push(flake_arg.into());
self
}

pub fn with_flake_args(mut self, args: Vec<String>) -> Self {
self.flake_ref.args = args;
self
}

pub fn requires_feature<S: Into<String>>(mut self, feature: S) -> Self {
self.features.push(feature.into());
self
}

pub fn requires_features(mut self, features: Vec<String>) -> Self {
self.features = features;
self
}

pub fn id(&self) -> Option<Uuid> {
self.id
}

pub fn display_name(&self) -> &Option<String> {
&self.display_name
}

pub fn status(&self) -> &TaskStatus {
&self.status
}

pub fn locks(&self) -> &Vec<Lock> {
&self.locks
}

pub fn flake_ref(&self) -> &FlakeRef {
&self.flake_ref
}

pub fn features(&self) -> &Vec<String> {
&self.features
}

pub fn build(self) -> Task {
Task {
id: self.id.unwrap_or_else(Uuid::new_v4),
display_name: self.display_name.unwrap_or_else(|| "Task".to_string()),
features: self.features,
status: self.status,
locks: self.locks,
flake_ref: self.flake_ref,
}
}
}

#[async_trait]
pub trait DocumentClient {
async fn get_all_tasks(&self) -> Result<Vec<Task>, VickyError>;
Expand Down
Loading

0 comments on commit 0094ea7

Please sign in to comment.