Skip to content

Commit

Permalink
feat: add --dev flag for upgrade command and update version handling
Browse files Browse the repository at this point in the history
- Add a new command-line flag `--dev` to the `upgrade` command
- Update the URL for downloading the latest version in the `DoQcadmin` function
- Add new fields `Latest`, `Stable`, and `Dev` to the `versionGet` struct
- Modify the `PreCheckLatestVersion` function to handle the new API response structure and the `--dev` flag
- Update the `ShowVersion` function to pass the `--dev` flag to `PreCheckLatestVersion`
- Add the `CDNVersionURL` constant in the `const.go` file

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Dec 4, 2023
1 parent f70deef commit 0e2ce57
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
9 changes: 6 additions & 3 deletions cmd/upgrade/q.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (
"github.com/easysoft/qcadmin/internal/pkg/util/log"
"github.com/easysoft/qcadmin/pkg/selfupdate"
"github.com/ergoapi/util/file"
uv "github.com/ergoapi/util/version"
"github.com/spf13/cobra"
)

type option struct {
log log.Logger
dev bool
}

func NewUpgradeQ(f factory.Factory) *cobra.Command {
Expand All @@ -39,18 +41,19 @@ func NewUpgradeQ(f factory.Factory) *cobra.Command {
up.DoQcadmin()
},
}
upq.Flags().BoolVarP(&up.dev, "dev", "", false, "upgrade to dev version")
return upq
}

func (up option) DoQcadmin() {
up.log.StartWait("fetch latest version from remote...")
lastVersion, lastType, err := version.PreCheckLatestVersion(up.log)
lastVersion, lastType, err := version.PreCheckLatestVersion(up.log, up.dev, "")
up.log.StopWait()
if err != nil {
up.log.Errorf("fetch latest version err, reason: %v", err)
return
}
if lastVersion == "" || lastVersion == common.Version || strings.Contains(common.Version, lastVersion) {
if lastVersion == "" || lastVersion == common.Version || strings.Contains(common.Version, lastVersion) || uv.LT(lastVersion, common.Version) {
up.log.Infof("The current version %s is the latest version", common.Version)
return
}
Expand All @@ -60,7 +63,7 @@ func (up option) DoQcadmin() {
return
}
up.log.StartWait(fmt.Sprintf("downloading version %s...", lastVersion))
assetURL := fmt.Sprintf("https://pkg.qucheng.com/qucheng/cli/stable/qcadmin_%s_%s", runtime.GOOS, runtime.GOARCH)
assetURL := fmt.Sprintf("https://pkg.zentao.net/cli/devops/v%s/qcadmin_%s_%s", lastVersion, runtime.GOOS, runtime.GOARCH)
if lastType == "github" {
assetURL = fmt.Sprintf("https://github.com/easysoft/quickon_cli/releases/download/%s/qcadmin_%s_%s", lastVersion, runtime.GOOS, runtime.GOARCH)
}
Expand Down
36 changes: 18 additions & 18 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,9 @@ const (
)

type versionGet struct {
Code int `json:"code"`
Data struct {
Name string `json:"name"`
Version string `json:"version"`
Sync time.Time `json:"sync"`
} `json:"data"`
Message string `json:"message"`
Timestamp int `json:"timestamp"`
Latest string `json:"latest"`
Stable string `json:"stable,omitempty"`
Dev string `json:"dev,omitempty"`
}

type versionInfo struct {
Expand Down Expand Up @@ -115,13 +110,15 @@ func (v versionInfo) ServerDeployed() bool {
}

// PreCheckLatestVersion 检查最新版本
func PreCheckLatestVersion(log logpkg.Logger) (version, t string, err error) {
version, _ = checkLastVersionFromGithub()
if version != "" {
log.Debugf("fetch version from github: %s", version)
return version, "github", nil
func PreCheckLatestVersion(log logpkg.Logger, dev bool, st string) (version, t string, err error) {
if st == "github" {
version, _ = checkLastVersionFromGithub()
if version != "" {
log.Debugf("fetch version from github: %s", version)
return version, "github", nil
}
}
version, err = checkLatestVersionFromAPI()
version, err = checkLatestVersionFromAPI(dev)
if err != nil {
return version, "api", err
}
Expand All @@ -141,14 +138,17 @@ func checkLastVersionFromGithub() (string, error) {
return tag.Name, nil
}

func checkLatestVersionFromAPI() (string, error) {
func checkLatestVersionFromAPI(dev bool) (string, error) {
lastVersion := &versionGet{}
client := req.C().SetLogger(nil).SetUserAgent(common.GetUG()).SetTimeout(time.Second * 5)
_, err := client.R().SetSuccessResult(lastVersion).Get(common.GetAPI("/api/release/last/qcadmin"))
_, err := client.R().SetSuccessResult(lastVersion).Get(common.CDNVersionURL)
if err != nil {
return "", err
}
return lastVersion.Data.Version, nil
if dev {
return lastVersion.Dev, nil
}
return lastVersion.Latest, nil
}

func ShowVersion(log logpkg.Logger) {
Expand Down Expand Up @@ -180,7 +180,7 @@ func ShowVersion(log logpkg.Logger) {
}

log.StartWait("check update...")
lastVersion, lastType, err := PreCheckLatestVersion(log)
lastVersion, lastType, err := PreCheckLatestVersion(log, false, "")
log.StopWait()
if err != nil {
log.Debugf("get update message err: %v", err)
Expand Down
1 change: 1 addition & 0 deletions common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const (
V2exGenerate204URL = "https://captive.v2ex.co/generate_204"
CloudflareEdgeTraceURL = "https://www.cloudflare.com/cdn-cgi/trace"
CneAPITokenHeader = "X-Auth-Token"
CDNVersionURL = "https://pkg.zentao.net/cli/devops/version.json"
)

const (
Expand Down

0 comments on commit 0e2ce57

Please sign in to comment.