Vorrei creare un collegamento simbolico relativo in go utilizzando il pacchetto os.Creazione di un collegamento simbolico relativo tramite il pacchetto os
già contains the function: os.SymLink(oldname, newname string)
, ma non è possibile creare collegamenti simbolici relativi.
Per esempio, se si esegue il seguente:
package main
import (
"io/ioutil"
"os"
"path/filepath"
)
func main() {
path := "/tmp/rolfl/symexample"
target := filepath.Join(path, "symtarget.txt")
os.MkdirAll(path, 0755)
ioutil.WriteFile(target, []byte("Hello\n"), 0644)
symlink := filepath.Join(path, "symlink")
os.Symlink(target, symlink)
}
Crea il seguente nel mio file system:
$ ls -la /tmp/rolfl/symexample
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:21 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
-rw-r--r-- 1 rolf rolf 6 Feb 21 15:21 symtarget.txt
Come posso usare golang per creare il link simbolico relativo che assomiglia a:
$ ln -s symtarget.txt symrelative
$ ls -la
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:23 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
lrwxrwxrwx 1 rolf rolf 13 Feb 21 15:23 symrelative -> symtarget.txt
-rw-r--r-- 1 rolf rolf 6 Feb 21 15:21 symtarget.txt
Voglio qualcosa che è come il symrelative
sopra.
Devo ricorrere a os/exec
:
cmd := exec.Command("ln", "-s", "symtarget.txt", "symlink")
cmd.Dir = "/tmp/rolfl/symexample"
cmd.CombinedOutput()