Skip to content

Commit

Permalink
Support to set the targert directory (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored May 20, 2022
1 parent aa3f42f commit 8a5a1dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 9 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
return
}

if o.Package.TargetDirectory == "" {
o.Package.TargetDirectory = "/usr/local/bin"
}
if err = sysos.MkdirAll(o.Package.TargetDirectory, 0750); err != nil {
return
}

// aka go get github.com/xxx/xxx
if o.fromSource {
err = o.installFromSource()
Expand Down Expand Up @@ -162,6 +169,7 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
Output: o.Output,
CleanPackage: o.CleanPackage,
AdditionBinaries: o.Package.AdditionBinaries,
TargetDirectory: o.Package.TargetDirectory,
}
// install requirements tools in the post phase
if len(o.Package.Requirements) > 0 {
Expand Down Expand Up @@ -256,7 +264,7 @@ func (o *installOption) installFromSource() (err error) {
if o.Package != nil && o.Package.TargetBinary != "" {
targetName = o.Package.TargetBinary
}
err = is.OverWriteBinary(binaryPath, fmt.Sprintf("/usr/local/bin/%s", targetName))
err = is.OverWriteBinary(binaryPath, path.Join(o.Package.TargetDirectory, targetName))
}
return
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/installer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func (o *Installer) Install() (err error) {
if o.Tar {
if err = o.extractFiles(tarFile, o.Name); err == nil {
source = fmt.Sprintf("%s/%s", filepath.Dir(tarFile), o.Name)
target = fmt.Sprintf("/usr/local/bin/%s", targetBinary)
target = path.Join(o.TargetDirectory, targetBinary)
} else {
err = fmt.Errorf("cannot extract %s from tar file, error: %v", tarFile, err)
}
} else {
source = o.Source
target = fmt.Sprintf("/usr/local/bin/%s", targetBinary)
target = path.Join(o.TargetDirectory, targetBinary)
}

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

for i := range o.AdditionBinaries {
addition := o.AdditionBinaries[i]
if err = o.OverWriteBinary(addition, fmt.Sprintf("/usr/local/bin/%s", filepath.Base(addition))); err != nil {
if err = o.OverWriteBinary(addition, path.Join(o.TargetDirectory, filepath.Base(addition))); err != nil {
return
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/installer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type HDConfig struct {
FormatOverrides PackagingFormat `yaml:"formatOverrides"`
Binary string `yaml:"binary"`
TargetBinary string `yaml:"targetBinary"`
TargetDirectory string `yaml:"targetDirectory"`
AdditionBinaries []string `yaml:"additionBinaries"`
FromSource bool `yaml:"fromSource"`
URL string `yaml:"url"`
Expand Down Expand Up @@ -53,6 +54,7 @@ type Installer struct {
Package *HDConfig
Tar bool
Output string
TargetDirectory string
Source string
Name string
CleanPackage bool
Expand Down

0 comments on commit 8a5a1dd

Please sign in to comment.