Skip to content

Commit b859b9f

Browse files
authored
refactory upgrade cmd (#3)
* Refactory upgrade cmd * Igore io close errors
1 parent c35ef6a commit b859b9f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

common/ioutil.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package common
2+
3+
// Printer for print the info
4+
type Printer interface {
5+
PrintErr(i ...interface{})
6+
Println(i ...interface{})
7+
Printf(format string, i ...interface{})
8+
}

version/upgrade.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"compress/gzip"
66
"fmt"
77
"github.com/google/go-github/v29/github"
8+
"github.com/linuxsuren/cobra-extension/common"
89
gh "github.com/linuxsuren/cobra-extension/github"
910
httpdownloader "github.com/linuxsuren/http-downloader/pkg"
1011
"github.com/spf13/cobra"
@@ -76,8 +77,18 @@ func (o *SelfUpgradeOption) RunE(cmd *cobra.Command, args []string) (err error)
7677
}
7778
return
7879
}
79-
f.Close()
80+
defer func() {
81+
_ = f.Close()
82+
}()
83+
84+
currentVersion := GetVersion()
85+
err = o.Download(cmd, version, currentVersion, targetPath)
86+
return
87+
}
8088

89+
// Download downloads the binary file from GitHub release
90+
// Org, Repo, Name is necessary
91+
func (o *SelfUpgradeOption) Download(log common.Printer, version, currentVersion, targetPath string) (err error) {
8192
// try to understand the version from user input
8293
switch version {
8394
case "dev":
@@ -98,12 +109,11 @@ func (o *SelfUpgradeOption) RunE(cmd *cobra.Command, args []string) (err error)
98109
}
99110

100111
// version review
101-
currentVersion := GetVersion()
102112
if currentVersion == version {
103-
cmd.Printf("no need to upgrade %s\n", o.Name)
113+
log.Printf("no need to upgrade %s\n", o.Name)
104114
return
105115
}
106-
cmd.Println(fmt.Sprintf("prepare to upgrade to %s", version))
116+
log.Println(fmt.Sprintf("prepare to upgrade to %s", version))
107117

108118
// download the tar file of target file
109119
tmpDir := os.TempDir()
@@ -121,7 +131,7 @@ func (o *SelfUpgradeOption) RunE(cmd *cobra.Command, args []string) (err error)
121131
o.downloadCount(version, runtime.GOOS)
122132
}()
123133
}
124-
cmd.Println("start to download from", fileURL)
134+
log.Println("start to download from", fileURL)
125135

126136
defer func() {
127137
_ = os.RemoveAll(output)

0 commit comments

Comments
 (0)