2022-09-20 18:42:20 +00:00
|
|
|
pub mod base;
|
|
|
|
pub mod meta;
|
2022-09-15 19:11:46 +00:00
|
|
|
|
2022-09-20 18:42:20 +00:00
|
|
|
use base::{
|
2022-09-21 22:20:31 +00:00
|
|
|
ConfigureNixDaemonService, ConfigureNixDaemonServiceReceipt, CreateDirectory,
|
|
|
|
CreateDirectoryReceipt, CreateFile, CreateFileReceipt, CreateGroup, CreateGroupReceipt,
|
|
|
|
CreateOrAppendFile, CreateOrAppendFileReceipt, CreateUser, CreateUserReceipt, FetchNix,
|
|
|
|
FetchNixReceipt, MoveUnpackedNix, MoveUnpackedNixReceipt, PlaceChannelConfiguration,
|
|
|
|
PlaceChannelConfigurationReceipt, PlaceNixConfiguration, PlaceNixConfigurationReceipt,
|
2022-09-20 18:42:20 +00:00
|
|
|
SetupDefaultProfile, SetupDefaultProfileReceipt,
|
|
|
|
};
|
|
|
|
use meta::{
|
2022-09-21 22:20:31 +00:00
|
|
|
ConfigureNix, ConfigureNixReceipt, ConfigureShellProfile, ConfigureShellProfileReceipt,
|
|
|
|
CreateNixTree, CreateNixTreeReceipt, CreateUsersAndGroup, CreateUsersAndGroupReceipt,
|
|
|
|
ProvisionNix, ProvisionNixReceipt, StartNixDaemon, StartNixDaemonReceipt,
|
2022-09-15 19:11:46 +00:00
|
|
|
};
|
2022-09-20 18:42:20 +00:00
|
|
|
|
2022-09-15 19:11:46 +00:00
|
|
|
use crate::HarmonicError;
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
pub trait Actionable<'a>: serde::de::Deserialize<'a> + serde::Serialize {
|
2022-09-20 18:42:20 +00:00
|
|
|
type Receipt;
|
2022-09-15 19:11:46 +00:00
|
|
|
fn description(&self) -> Vec<ActionDescription>;
|
2022-09-20 18:42:20 +00:00
|
|
|
async fn execute(self) -> Result<Self::Receipt, HarmonicError>;
|
2022-09-15 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
pub trait Revertable<'a>: serde::de::Deserialize<'a> + serde::Serialize {
|
|
|
|
fn description(&self) -> Vec<ActionDescription>;
|
|
|
|
async fn revert(self) -> Result<(), HarmonicError>;
|
|
|
|
}
|
2022-09-14 22:18:13 +00:00
|
|
|
|
2022-09-15 17:29:22 +00:00
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
|
|
|
|
|
|
|
|
pub struct ActionDescription {
|
|
|
|
pub description: String,
|
|
|
|
pub explanation: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActionDescription {
|
|
|
|
fn new(description: String, explanation: Vec<String>) -> Self {
|
2022-09-15 19:11:46 +00:00
|
|
|
Self {
|
|
|
|
description,
|
|
|
|
explanation,
|
|
|
|
}
|
2022-09-15 17:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:18:13 +00:00
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
|
|
|
|
pub enum Action {
|
2022-09-15 19:11:46 +00:00
|
|
|
ConfigureNixDaemonService(ConfigureNixDaemonService),
|
|
|
|
ConfigureNix(ConfigureNix),
|
|
|
|
ConfigureShellProfile(ConfigureShellProfile),
|
2022-09-20 18:42:20 +00:00
|
|
|
CreateDirectory(CreateDirectory),
|
2022-09-21 20:26:45 +00:00
|
|
|
CreateFile(CreateFile),
|
2022-09-15 19:11:46 +00:00
|
|
|
CreateGroup(CreateGroup),
|
2022-09-21 20:26:45 +00:00
|
|
|
CreateOrAppendFile(CreateOrAppendFile),
|
2022-09-15 19:11:46 +00:00
|
|
|
CreateNixTree(CreateNixTree),
|
2022-09-14 22:18:13 +00:00
|
|
|
CreateUser(CreateUser),
|
2022-09-20 18:42:20 +00:00
|
|
|
CreateUsersAndGroup(CreateUsersAndGroup),
|
|
|
|
FetchNix(FetchNix),
|
|
|
|
MoveUnpackedNix(MoveUnpackedNix),
|
2022-09-15 19:11:46 +00:00
|
|
|
PlaceChannelConfiguration(PlaceChannelConfiguration),
|
|
|
|
PlaceNixConfiguration(PlaceNixConfiguration),
|
|
|
|
SetupDefaultProfile(SetupDefaultProfile),
|
|
|
|
StartNixDaemon(StartNixDaemon),
|
2022-09-20 18:42:20 +00:00
|
|
|
ProvisionNix(ProvisionNix),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
|
|
|
|
pub enum ActionReceipt {
|
2022-09-15 19:11:46 +00:00
|
|
|
ConfigureNixDaemonService(ConfigureNixDaemonServiceReceipt),
|
|
|
|
ConfigureNix(ConfigureNixReceipt),
|
|
|
|
ConfigureShellProfile(ConfigureShellProfileReceipt),
|
|
|
|
CreateDirectory(CreateDirectoryReceipt),
|
2022-09-21 20:26:45 +00:00
|
|
|
CreateFile(CreateFileReceipt),
|
2022-09-15 19:11:46 +00:00
|
|
|
CreateGroup(CreateGroupReceipt),
|
2022-09-21 20:26:45 +00:00
|
|
|
CreateOrAppendFile(CreateOrAppendFileReceipt),
|
2022-09-15 19:11:46 +00:00
|
|
|
CreateNixTree(CreateNixTreeReceipt),
|
2022-09-14 22:18:13 +00:00
|
|
|
CreateUser(CreateUserReceipt),
|
2022-09-20 18:42:20 +00:00
|
|
|
CreateUsersAndGroup(CreateUsersAndGroupReceipt),
|
|
|
|
FetchNix(FetchNixReceipt),
|
|
|
|
MoveUnpackedNix(MoveUnpackedNixReceipt),
|
2022-09-15 19:11:46 +00:00
|
|
|
PlaceChannelConfiguration(PlaceChannelConfigurationReceipt),
|
|
|
|
PlaceNixConfiguration(PlaceNixConfigurationReceipt),
|
|
|
|
SetupDefaultProfile(SetupDefaultProfileReceipt),
|
|
|
|
StartNixDaemon(StartNixDaemonReceipt),
|
2022-09-20 18:42:20 +00:00
|
|
|
ProvisionNix(ProvisionNixReceipt),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<'a> Actionable<'a> for Action {
|
2022-09-20 18:42:20 +00:00
|
|
|
type Receipt = ActionReceipt;
|
2022-09-15 17:29:22 +00:00
|
|
|
fn description(&self) -> Vec<ActionDescription> {
|
2022-09-14 22:18:13 +00:00
|
|
|
match self {
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::ConfigureNixDaemonService(i) => i.description(),
|
|
|
|
Action::ConfigureNix(i) => i.description(),
|
|
|
|
Action::ConfigureShellProfile(i) => i.description(),
|
|
|
|
Action::CreateDirectory(i) => i.description(),
|
2022-09-21 20:26:45 +00:00
|
|
|
Action::CreateFile(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::CreateGroup(i) => i.description(),
|
2022-09-21 20:26:45 +00:00
|
|
|
Action::CreateOrAppendFile(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::CreateNixTree(i) => i.description(),
|
2022-09-14 22:18:13 +00:00
|
|
|
Action::CreateUser(i) => i.description(),
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::CreateUsersAndGroup(i) => i.description(),
|
|
|
|
Action::FetchNix(i) => i.description(),
|
|
|
|
Action::MoveUnpackedNix(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
Action::PlaceChannelConfiguration(i) => i.description(),
|
|
|
|
Action::PlaceNixConfiguration(i) => i.description(),
|
|
|
|
Action::SetupDefaultProfile(i) => i.description(),
|
|
|
|
Action::StartNixDaemon(i) => i.description(),
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::ProvisionNix(i) => i.description(),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 18:42:20 +00:00
|
|
|
async fn execute(self) -> Result<Self::Receipt, HarmonicError> {
|
2022-09-14 22:18:13 +00:00
|
|
|
match self {
|
2022-09-21 22:20:31 +00:00
|
|
|
Action::ConfigureNixDaemonService(i) => i
|
|
|
|
.execute()
|
|
|
|
.await
|
|
|
|
.map(ActionReceipt::ConfigureNixDaemonService),
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::ConfigureNix(i) => i.execute().await.map(ActionReceipt::ConfigureNix),
|
2022-09-21 22:20:31 +00:00
|
|
|
Action::ConfigureShellProfile(i) => {
|
|
|
|
i.execute().await.map(ActionReceipt::ConfigureShellProfile)
|
|
|
|
},
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::CreateDirectory(i) => i.execute().await.map(ActionReceipt::CreateDirectory),
|
2022-09-21 20:26:45 +00:00
|
|
|
Action::CreateFile(i) => i.execute().await.map(ActionReceipt::CreateFile),
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::CreateGroup(i) => i.execute().await.map(ActionReceipt::CreateGroup),
|
2022-09-21 22:20:31 +00:00
|
|
|
Action::CreateOrAppendFile(i) => {
|
|
|
|
i.execute().await.map(ActionReceipt::CreateOrAppendFile)
|
|
|
|
},
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::CreateNixTree(i) => i.execute().await.map(ActionReceipt::CreateNixTree),
|
|
|
|
Action::CreateUser(i) => i.execute().await.map(ActionReceipt::CreateUser),
|
2022-09-21 22:20:31 +00:00
|
|
|
Action::CreateUsersAndGroup(i) => {
|
|
|
|
i.execute().await.map(ActionReceipt::CreateUsersAndGroup)
|
|
|
|
},
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::FetchNix(i) => i.execute().await.map(ActionReceipt::FetchNix),
|
|
|
|
Action::MoveUnpackedNix(i) => i.execute().await.map(ActionReceipt::MoveUnpackedNix),
|
2022-09-21 22:20:31 +00:00
|
|
|
Action::PlaceChannelConfiguration(i) => i
|
|
|
|
.execute()
|
|
|
|
.await
|
|
|
|
.map(ActionReceipt::PlaceChannelConfiguration),
|
|
|
|
Action::PlaceNixConfiguration(i) => {
|
|
|
|
i.execute().await.map(ActionReceipt::PlaceNixConfiguration)
|
|
|
|
},
|
|
|
|
Action::SetupDefaultProfile(i) => {
|
|
|
|
i.execute().await.map(ActionReceipt::SetupDefaultProfile)
|
|
|
|
},
|
2022-09-20 18:42:20 +00:00
|
|
|
Action::StartNixDaemon(i) => i.execute().await.map(ActionReceipt::StartNixDaemon),
|
|
|
|
Action::ProvisionNix(i) => i.execute().await.map(ActionReceipt::ProvisionNix),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<'a> Revertable<'a> for ActionReceipt {
|
2022-09-15 17:29:22 +00:00
|
|
|
fn description(&self) -> Vec<ActionDescription> {
|
2022-09-14 22:18:13 +00:00
|
|
|
match self {
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::ConfigureNixDaemonService(i) => i.description(),
|
|
|
|
ActionReceipt::ConfigureNix(i) => i.description(),
|
|
|
|
ActionReceipt::ConfigureShellProfile(i) => i.description(),
|
|
|
|
ActionReceipt::CreateDirectory(i) => i.description(),
|
2022-09-21 20:26:45 +00:00
|
|
|
ActionReceipt::CreateFile(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::CreateGroup(i) => i.description(),
|
2022-09-21 20:26:45 +00:00
|
|
|
ActionReceipt::CreateOrAppendFile(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::CreateNixTree(i) => i.description(),
|
2022-09-14 22:18:13 +00:00
|
|
|
ActionReceipt::CreateUser(i) => i.description(),
|
2022-09-20 18:42:20 +00:00
|
|
|
ActionReceipt::CreateUsersAndGroup(i) => i.description(),
|
|
|
|
ActionReceipt::FetchNix(i) => i.description(),
|
|
|
|
ActionReceipt::MoveUnpackedNix(i) => i.description(),
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::PlaceChannelConfiguration(i) => i.description(),
|
|
|
|
ActionReceipt::PlaceNixConfiguration(i) => i.description(),
|
|
|
|
ActionReceipt::SetupDefaultProfile(i) => i.description(),
|
|
|
|
ActionReceipt::StartNixDaemon(i) => i.description(),
|
2022-09-20 18:42:20 +00:00
|
|
|
ActionReceipt::ProvisionNix(i) => i.description(),
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn revert(self) -> Result<(), HarmonicError> {
|
|
|
|
match self {
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::ConfigureNixDaemonService(i) => i.revert().await,
|
|
|
|
ActionReceipt::ConfigureNix(i) => i.revert().await,
|
|
|
|
ActionReceipt::ConfigureShellProfile(i) => i.revert().await,
|
|
|
|
ActionReceipt::CreateDirectory(i) => i.revert().await,
|
2022-09-21 20:26:45 +00:00
|
|
|
ActionReceipt::CreateFile(i) => i.revert().await,
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::CreateGroup(i) => i.revert().await,
|
2022-09-21 20:26:45 +00:00
|
|
|
ActionReceipt::CreateOrAppendFile(i) => i.revert().await,
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::CreateNixTree(i) => i.revert().await,
|
2022-09-14 22:18:13 +00:00
|
|
|
ActionReceipt::CreateUser(i) => i.revert().await,
|
2022-09-20 18:42:20 +00:00
|
|
|
ActionReceipt::CreateUsersAndGroup(i) => i.revert().await,
|
|
|
|
ActionReceipt::FetchNix(i) => i.revert().await,
|
|
|
|
ActionReceipt::MoveUnpackedNix(i) => i.revert().await,
|
2022-09-15 19:11:46 +00:00
|
|
|
ActionReceipt::PlaceChannelConfiguration(i) => i.revert().await,
|
|
|
|
ActionReceipt::PlaceNixConfiguration(i) => i.revert().await,
|
|
|
|
ActionReceipt::SetupDefaultProfile(i) => i.revert().await,
|
|
|
|
ActionReceipt::StartNixDaemon(i) => i.revert().await,
|
2022-09-20 18:42:20 +00:00
|
|
|
ActionReceipt::ProvisionNix(i) => i.revert().await,
|
2022-09-14 22:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|