#resolve a path with a '~'

1 messages · Page 1 of 1 (latest)

viral zinc
#

Hi all, having trouble finding the right way to open a file path that starts with a ~. Seems like std.fs.cwd().realpathAlloc(allocater, "~"), std.fs.accessAbsolute() etc can't access the file path when it starts with ~ . Is there a function that can resolve the ~? Thanks!

rancid frost
#

the "easy" way would be to check if the path starts with ~/, and if it does, find and open $HOME as directory and use Dir.openFile with the rest of the path to open the file

signal gull
#

I wonder if std.fs.path.resolve handles this... 🤔

#

nope

rancid frost
#

it doesn't because it potentially requires allocation

signal gull
#

i mean resolve(Posix) takes in an allocator

#

but that doesn't matter anyways because the only syscall in resolvePosix is via getCwdAlloc which obviously doesn't help resolve ~ so yeha :(

rancid frost
#

oh right, its immutable

#

I could see it doing ~ though, thats not a syscall

tired hawk
#

Isn't that a shell thing though

signal gull
#

yeah that's what i was thinking actually

#

~ is just subbed with $HOMEDIR

tired hawk
#

a good shell shouldn't pass ~ into any programs

rancid frost
#

eg. user input, configuration, etc.

tired hawk
#

true, but I'm not sure that should be handled by the std.fs api

signal gull
#

i agree

#

check out known-folders btw

tired hawk
#

yeah, that's where I would prefer it to be

#

a good, community approved package

signal gull
#

I, Community, approve this package

viral zinc
#

While I agree that std.fs might not be the right place for it, I guess I expected something in std.os or something to know how to resolve ~ in a path to $HOMEDIR. But this answers my question, in any case. Thanks!

tired hawk
#

what shell are you using anyway?

viral zinc
#

I use zsh, but ~ can appear in user input or in configuration files, etc.

tired hawk
#

ahh, gotcha

viral zinc
#

in this case a file that was being loaded that tells this program where to find other things had a ~ in it

tired hawk
#

well, in case you decide to roll it yourself, you can get the $HOME variable from std.process.getEnvVarOwned

viral zinc
#

I might file an issue about this and see what they say, I can see myself bumping into this again in the future

tired hawk
#

I'm very skeptical this would be added to the stdlib, but you're free to try

coarse island
#

A general filepath package should probably be added IMO

soft fossil
#

I implemented this for Go and my package has been used in almost all major Go programs in the ecosystem (i.e. all the HashiCorp tools, Kubernetes, Docker, etc.). If you want to steal this logic, it has been production proven for almost a decade: https://github.com/mitchellh/go-homedir/blob/main/homedir.go

Its surprisingly messy to get the home directory, but not overly complicated! 🙂

viral zinc