Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mktemp functionality in Web5 CLI #387

Open
16 tasks
Tracked by #322
blackgirlbytes opened this issue Oct 9, 2024 · 10 comments
Open
16 tasks
Tracked by #322

Implement mktemp functionality in Web5 CLI #387

blackgirlbytes opened this issue Oct 9, 2024 · 10 comments

Comments

@blackgirlbytes
Copy link
Contributor

Implement mktemp functionality in Web5 CLI

🚀 Goal

Add mktemp functionality to the Web5 Rust CLI to allow users to create temporary files and directories securely.

🔑 Tasks

  • Research Rust's standard library capabilities for temporary file/directory creation
  • Design the command structure (e.g., web5 mktemp [OPTIONS])
  • Implement core mktemp functionality:
    • Create temporary files
    • Create temporary directories
    • Allow user to specify prefix/suffix for temp names
    • Ensure proper permissions are set on created files/directories
  • Add options for:
    • Dry-run mode (print name without creating)
    • Specifying alternative temp directory
    • Creating a specified number of temporary items
  • Implement proper error handling and user feedback
  • Ensure cross-platform compatibility (Unix-like systems and Windows)
  • Write unit tests for the new functionality
  • Update CLI documentation to include the new mktemp command
  • Add examples of mktemp usage to the CLI help text

🌟 Resources

⚡ Getting Started

  1. Comment ".take" on this issue to get assigned
  2. Fork the repository and create a new branch for this task
  3. Implement the mktemp functionality
  4. Write tests for your implementation
  5. Update documentation and help text
  6. Submit a pull request with your changes
  7. Respond to any feedback during the review process

Implementation Ideas

  1. Consider using the tempfile crate for robust temporary file handling:
use tempfile::Builder;

let named_tempfile = Builder::new()
    .prefix("web5-")
    .suffix(".tmp")
    .rand_bytes(5)
    .tempfile()?;

println!("Created temporary file: {:?}", named_tempfile.path());
  1. For cross-platform compatibility, use std::env::temp_dir() to get the system's temp directory:
use std::env;

let temp_dir = env::temp_dir();
println!("System temp directory: {:?}", temp_dir);
  1. Implement a function to generate unique names:
use rand::{thread_rng, Rng};
use std::path::PathBuf;

fn generate_unique_name(prefix: &str, suffix: &str) -> PathBuf {
    let random_string: String = thread_rng()
        .sample_iter(&rand::distributions::Alphanumeric)
        .take(10)
        .map(char::from)
        .collect();
    
    PathBuf::from(format!("{}{}{}", prefix, random_string, suffix))
}

💭 Questions?

If you have any questions or need clarification, please comment on this issue or join our Discord community.

Happy coding! 🎉

@w3irdrobot
Copy link
Contributor

before picking this up, i'm just curious what the use-case for this command being in this CLI is versus just using the unix mktemp command available via GNU coreutils?

@blackgirlbytes
Copy link
Contributor Author

I'm going to ping @KendallWeihe to get his opinion on this! I made the issue based on a wish list he wrote but perhaps he can expand more on the goals!

@KendallWeihe
Copy link
Contributor

Trying to think back... yeah we don't want to reinvent the wheel and re-implement the mktemp unix tool. Perhaps what the original intent here was, to offer an option CLI parameter which instead of printing the results to the terminal stdout, it would create a file. Like for example, when we call web5 did create the current DX is to print to the terminal, but we could also offer a parameter to save it to a file. Thoughts?

@blackgirlbytes
Copy link
Contributor Author

oops that was my b! @KendallWeihe thanks for clariying!

@w3irdrobot
Copy link
Contributor

@KendallWeihe is the idea of adding an output flag that it adds a consistent way that works on unix-based and windows systems for saving to a file since on linux saving to a file could just be done by redirecting stdout to a file?

@KendallWeihe
Copy link
Contributor

@KendallWeihe is the idea of adding an output flag that it adds a consistent way that works on unix-based and windows systems for saving to a file since on linux saving to a file could just be done by redirecting stdout to a file?

Great point honestly. And yeah I think there is some marginal value to providing a first-class feature for file creation instead of relying on the developer to use their native systems file concatenation syntax, but it's definitely marginal. IMO it's a nice feature to have but arguably not a super high priority.

@w3irdrobot
Copy link
Contributor

w3irdrobot commented Oct 21, 2024

well if it's still desired to work on, i can do the work. shouldn't take long at all.

@KendallWeihe
Copy link
Contributor

well if it's still desired to work on, i can do the work. shouldn't take long at all.

Yeah let's do it!

@w3irdrobot
Copy link
Contributor

.take

Copy link

Thanks for taking this issue! Let us know if you have any questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants