How can I resolve a relative path to absolute path in golang? - go

Is there a api like 'path.resolve' in node? Or something can do the same?
For Example (nodejs code):
path.resolve("~/sample.sh")
Should got: /home/currentuser/sample.sh

Resolving ~ (denoting the user home) is a different story, and usually it's the shell that resolves this. For details see Expand tilde to home directory.
If you want to do it from Go code, you may use the user.Current() function to get details about the current user, including its home folder which will be User.HomeDir. But still, you'll have to handle replacing this yourself.
Original answer follows.
You may use path.Join() or filepath.Join().
For example:
base := "/home/bob"
fmt.Println(path.Join(base, "work/go", "src/github.com"))
Output:
/home/bob/work/go/src/github.com
You may use path.Clean() and filepath.Clean() to "remove" dots . and double dots .. from your path.
You may use filepath.Abs() to resolve relative paths and get an absolute (prepending the working directory if it's not absolute). filepath.Abs() also calls Clean() on the result.
For example:
fmt.Println(filepath.Abs("/home/bob/../alice"))
Outputs:
/home/alice <nil>
Try the examples on the Go Playground.
See related question: Resolving absolute path from relative path

Related

clang:how can fdebug-prefix-map use new path relative to user home path `~`?

I try rewrite the source file path to ~/src/lib by using fdebug-prefix-map.
I can confirm DW_AT_decl_file is rewritten to something like ~/src/lib/path.
But the result is lldb can't find the source file. If I change to a absolute path, it works fine.
How can I solve this?
You can use the target.source-map setting to remap location of source files. From (lldb) apropos source-map:
Source path remappings are used to track the change of location between a source file when built, and where it exists on the current system. It consists of an array of duples, the first element of each duple is some part (starting at the root) of the path to the file when it was built, and the second is where the remainder of the original build hierarchy is rooted on the local system. Each element of the array is checked in order and the first one that results in a match wins.
The usage looks something like:
(lldb) settings append target.source-map /foo /bar
Note that you use append here instead of set, because otherwise you'd overwrite the mapping every time you add an entry. You can check the mapping with:
(lldb) settings show target.source-map

KDB: How to convert relative path to absolute path?

Is there a way to convert a relative path to an absolute path in KDB?
For example:
filePath: `$concat[localPath,"\\",inProcessID,"\\",filename]
Which returns:
`..\..\code\products\Q\ShortLocator2\Request Files\1\Locate_CCL_11-13-2018_074736.csv
And then now I want to convert this to absolute path.
Ultimately you should solve the problem of why your "localPath" variable is relative in the first place, but here is an ugly function to solve your problem:
q){hsym `$("\\" sv neg[c]_"\\" vs system"cd"),"\\","\\" sv (c:count where ".."~/:a)_a:"\\" vs string x}[filePath]
`:C:\Users\code\products\Q\ShortLocator2\Request Files\1\Locate_CCL_11-13-201..
It is specific to windows
Do you need the canonical path to the file? If you are on linux readlink is commonly available and may help:
system "readlink -f ", filepath
But this obviously doesn't help for Windows (which it looks like you are using?). I'm not aware of a similar tool.
You can get the current working directory by typing...
q)homepath:`$system"pwd" // for Linux
,`/home/user
q)homepath:`$system"cd" // for Windows
,`C:\\Users\\user
To get the absolute path do...
q).Q.dd[hsym homepath; filepath]
`:/home/user/..
This should return the absolute path.
Does that answer your question?

Using paths in different packages convenient way

I have a program in which I use a lot "../" which is to go one level up
in the file system and run some process on the directory with specific name. I have a command line tool in Go.
I have 3 questions
there is nicer way to do it instead of “../“
is there a const with which I can use instead of “/“
if 2 is not available should I create “constants“ under that internal package to share the “/“ between packages since I need it in
many place (from diff packages...)
example
dir.zip("../"+tmpDirName, "../"+m.Id+".zip", "../"+tmpDirName)
Set a variable, and use that everywhere:
path := "../"
or
path := ".." + string(os.PathSeparator)
then later:
dir.zip(path+tmpDirName, path+m.Id+".zip", path+tmpDirName)
This makes it very easy to change the path in the future, via a command line option, configuration, or just editing the value.
Yes. os.PathSeparator is the OS-specific path separator for the current architecture.
n/a
declare a global const somewhere, but I would just use ".." everywhere
os.PathSeparator
use filepath.Join("..", someDir, someFilename)

TFS2010: Need the Absolute Path of the Source Directory

I am invoking VSDBCMD.EXE in my build process template, there is a custom setvar parameter that requires a reference to the current source directory, passing this path has become an unexpected challenge.
I've tried using relative paths and $(SourceDirectory) to no avail (it remains as the literal string "$(SourceDirectory)" when I see the debug output), the parameter needs an absolute path.
Is there any way to get the absolute path for the current source directory when the script runs?
In the DefaultTemplate build workflow there is a variable called SourcesDirectory that contains the absolute path.
If you pass it to an InvokeProcess you just type the variable name in the activity property, no $() around it.
It might be worth checking out this resource, where author makes use of ConvertWorkspaceItem within his build in order to pass in a string the disk location of a know target in source control

RUBYLIB Environment Path

So currently I have included the following in my .bashrc file.
export RUBYLIB=/home/git/project/app/helpers
I am trying to run rspec with a spec that has
require 'output_helper'
This file is in the helpers directory. My question is that when I change the export line to:
export RUBYLIB=/home/git/project/
It no longer finds the helper file. I thought that ruby should search the entire path I supply, and not just the outermost directory supplied? Is this the correct way to think about it? And if not, how can I make it so RUBY will search through all subdirectories and their subdirectories, etc?
Thanks,
Robin
Similar to PATH, you need to explicitly name the directory under which to look for libraries. However, this will not include any child directories within, so you will need to list any child sub-directories as well, delimiting them with a colon.
For example:
export RUBYLIB=/home/git/project:/home/git/project/app/helpers
As buruzaemon mentions, Ruby does not search subdirectories, so you need to include all the directories you want in your search path. However, what you probably want to do is:
require 'app/helpers/output_helper'
This way you aren't depending on the RUBYLIB environment variable being set a certain way. When you're deploying code to production, or collaborating with others, these little dependencies can make for annoying debugging sessions.
Also as a side note, you can specify . as a search path, rather than using machine-specific absolute paths.

Resources