Skip to content

Commit 8a5a1dd

Browse files
authored
Support to set the targert directory (#243)
1 parent aa3f42f commit 8a5a1dd

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

cmd/install.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
134134
return
135135
}
136136

137+
if o.Package.TargetDirectory == "" {
138+
o.Package.TargetDirectory = "/usr/local/bin"
139+
}
140+
if err = sysos.MkdirAll(o.Package.TargetDirectory, 0750); err != nil {
141+
return
142+
}
143+
137144
// aka go get github.com/xxx/xxx
138145
if o.fromSource {
139146
err = o.installFromSource()
@@ -162,6 +169,7 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
162169
Output: o.Output,
163170
CleanPackage: o.CleanPackage,
164171
AdditionBinaries: o.Package.AdditionBinaries,
172+
TargetDirectory: o.Package.TargetDirectory,
165173
}
166174
// install requirements tools in the post phase
167175
if len(o.Package.Requirements) > 0 {
@@ -256,7 +264,7 @@ func (o *installOption) installFromSource() (err error) {
256264
if o.Package != nil && o.Package.TargetBinary != "" {
257265
targetName = o.Package.TargetBinary
258266
}
259-
err = is.OverWriteBinary(binaryPath, fmt.Sprintf("/usr/local/bin/%s", targetName))
267+
err = is.OverWriteBinary(binaryPath, path.Join(o.Package.TargetDirectory, targetName))
260268
}
261269
return
262270
}

pkg/installer/process.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ func (o *Installer) Install() (err error) {
2626
if o.Tar {
2727
if err = o.extractFiles(tarFile, o.Name); err == nil {
2828
source = fmt.Sprintf("%s/%s", filepath.Dir(tarFile), o.Name)
29-
target = fmt.Sprintf("/usr/local/bin/%s", targetBinary)
29+
target = path.Join(o.TargetDirectory, targetBinary)
3030
} else {
3131
err = fmt.Errorf("cannot extract %s from tar file, error: %v", tarFile, err)
3232
}
3333
} else {
3434
source = o.Source
35-
target = fmt.Sprintf("/usr/local/bin/%s", targetBinary)
35+
target = path.Join(o.TargetDirectory, targetBinary)
3636
}
3737

3838
if err == nil {
@@ -51,7 +51,7 @@ func (o *Installer) Install() (err error) {
5151

5252
for i := range o.AdditionBinaries {
5353
addition := o.AdditionBinaries[i]
54-
if err = o.OverWriteBinary(addition, fmt.Sprintf("/usr/local/bin/%s", filepath.Base(addition))); err != nil {
54+
if err = o.OverWriteBinary(addition, path.Join(o.TargetDirectory, filepath.Base(addition))); err != nil {
5555
return
5656
}
5757
}

pkg/installer/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type HDConfig struct {
1414
FormatOverrides PackagingFormat `yaml:"formatOverrides"`
1515
Binary string `yaml:"binary"`
1616
TargetBinary string `yaml:"targetBinary"`
17+
TargetDirectory string `yaml:"targetDirectory"`
1718
AdditionBinaries []string `yaml:"additionBinaries"`
1819
FromSource bool `yaml:"fromSource"`
1920
URL string `yaml:"url"`
@@ -53,6 +54,7 @@ type Installer struct {
5354
Package *HDConfig
5455
Tar bool
5556
Output string
57+
TargetDirectory string
5658
Source string
5759
Name string
5860
CleanPackage bool

0 commit comments

Comments
 (0)