willeccles / cpm

🌵 A wrapper for package managers to make them consistent for those of us who are lazy.
MIT License
68 stars 13 forks source link

from: fallback to command for non-existent files #51

Closed eepykate closed 2 years ago

eepykate commented 2 years ago

example (From a dir without that filename): cpm f test will be replaced with cpm f /bin/test, which will probably be coreutils.

willeccles commented 2 years ago

Could you perhaps make this part of/adjacent to the resolve function? It feels a little out of place in the middle of arg parsing.

eepykate commented 2 years ago

Like that?

willeccles commented 2 years ago

I think putting it in the resolve() function would be good.

eepykate commented 2 years ago

I mean, resolve is currently only used in xbps.

Do you want me to add it to every other PM?

eepykate commented 2 years ago

to be clear,

  cd -P "${1%/*}" 2>/dev/null || PWD="${1%/*}"

what's this meant to do?

willeccles commented 2 years ago

Rename this PR and make it both update resolve() and use it for other PMs as well.

willeccles commented 2 years ago

to be clear,

  cd -P "${1%/*}" 2>/dev/null || PWD="${1%/*}"

what's this meant to do?

Example: I want to know what package provides /bin/something, so I do cpm from /bin/something. This fails. There are two possible reasons:

  1. /bin is a symlink to /usr/bin
  2. /bin/something is a symlink to something else

This function resolves symlinks in paths so that this does not happen. You can just add this functionality to it.

eepykate commented 2 years ago

does it.. even work?

image

eepykate commented 2 years ago

Oh, the next line makes it work

yeah i'm never gonna look at those lines again, it hurts

eepykate commented 2 years ago

@willeccles You might wanna link the issue to this PR in this PR's sidebar btw.

willeccles commented 2 years ago

Oh, the next line makes it work

yeah i'm never gonna look at those lines again, it hurts

You'll never guess who I got them from.

willeccles commented 2 years ago

Does this actually work with more than one argument?

eepykate commented 2 years ago

You'll never guess who I got them from.

honestly the natural leadon from that is that I did it? ​ Fairly sure I didn't, and I didn't even know that -P existed. Foxsouns, I guess?

Does this actually work with more than one argument?

Yeah.

willeccles commented 2 years ago

At least xbps only allows one argument, and I can't imagine a use-case for multiple anyway. I'd change all the $@ to $1 and make the function only take one argument.

eepykate commented 2 years ago

Oh, wait, it did work but i think i broke it.

eepykate commented 2 years ago

At least xbps only allows one argument, and I can't imagine a use-case for multiple anyway. I'd change all the $@ to $1 and make the function only take one argument.

Heavily disagree.

Even if some PMs only support one, others support more, and the ones that don't support >1 will have an error message.

willeccles commented 2 years ago

The xbps error message is literally just printing the usage information. I also can't imagine why you'd want to do cpm from file1 file2, especially if they're from different packages anyway.

eepykate commented 2 years ago

I also can't imagine why you'd want to do

So you don't have to run multiple commands..? The whole reason cpm exists is for laziness.

If this was files, I'd agree, but for from, nah.

willeccles commented 2 years ago

At any rate, like we do for other things, PMs which only support one argument should only be given one.

eepykate commented 2 years ago

oh, that explains it

resolve() is broken anyway

eepykate commented 2 years ago

image

first == my edits

second == original

willeccles commented 2 years ago

What is test?

eepykate commented 2 years ago

image

eepykate commented 2 years ago

I assume that's more clear.

willeccles commented 2 years ago

This does not happen on my machine.

eepykate commented 2 years ago

It does on mine.

eepykate commented 2 years ago

Oh, it's my changes outside of the function it seems

eepykate commented 2 years ago

in my defense that function is a curse from satan and you should have just used realpath

eepykate commented 2 years ago

no thoughts, head empty

eepykate commented 2 years ago

image

resolve() {
  cd -P "${1%/*}" 2>/dev/null || PWD="${1%/*}"
  printf '%s\n' "${PWD}/${1##*/}"
}
realpath /usr/bin/test
cd /usr/bin
resolve test
resolve /usr/bin/test

sorry for spam

willeccles commented 2 years ago

in my defense that function is a curse from satan and you should have just used realpath

realpath isn't POSIX, though.

eepykate commented 2 years ago

pain

honestly, i'd just not bother resolving symlinks at all. It doesn't work, and there's no good solution, especially not a short & understandable one.

Also, half the reason I caved into splitting f/F was because you wanted relative paths, which resolve() doesn't even support.....

willeccles commented 2 years ago

I have fixed the resolve function. Also, xbps does not work with symlinks, and other PMs may also not work with symlinks. Here's a fixed version, I think:

resolve() {
  p="${1%/*}"
  [ -f "$p" ] && p="."
  cd -P "$p" 2>/dev/null || PWD="$p"
  printf '%s\n' "${PWD}/${1##*/}"
}
eepykate commented 2 years ago

oh, i was just going to go back to the original code because it is literally much better

willeccles commented 2 years ago

The -f here may need to be updated, just in case you try to use it on a special file or something, I suppose.

eepykate commented 2 years ago
  1. i'll have to remove the quotes for multiple arguments to work with a function, ergo breaking spaces
  2. resolve() is from hell and i hate it
  3. it's a simple one line improvement
willeccles commented 2 years ago

Just don't support multiple arguments.

eepykate commented 2 years ago

No.

willeccles commented 2 years ago

Actually, I think we can improve my fixed version:

resolve() {
  p="${1%/*}"
  [ "$p" = "$1" ] && p="."
  cd -P "$p" 2>/dev/null || PWD="$p"
  printf '%s\n' "${PWD}/${1##*/}"
}
willeccles commented 2 years ago

I'll even commit this myself to save you the trouble.

eepykate commented 2 years ago

:+1: Unless it's a complete rewrite of it, I would much prefer never touching resolve() again.

willeccles commented 2 years ago

Your fix is a pretty trivial one-line addition to the resolve function, and adding it to all PMs is quite simple.

eepykate commented 2 years ago

But, adding resolve() to all PMs is a downgrade.

willeccles commented 2 years ago

But, adding resolve() to all PMs is a downgrade.

How?

eepykate commented 2 years ago
  1. resolve() only exists to fix xbps being bad
  2. other PMs support multiple arguments, and there is no good way to make this function that only exists to fix xbps being bad support multiple arguments.
willeccles commented 2 years ago
1. resolve() only exists to fix xbps being bad

2. other PMs support multiple arguments, and there is no good way to make this function _that only exists to fix xbps being bad_ support multiple arguments.

I only created it for xbps, but I have no idea if other package managers require it as well. Not to mention, your current patch doesn't even support your multiple arguments stance.

eepykate commented 2 years ago

doesn't even support your multiple arguments stance.

Difference is, that's for one scenario, and not a constant thing.

willeccles commented 2 years ago

doesn't even support your multiple arguments stance.

Difference is, that's for one scenario, and not a constant thing.

Inconsistently handling multiple arguments is much worse than consistently allowing only one argument.

eepykate commented 2 years ago

Heavily disagree.

I'd prefer sometimes being shit (but that time also has a different improvement) over always being shit (even when not using the addition).

willeccles commented 2 years ago

I don't think it's always bad in either case. If you want to lookup multiple files at once and your PM allows it, you can use the PM directly if you'd prefer not to write two commands. If you moved your change to the resolve function and then applied that to all PMs, every package manager benefits from both symlink resolution and your patch. If you also want to make it work for multiple arguments, you can do that, too. Just don't do it for PMs which only support one.