Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type downloadOption struct {
KeepPart bool

// inner fields
name string
Tar bool
name string
Tar bool
Package *hdConfig
}

const (
Expand Down Expand Up @@ -122,13 +123,14 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
cfg := hdConfig{}

if err = yaml.Unmarshal(data, &cfg); err == nil {
hdPackage := &hdPackage{
hdPkg := &hdPackage{
Name: o.name,
Version: version,
OS: getReplacement(runtime.GOOS, cfg.Replacements),
Arch: getReplacement(runtime.GOARCH, cfg.Replacements),
VersionNum: strings.TrimPrefix(version, "v"),
}
o.Package = &cfg

if version == "latest" {
ghClient := pkg.ReleaseClient{
Expand All @@ -137,8 +139,8 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
}
ghClient.Init()
if asset, err := ghClient.GetLatestJCLIAsset(); err == nil {
hdPackage.Version = asset.TagName
hdPackage.VersionNum = strings.TrimPrefix(asset.TagName, "v")
hdPkg.Version = asset.TagName
hdPkg.VersionNum = strings.TrimPrefix(asset.TagName, "v")
} else {
fmt.Println(err, "cannot get the asset")
}
Expand All @@ -149,7 +151,7 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
tmp, _ := template.New("hd").Parse(cfg.URL)

var buf bytes.Buffer
if err = tmp.Execute(&buf, hdPackage); err == nil {
if err = tmp.Execute(&buf, hdPkg); err == nil {
url = buf.String()
} else {
return
Expand All @@ -158,7 +160,7 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
tmp, _ := template.New("hd").Parse(cfg.Filename)

var buf bytes.Buffer
if err = tmp.Execute(&buf, hdPackage); err == nil {
if err = tmp.Execute(&buf, hdPkg); err == nil {
url = fmt.Sprintf("https://github.com/%s/%s/releases/%s/download/%s",
org, repo, version, buf.String())

Expand All @@ -168,8 +170,24 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
}
}

if err = renderCmdWithArgs(cfg.PreInstall, hdPkg); err != nil {
return
}
if err = renderCmdWithArgs(cfg.Installation, hdPkg); err != nil {
return
}
if err = renderCmdWithArgs(cfg.PostInstall, hdPkg); err != nil {
return
}
if err = renderCmdWithArgs(cfg.TestInstall, hdPkg); err != nil {
return
}

o.Tar = cfg.Tar != "false"
if cfg.Binary != "" {
if cfg.Binary, err = renderTemplate(cfg.Binary, hdPkg); err != nil {
return
}
o.name = cfg.Binary
}
}
Expand All @@ -178,13 +196,50 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
return
}

func renderTemplate(text string, hdPkg *hdPackage) (result string, err error) {
tmp, _ := template.New("hd").Parse(text)

var buf bytes.Buffer
if err = tmp.Execute(&buf, hdPkg); err == nil {
result = buf.String()
}
return
}

func renderCmdWithArgs(cmd *cmdWithArgs, hdPkg *hdPackage) (err error) {
if cmd == nil {
return
}

if cmd.Cmd, err = renderTemplate(cmd.Cmd, hdPkg); err != nil {
return
}

for i := range cmd.Args {
arg := cmd.Args[i]
if cmd.Args[i], err = renderTemplate(arg, hdPkg); err != nil {
return
}
}
return
}

type hdConfig struct {
Name string
Filename string
Binary string
URL string `yaml:"url"`
Tar string
Replacements map[string]string
Installation *cmdWithArgs
PreInstall *cmdWithArgs
PostInstall *cmdWithArgs
TestInstall *cmdWithArgs
}

type cmdWithArgs struct {
Cmd string
Args []string
}

type hdPackage struct {
Expand Down
35 changes: 30 additions & 5 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func NewInstallCmd() (cmd *cobra.Command) {
opt := &installOption{}
cmd = &cobra.Command{
Use: "install",
Short: "Install a package from https://github.com/LinuxSuRen/hd-home",
Example: "hd install jenkins-zh/jenkins-cli/jcli -t 6",
PreRunE: opt.preRunE,
RunE: opt.runE,
}
Expand All @@ -29,6 +31,8 @@ func NewInstallCmd() (cmd *cobra.Command) {
flags.BoolVarP(&opt.ShowProgress, "show-progress", "", true, "If show the progress of download")
flags.BoolVarP(&opt.Fetch, "fetch", "", true,
"If fetch the latest config from https://github.com/LinuxSuRen/hd-home")
flags.BoolVarP(&opt.Download, "download", "", true,
"If download the package")
flags.IntVarP(&opt.Thread, "thread", "t", 4,
`Download file with multi-threads. It only works when its value is bigger than 1`)
flags.BoolVarP(&opt.KeepPart, "keep-part", "", false,
Expand All @@ -41,7 +45,8 @@ func NewInstallCmd() (cmd *cobra.Command) {

type installOption struct {
downloadOption
Mode string
Download bool
Mode string
}

func (o *installOption) preRunE(cmd *cobra.Command, args []string) (err error) {
Expand All @@ -50,8 +55,10 @@ func (o *installOption) preRunE(cmd *cobra.Command, args []string) (err error) {
}

func (o *installOption) runE(cmd *cobra.Command, args []string) (err error) {
if err = o.downloadOption.runE(cmd, args); err != nil {
return
if o.Download {
if err = o.downloadOption.runE(cmd, args); err != nil {
return
}
}

var source string
Expand All @@ -69,13 +76,31 @@ func (o *installOption) runE(cmd *cobra.Command, args []string) (err error) {
}

if err == nil {
fmt.Println("install", source, "to", target)
err = o.overWriteBinary(source, target)
if o.Package != nil && o.Package.PreInstall != nil {
if err = execCommand(o.Package.PreInstall.Cmd, o.Package.PreInstall.Args...); err != nil {
return
}
}

if o.Package != nil && o.Package.Installation != nil {
err = execCommand(o.Package.Installation.Cmd, o.Package.Installation.Args...)
} else {
err = o.overWriteBinary(source, target)
}

if err == nil && o.Package != nil && o.Package.PostInstall != nil {
err = execCommand(o.Package.PostInstall.Cmd, o.Package.PostInstall.Args...)
}

if err == nil && o.Package != nil && o.Package.TestInstall != nil {
err = execCommand(o.Package.TestInstall.Cmd, o.Package.TestInstall.Args...)
}
}
return
}

func (o *installOption) overWriteBinary(sourceFile, targetPath string) (err error) {
fmt.Println("install", sourceFile, "to", targetPath)
switch runtime.GOOS {
case "linux", "darwin":
if err = execCommand("chmod", "u+x", sourceFile); err != nil {
Expand Down