forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linker.go
49 lines (36 loc) · 1.07 KB
/
linker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package disk
import (
"strings"
"fmt"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type Linker struct {
Runner boshsys.CmdRunner
}
func (l *Linker) LinkTarget(location string) (target string, err error) {
isLinkedCommand := fmt.Sprintf(
"Get-Item %s -ErrorAction Ignore | Select -ExpandProperty Target -ErrorAction Ignore",
location,
)
isLinkedCommandArgs := strings.Split(isLinkedCommand, " ")
stdout, _, exitStatus, err := l.Runner.RunCommand(
isLinkedCommandArgs[0],
isLinkedCommandArgs[1:]...,
)
if err != nil && exitStatus == -1 {
return "", fmt.Errorf("failed to check for existing symbolic link: %s", err)
}
return strings.TrimSpace(stdout), nil
}
func (l *Linker) Link(location, target string) error {
createLinkCommand := fmt.Sprintf("cmd.exe /c mklink /d %s %s", location, target)
createLinkCommandArgs := strings.Split(createLinkCommand, " ")
_, _, _, err := l.Runner.RunCommand(
createLinkCommandArgs[0],
createLinkCommandArgs[1:]...,
)
if err != nil {
return fmt.Errorf("failed to create symbolic link: %s", err)
}
return nil
}