Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue that the default selected version may be a preprelease version #1128

Merged
merged 8 commits into from
Feb 22, 2021
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
19 changes: 6 additions & 13 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/repository/v1manifest"
pkgver "github.com/pingcap/tiup/pkg/repository/version"
"github.com/pingcap/tiup/pkg/set"
"github.com/pingcap/tiup/pkg/tui"
"github.com/pingcap/tiup/pkg/version"
Expand Down Expand Up @@ -202,25 +201,19 @@ func showComponentVersions(env *environment.Environment, component string, opt l
for plat := range comp.Platforms {
versions := comp.VersionList(plat)
for ver, verinfo := range versions {
if pkgver.Version(ver).IsNightly() && ver == comp.Nightly {
platforms[version.NightlyVersion] = append(platforms[version.NightlyVersion], plat)
released[version.NightlyVersion] = verinfo.Released
} else {
platforms[ver] = append(platforms[ver], plat)
released[ver] = verinfo.Released
if ver == comp.Nightly {
key := fmt.Sprintf("%s -> %s", version.NightlyVersion, comp.Nightly)
platforms[key] = append(platforms[key], plat)
released[key] = verinfo.Released
}
platforms[ver] = append(platforms[ver], plat)
released[ver] = verinfo.Released
}
}
verList := []string{}
for v := range platforms {
if pkgver.Version(v).IsNightly() {
continue
}
verList = append(verList, v)
}
if comp.Nightly != "" {
verList = append(verList, version.NightlyVersion)
}
sort.Slice(verList, func(p, q int) bool {
return semver.Compare(verList[p], verList[q]) < 0
})
Expand Down
31 changes: 23 additions & 8 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/localdata"
pkgver "github.com/pingcap/tiup/pkg/repository/version"
"github.com/pingcap/tiup/pkg/version"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -81,26 +83,39 @@ which is used to uninstall tiup.

func removeComponents(env *environment.Environment, specs []string, all bool) error {
for _, spec := range specs {
var path string
paths := []string{}
if strings.Contains(spec, ":") {
parts := strings.SplitN(spec, ":", 2)
// after this version is deleted, component will have no version left. delete the whole component dir directly
if dir, err := ioutil.ReadDir(env.LocalPath(localdata.ComponentParentDir, parts[0])); err == nil && len(dir) <= 1 {
path = env.LocalPath(localdata.ComponentParentDir, parts[0])
dir, err := ioutil.ReadDir(env.LocalPath(localdata.ComponentParentDir, parts[0]))
if err != nil {
return errors.Trace(err)
}
if parts[1] == version.NightlyVersion {
for _, fi := range dir {
if pkgver.Version(fi.Name()).IsNightly() {
paths = append(paths, env.LocalPath(localdata.ComponentParentDir, parts[0], fi.Name()))
}
}
} else {
path = env.LocalPath(localdata.ComponentParentDir, parts[0], parts[1])
paths = append(paths, env.LocalPath(localdata.ComponentParentDir, parts[0], parts[1]))
}
if len(dir)-len(paths) < 1 {
paths = append(paths, env.LocalPath(localdata.ComponentParentDir, parts[0]))
}
} else {
if !all {
fmt.Printf("Use `tiup uninstall %s --all` if you want to remove all versions.\n", spec)
continue
}
path = env.LocalPath(localdata.ComponentParentDir, spec)
paths = append(paths, env.LocalPath(localdata.ComponentParentDir, spec))
}
err := os.RemoveAll(path)
if err != nil {
return err
for _, path := range paths {
if err := os.RemoveAll(path); err != nil {
return errors.Trace(err)
}
}

fmt.Printf("Uninstalled component `%s` successfully!\n", spec)
}
return nil
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/tikv/pd v1.1.0-beta.0.20210105064748-f4e7924b50b6
github.com/tj/assert v0.0.0-20190920132354-ee03d75cd160
github.com/tj/go-termd v0.0.2-0.20200115111609-7f6aeb166380
github.com/xo/usql v0.7.8
go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b
Expand Down
38 changes: 23 additions & 15 deletions pkg/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tiup/pkg/repository/v1manifest"
pkgver "github.com/pingcap/tiup/pkg/repository/version"
"github.com/pingcap/tiup/pkg/verbose"
"github.com/pingcap/tiup/pkg/version"
"golang.org/x/mod/semver"
)

Expand Down Expand Up @@ -141,7 +142,7 @@ func (env *Environment) UpdateComponents(specs []string, nightly, force bool) er
if component == tiupName {
continue
}
v1specs = append(v1specs, repository.ComponentSpec{ID: component, Version: v.String(), Force: force, Nightly: nightly})
v1specs = append(v1specs, repository.ComponentSpec{ID: component, Version: v.String(), Force: force})
}
return env.v1Repo.UpdateComponents(v1specs)
}
Expand Down Expand Up @@ -183,28 +184,34 @@ func (env *Environment) SelectInstalledVersion(component string, version pkgver.
}

// DownloadComponentIfMissing downloads the specific version of a component if it is missing
func (env *Environment) DownloadComponentIfMissing(component string, version pkgver.Version) (pkgver.Version, error) {
func (env *Environment) DownloadComponentIfMissing(component string, ver pkgver.Version) (pkgver.Version, error) {
versions, err := env.profile.InstalledVersions(component)
if err != nil {
return "", err
}

if ver.String() == version.NightlyVersion {
if ver, _, err = env.v1Repo.LatestNightlyVersion(component); err != nil {
return "", err
}
}

// Use the latest version if user doesn't specify a specific version and
// download the latest version if the specific component doesn't be installed

// Check whether the specific version exist in local
if version.IsEmpty() && len(versions) > 0 {
if ver.IsEmpty() && len(versions) > 0 {
sort.Slice(versions, func(i, j int) bool {
return semver.Compare(versions[i], versions[j]) < 0
})
version = pkgver.Version(versions[len(versions)-1])
ver = pkgver.Version(versions[len(versions)-1])
}

needDownload := version.IsEmpty()
if !version.IsEmpty() {
needDownload := ver.IsEmpty()
if !ver.IsEmpty() {
installed := false
for _, v := range versions {
if pkgver.Version(v) == version {
if pkgver.Version(v) == ver {
installed = true
break
}
Expand All @@ -213,18 +220,18 @@ func (env *Environment) DownloadComponentIfMissing(component string, version pkg
}

if needDownload {
fmt.Printf("The component `%s` is not installed; downloading from repository.\n", component)
err := env.downloadComponent(component, version, false)
fmt.Printf("The component `%s` version %s is not installed; downloading from repository.\n", component, ver.String())
err := env.downloadComponent(component, ver, false)
if err != nil {
return "", err
}
}

if version.IsEmpty() {
return env.SelectInstalledVersion(component, version)
if ver.IsEmpty() {
return env.SelectInstalledVersion(component, ver)
}

return version, nil
return ver, nil
}

// GetComponentInstalledVersion return the installed version of component.
Expand All @@ -233,12 +240,13 @@ func (env *Environment) GetComponentInstalledVersion(component string, version p
}

// BinaryPath return the installed binary path.
func (env *Environment) BinaryPath(component string, version pkgver.Version) (string, error) {
installPath, err := env.profile.ComponentInstalledPath(component, version)
func (env *Environment) BinaryPath(component string, ver pkgver.Version) (string, error) {
installPath, err := env.profile.ComponentInstalledPath(component, ver)
if err != nil {
return "", err
}
return env.v1Repo.BinaryPath(installPath, component, string(version))

return env.v1Repo.BinaryPath(installPath, component, ver.String())
}

// ParseCompVersion parses component part from <component>[:version] specification
Expand Down
2 changes: 1 addition & 1 deletion pkg/exec/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func PrepareCommand(p *PrepareCommandParams) (*exec.Cmd, error) {
}

if p.Version.IsEmpty() && p.CheckUpdate {
latestV, _, err := env.V1Repository().LatestStableVersion(p.Component, true)
latestV, _, err := env.V1Repository().LatestStableVersion(p.Component, false)
if err != nil {
return nil, err
}
Expand Down
29 changes: 18 additions & 11 deletions pkg/localdata/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/pingcap/errors"
pkgver "github.com/pingcap/tiup/pkg/repository/version"
"github.com/pingcap/tiup/pkg/utils"
"github.com/pingcap/tiup/pkg/version"
"golang.org/x/mod/semver"
)

Expand Down Expand Up @@ -79,9 +80,9 @@ func (p *Profile) Root() string {
}

// GetComponentInstalledVersion return the installed version of component.
func (p *Profile) GetComponentInstalledVersion(component string, version pkgver.Version) (pkgver.Version, error) {
if !version.IsEmpty() {
return version, nil
func (p *Profile) GetComponentInstalledVersion(component string, ver pkgver.Version) (pkgver.Version, error) {
if !ver.IsEmpty() && ver.String() != version.NightlyVersion {
return ver, nil
}
versions, err := p.InstalledVersions(component)
if err != nil {
Expand All @@ -92,15 +93,21 @@ func (p *Profile) GetComponentInstalledVersion(component string, version pkgver.
// report an error if the specific component doesn't be installed

// Check whether the specific version exist in local
if len(versions) > 0 {
sort.Slice(versions, func(i, j int) bool {
return semver.Compare(versions[i], versions[j]) < 0
})
version = pkgver.Version(versions[len(versions)-1])
} else {
return "", fmt.Errorf("component not installed, please try `tiup install %s` to install it", component)
if len(versions) == 0 {
return "", errors.Errorf("component not installed, please try `tiup install %s` to install it", component)
}
return version, nil
sort.Slice(versions, func(i, j int) bool {
return semver.Compare(versions[i], versions[j]) < 0
})
if ver.String() != version.NightlyVersion {
for i := len(versions); i > 0; i-- {
if pkgver.Version(versions[i-1]).IsNightly() {
return pkgver.Version(versions[i-1]), nil
}
}
return "", errors.Errorf("component(nightly) not installed, please try `tiup install %s:nightly` to install it", component)
}
return pkgver.Version(versions[len(versions)-1]), nil
}

// ComponentInstalledPath returns the path where the component installed
Expand Down
3 changes: 3 additions & 0 deletions pkg/repository/clone_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ func combineVersions(versions *[]string,
continue
}
for _, selectedVersion := range selectedVersions {
if selectedVersion == version.NightlyVersion {
selectedVersion = manifest.Nightly
}
_, found := versions[selectedVersion]
// Some TiUP components won't be bound version with TiDB, if cannot find
// selected version we download the latest version to as a alternative
Expand Down
2 changes: 1 addition & 1 deletion pkg/repository/merge_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func UpdateManifestForPublish(m *v1manifest.Component,
}
}

if strings.Contains(ver, version.NightlyVersion) {
if pkgver.Version(ver).IsNightly() {
m.Nightly = ver
}

Expand Down
Loading