Skip to content

Commit

Permalink
update ui
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar committed Aug 25, 2023
1 parent 3453440 commit 4b0d55b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
22 changes: 3 additions & 19 deletions cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,23 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/pingcap/tiup/pkg/environment"
"github.com/spf13/cobra"
)

func newLinkCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "link <component>[:version]",
Short: "Link component binary to $PATH",
Long: `[experimental feature]
Link component binary to $PATH`,
Short: "Link component binary to $TIUP_HOME/bin/",
Long: `[experimental] Link component binary to $TIUP_HOME/bin/`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
if len(args) != 1 {
return cmd.Help()
}
component, version := environment.ParseCompVersion(args[0])
if version == "" {
var err error
version, err = env.SelectInstalledVersion(component, version)
if err != nil {
return err
}
}
binPath, _ := env.BinaryPath(component, version)
target := filepath.Join(env.LocalPath("bin"), filepath.Base(binPath))
fmt.Printf("package %s provides these executables: %s\n", component, filepath.Base(binPath))
_ = os.Remove(target)
return os.Symlink(binPath, target)
return env.Link(component, version)
},
}
return cmd
Expand Down
21 changes: 16 additions & 5 deletions cmd/unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,34 @@ import (
"path/filepath"

"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/tui"
"github.com/spf13/cobra"
)

func newUnlinkCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "unlink <component>",
Short: "Unlink component binary to $PATH",
Long: `[experimental feature]
Unlink component binary in $PATH`,
Short: "Unlink component binary to $TIUP_HOME/bin/",
Long: `[experimental] Unlink component binary in $TIUP_HOME/bin/`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
if len(args) != 1 {
return cmd.Help()
}
component, _ := environment.ParseCompVersion(args[0])
target := filepath.Join(env.LocalPath("bin"), component)
component, version := environment.ParseCompVersion(args[0])
version, err := env.SelectInstalledVersion(component, version)
if err != nil {
return err
}
binPath, err := env.BinaryPath(component, version)
if err != nil {
return err
}
target := env.LocalPath("bin", filepath.Base(binPath))
if err := tui.PromptForConfirmOrAbortError("%s will be removed.\n Do you want to continue? [y/N]:", target); err != nil {
return err
}
return os.Remove(target)
},
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func (env *Environment) SelectInstalledVersion(component string, ver utils.Versi
})

errInstallFirst := errors.Annotatef(ErrInstallFirst, "use `tiup install %s` to install component `%s` first", component, component)
if !ver.IsEmpty() {
errInstallFirst = errors.Annotatef(ErrInstallFirst, "use `tiup install %s:%s` to install specified version", component, ver.String())
}

if ver.IsEmpty() || string(ver) == utils.NightlyVersionAlias {
var selected utils.Version
Expand Down Expand Up @@ -287,6 +290,45 @@ func (env *Environment) BinaryPath(component string, ver utils.Version) (string,
return env.v1Repo.BinaryPath(installPath, component, ver.String())
}

// Link add soft link to $TIUP_HOME/bin/
func (env *Environment) Link(component string, version utils.Version) error {
version, err := env.SelectInstalledVersion(component, version)
if err != nil {
return err
}
binPath, err := env.BinaryPath(component, version)
if err != nil {
return err
}

target := env.LocalPath("bin", filepath.Base(binPath))
backup := target + ".old"
exist := true
_, err = os.Stat(target)
if err != nil {
if !os.IsNotExist(err) {
return err
}
exist = false
}
if exist {
if err := os.Rename(target, backup); err != nil {
fmt.Printf("Backup of `%s` to `%s` failed.\n", target, backup)
return err
}
}

fmt.Printf("package %s provides these executables: %s\n", component, filepath.Base(binPath))

err = os.Symlink(binPath, target)
if err != nil {
os.Rename(backup, target)
} else {
os.Remove(backup)
}
return err
}

// ParseCompVersion parses component part from <component>[:version] specification
func ParseCompVersion(spec string) (string, utils.Version) {
if strings.Contains(spec, ":") {
Expand Down

0 comments on commit 4b0d55b

Please sign in to comment.