Skip to content

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

Merged
tertsdiepraam merged 2 commits intouutils:mainfrom
tskinn:main
Nov 7, 2023
Merged

mktemp: add func to expose functionality (for use in nushell)#5479
tertsdiepraam merged 2 commits intouutils:mainfrom
tskinn:main

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
Collaborator

@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
Collaborator

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

}

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
Collaborator

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
Collaborator

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
Collaborator

@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!

}
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
Collaborator

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
Collaborator

@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

// 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
Collaborator

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.

@github-actions
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!


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

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
Collaborator

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