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

mktemp: add func to expose functionality (for use in nushell) #5479

Merged
merged 2 commits into from
Nov 7, 2023

Conversation

tskinn
Copy link
Contributor

@tskinn tskinn commented Oct 30, 2023

Tried to follow @tertsdiepraam guidance here

Link to #5465

@cakebaker cakebaker linked an issue Oct 30, 2023 that may be closed by this pull request
Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly how I imagined it! Thanks! I just have a few additional suggestions.

}

pub fn mktemp(
dir: &str,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to make this into a &Path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sounds good

@@ -441,7 +442,7 @@ pub fn uu_app() -> Command {
.arg(Arg::new(ARG_TEMPLATE).num_args(..=1))
}

pub fn dry_exec(tmpdir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<()> {
pub fn dry_exec(tmpdir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<PathBuf> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you happen to know why this is pub?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. It did seem a little odd.

Ok(path)
}

pub fn mktemp(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give this a bit of documentation?

@tskinn
Copy link
Contributor Author

tskinn commented Oct 30, 2023

So sorry! I should have had a quick try at implementing mktemp in nushell before bothering you.

After doing so I realized I wasn't exposing the stuff I needed. Since there is a little bit of logic around getting the system tmp dir I just rearranged where that was done exactly so we could reuse that in the pub mkdir function. None of the actual logic should have changed.

Basically what changed

  • removed pub from dry exec
  • created a new func for Options struct containing the logic for getting the system tmp dir which is reused in the original from function
  • exec functions don't print but return result with path
  • uumain prints to stdout
  • added new mktemp function

Now that I'm looking back I can probably cleanup the arguments to the mktemp function. Going to do that.

@tskinn
Copy link
Contributor Author

tskinn commented Oct 31, 2023

Ok @tertsdiepraam let me know what you think. I'm a rust noob so I'm sure theres lots to change. I can get nushell mktemp working with this api at least.

Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I think it should be done slightly differently, but it's very much going in the right direction, see my other comment. I hope my description is clear, if it's not, feel free to ask for clarification!

// If no template argument is given, `--tmpdir` is implied.
None => {
let tmpdir = Some(tmpdir.unwrap_or_else(env::temp_dir));
let template = DEFAULT_TEMPLATE;
(tmpdir, template.to_string())
}
Some(template) => {
let tmpdir = if env::var(TMPDIR_ENV_VAR).is_ok() && matches.get_flag(OPT_T) {
let tmpdir = if env::var(TMPDIR_ENV_VAR).is_ok() && treat_as_template {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nushell has their own env var handling, so I think that what we expose should not depend on env vars. Basically what I think should happen is that this method should not have been changed, but the Options struct should be exposed, so that they can construct it however they want. Just like how we did it in cp:

pub struct Options {

The mktemp function would then also take the Options struct instead of all the separate parameters.

Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Just one last small suggestion

@@ -352,17 +353,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

// Create the temporary file or directory, or simulate creating it.
let res = if dry_run {
dry_exec(&tmpdir, &prefix, rand, &suffix)
dry_exec(Path::new(&tmpdir), &prefix, rand, &suffix)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to fix this here, but could you add a TODO saying that we should get a PathBuf from clap instead of String?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah absolutely. Thanks for your patience by the way.

It seems like we are already getting the pathbuf from clap https://github.com/uutils/coreutils/blob/main/src/uu/mktemp/src/mktemp.rs#L413-L430 Maybe I'm looking at the wrong thing here.

But I see we can at least use PathBuf in the Params struct. I'll add a commit for that.

Copy link

github-actions bot commented Nov 7, 2023

GNU testsuite comparison:

Congrats! The gnu test tests/tail/retry is no longer failing!
Congrats! The gnu test tests/tail/symlink is no longer failing!

@@ -297,7 +298,7 @@ impl Params {
let num_rand_chars = j - i;

Ok(Self {
directory,
directory: directory.into(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool that we indeed already get the PathBuf from clap! However, we still need to fix this conversion from String inbetween, but I'll open an issue for that.

@tertsdiepraam
Copy link
Member

Thanks! Hope to see you on the nushell side to implement that part!

@tertsdiepraam tertsdiepraam merged commit 6678c17 into uutils:main Nov 7, 2023
tuxdna pushed a commit to tuxdna/coreutils that referenced this pull request Nov 8, 2023
…#5479)

* mktemp: add func to expose functionality

* mktemp: cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mktemp: expose functionality for nushell
3 participants