croshet 0.1.0

Cross platform Unix-like shell scripting library
Documentation
// SPDX-License-Identifier: MIT AND MPL-2.0

use crate::shell::types::EnvChange;
use crate::shell::types::ExecuteResult;

use super::ShellCommand;
use super::ShellCommandContext;

pub struct ExportCommand;

#[async_trait::async_trait]
impl ShellCommand for ExportCommand {
  async fn execute(&self, context: ShellCommandContext) -> ExecuteResult {
    let mut changes = Vec::new();
    for arg in context.args {
      // todo: support non-UTF8 data here
      if let Some(arg) = arg.to_str() {
        // ignore if it doesn't contain an equals
        if let Some(equals_index) = arg.find('=') {
          let arg_name = &arg[..equals_index];
          let arg_value = &arg[equals_index + 1..];
          changes.push(EnvChange::SetEnvVar(arg_name.into(), arg_value.into()));
        }
      }
    }

    ExecuteResult::Continue(0, changes, Vec::new())
  }
}