Skip to content

Commit

Permalink
Merge pull request #8 from MTVersionManager/removecmd
Browse files Browse the repository at this point in the history
Add remove command and do some other improvements
  • Loading branch information
leomick authored Nov 30, 2024
2 parents 7824f5e + dea8092 commit 82ddfc0
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 21 deletions.
22 changes: 18 additions & 4 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import (
)

type installModel struct {
installer install.Model
installer install.Model
installed bool
version string
pluginName string
}

func installInitialModel(plugin mtvmplugin.Plugin, pluginName string, version string) installModel {
downloadModel := install.New(plugin, pluginName, version)
return installModel{
installer: downloadModel,
installer: downloadModel,
installed: false,
version: version,
pluginName: pluginName,
}
}

Expand All @@ -28,13 +34,21 @@ func (m installModel) Init() tea.Cmd {
}

func (m installModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case install.InstalledMsg:
m.installed = true
return m, tea.Quit
}
var cmd tea.Cmd
m.installer, cmd = m.installer.Update(msg)
return m, cmd
}

func (m installModel) View() string {
return m.installer.View()
if !m.installed {
return m.installer.View()
}
return fmt.Sprintf("%v Installed version %v of %v\n", shared.CheckMark, m.version, m.pluginName)
}

// installCmd represents the install command
Expand All @@ -45,7 +59,7 @@ var installCmd = &cobra.Command{
For example:
If you run "mtvm install go latest" it will install the latest version of go`,
Args: cobra.ExactArgs(2),
Aliases: []string{"i"},
Aliases: []string{"i", "in"},
Run: func(cmd *cobra.Command, args []string) {
err := createInstallDir()
if err != nil {
Expand Down
93 changes: 92 additions & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,77 @@ package cmd

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

"github.com/MTVersionManager/mtvm/shared"
"github.com/MTVersionManager/mtvmplugin"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)

type removeModel struct {
plugin mtvmplugin.Plugin
version string
pluginName string
spinner spinner.Model
done bool
}

func removeInitialModel(plugin mtvmplugin.Plugin, version string, tool string) removeModel {
spin := spinner.New()
spin.Spinner = spinner.Dot
return removeModel{
plugin: plugin,
version: version,
pluginName: tool,
spinner: spin,
done: false,
}
}

func (m removeModel) Init() tea.Cmd {
return tea.Batch(m.spinner.Tick, remove(m.plugin, m.version, filepath.Join(shared.Configuration.InstallDir, m.pluginName), shared.Configuration.PathDir))
}

func (m removeModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case shared.SuccessMsg:
if msg == "remove" {
m.done = true
return m, tea.Quit
}
case error:
log.Fatal(msg)
}
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}

func (m removeModel) View() string {
if m.done {
return fmt.Sprintf("%v Successfully removed version %v of %v\n", shared.CheckMark, m.version, m.pluginName)
}
return fmt.Sprintf("%v Removing version %v of %v\n", m.spinner.View(), m.version, m.pluginName)
}

func remove(plugin mtvmplugin.Plugin, version string, installDir string, pathDir string) tea.Cmd {
return func() tea.Msg {
currentVer, err := plugin.GetCurrentVersion(installDir, pathDir)
if err != nil {
return err
}
err = plugin.Remove(filepath.Join(installDir, version), pathDir, version == currentVer)
if err != nil {
return err
}
return shared.SuccessMsg("remove")
}
}

// removeCmd represents the remove command
var removeCmd = &cobra.Command{
Use: "remove [tool] [version]",
Expand All @@ -16,7 +83,31 @@ For example:
Args: cobra.ExactArgs(2),
Aliases: []string{"rm", "r"},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("remove called")
plugin, err := shared.LoadPlugin(args[0])
if err != nil {
log.Fatal(err)
}
version := args[1]
if version == "latest" {
version, err = plugin.GetLatestVersion()
if err != nil {
log.Fatal(err)
}
}
installed, err := shared.IsVersionInstalled(args[0], version)
if err != nil {
log.Fatal(err)
}
if installed {
p := tea.NewProgram(removeInitialModel(plugin, version, args[0]))
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
} else {
fmt.Println("That version is not installed so you can't remove it")
os.Exit(1)
}
},
}

Expand Down
97 changes: 87 additions & 10 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cmd

import (
"fmt"
"github.com/MTVersionManager/mtvm/components/install"
"github.com/MTVersionManager/mtvm/shared"
"github.com/MTVersionManager/mtvmplugin"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"log"
"os"
Expand All @@ -12,6 +15,75 @@ import (
"github.com/spf13/cobra"
)

type useInstallModel struct {
install install.Model
spinner spinner.Model
installed bool
used bool
plugin mtvmplugin.Plugin
pluginName string
version string
}

func useInstallInitialModel(plugin mtvmplugin.Plugin, pluginName string, version string) useInstallModel {
installer := install.New(plugin, pluginName, version)
spin := spinner.New()
spin.Spinner = spinner.Dot
return useInstallModel{
install: installer,
spinner: spin,
installed: false,
used: false,
plugin: plugin,
pluginName: pluginName,
version: version,
}
}

func (m useInstallModel) Init() tea.Cmd {
return tea.Batch(m.spinner.Tick, m.install.Init())
}

func (m useInstallModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case install.InstalledMsg:
m.installed = true
cmds = append(cmds, Use(m.plugin, m.pluginName, m.version))
case shared.SuccessMsg:
if msg == "use" {
m.used = true
return m, tea.Quit
}
}
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
cmds = append(cmds, cmd)
m.install, cmd = m.install.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m useInstallModel) View() string {
if !m.installed {
return m.install.View()
}
installSuccess := fmt.Sprintf("%v Installed version %v of %v\n", shared.CheckMark, m.version, m.pluginName)
if !m.used {
return fmt.Sprintf("%v%v Setting version of %v to %v\n", installSuccess, m.spinner.View(), m.pluginName, m.version)
}
return fmt.Sprintf("%v%v Set version of %v to %v\n", installSuccess, shared.CheckMark, m.pluginName, m.version)
}

func Use(plugin mtvmplugin.Plugin, tool string, version string) tea.Cmd {
return func() tea.Msg {
err := plugin.Use(filepath.Join(shared.Configuration.InstallDir, tool, version), shared.Configuration.PathDir)
if err != nil {
return err
}
return shared.SuccessMsg("use")
}
}

// useCmd represents the use command
var useCmd = &cobra.Command{
Use: "use [tool] [version]",
Expand Down Expand Up @@ -46,23 +118,28 @@ So if you run go version it will print the version number 1.23.3`,
log.Fatal(err)
}
if installFlagUsed && !versionInstalled {
p := tea.NewProgram(installInitialModel(plugin, args[0], version))
err = createPathDir()
if err != nil {
log.Fatal(err)
}
p := tea.NewProgram(useInstallInitialModel(plugin, args[0], version))
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
} else if !versionInstalled {
fmt.Println("That version is not installed.")
os.Exit(1)
} else {
err = createPathDir()
if err != nil {
log.Fatal(err)
}
err = plugin.Use(filepath.Join(shared.Configuration.InstallDir, args[0], version), shared.Configuration.PathDir)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v Set version of %v to %v\n", shared.CheckMark, args[0], version)
}
err = createPathDir()
if err != nil {
log.Fatal(err)
}
err = plugin.Use(filepath.Join(shared.Configuration.InstallDir, args[0], version), shared.Configuration.PathDir)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Set version of %v to %v\n", args[0], version)
case installFlagUsed:
fmt.Println("You need to specify a version to install.")
err = cmd.Usage()
Expand Down
2 changes: 0 additions & 2 deletions components/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case downloadProgress.DownloadedMsg:
m.installing = false
cmds = append(cmds, Install(m.plugin, shared.Configuration.InstallDir, m.pluginName, m.version))
case InstalledMsg:
return m, tea.Quit
}
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/MTVersionManager/mtvm
go 1.23.3

require (
github.com/MTVersionManager/goplugin v0.0.0-20241123220718-a0fdaa16d1fc
github.com/MTVersionManager/mtvmplugin v0.0.0-20241123190034-39e07c9b14ee
github.com/MTVersionManager/goplugin v0.0.0-20241129191057-3b297f24bb72
github.com/MTVersionManager/mtvmplugin v0.0.0-20241129190150-d9f58a7a95b9
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.2.3
github.com/spf13/cobra v1.8.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ github.com/MTVersionManager/goplugin v0.0.0-20241123204902-3c4951b6c6f9 h1:n6uet
github.com/MTVersionManager/goplugin v0.0.0-20241123204902-3c4951b6c6f9/go.mod h1:gRYM+g+pYyR1t97EwpR51y7cUC9amD7g0AHaX7PFswM=
github.com/MTVersionManager/goplugin v0.0.0-20241123220718-a0fdaa16d1fc h1:A+KSXk3SYc1HQYYDRPTV+NXahR5zg7AjPEFT3zhUI7A=
github.com/MTVersionManager/goplugin v0.0.0-20241123220718-a0fdaa16d1fc/go.mod h1:gRYM+g+pYyR1t97EwpR51y7cUC9amD7g0AHaX7PFswM=
github.com/MTVersionManager/goplugin v0.0.0-20241129191057-3b297f24bb72 h1:/CIcgbUXBMYaeb0NXMvHVjX9qQlpsfEPPcTaRhqv6ug=
github.com/MTVersionManager/goplugin v0.0.0-20241129191057-3b297f24bb72/go.mod h1:m6goQ0JZthasMqby+9JgAvfWjje6vZqpeuY/upEpWzQ=
github.com/MTVersionManager/mtvmplugin v0.0.0-20241120211230-908487a55167 h1:vD7a1GuntMhCIeY7j1EJ1yUW/btyU9l5lJIOyIWJr1g=
github.com/MTVersionManager/mtvmplugin v0.0.0-20241120211230-908487a55167/go.mod h1:eo3CICn6Ovu09INVMm0iInMDl8hUgHYSCIITB0IVLJI=
github.com/MTVersionManager/mtvmplugin v0.0.0-20241123190034-39e07c9b14ee h1:G4pvjdAU0IlcEzL3wvB96XqhQIi4r8QN4Vz552yWG7E=
github.com/MTVersionManager/mtvmplugin v0.0.0-20241123190034-39e07c9b14ee/go.mod h1:eo3CICn6Ovu09INVMm0iInMDl8hUgHYSCIITB0IVLJI=
github.com/MTVersionManager/mtvmplugin v0.0.0-20241129190150-d9f58a7a95b9 h1:f3kWnv4GNghtPC2vw00SOo47GpnuSbNSmJ+1GjDklA4=
github.com/MTVersionManager/mtvmplugin v0.0.0-20241129190150-d9f58a7a95b9/go.mod h1:eo3CICn6Ovu09INVMm0iInMDl8hUgHYSCIITB0IVLJI=
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
Expand Down
10 changes: 8 additions & 2 deletions shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package shared

import (
"errors"
"github.com/MTVersionManager/goplugin"
"github.com/MTVersionManager/mtvmplugin"
"os"
"path/filepath"
"strings"

"github.com/MTVersionManager/goplugin"
"github.com/MTVersionManager/mtvmplugin"
"github.com/charmbracelet/lipgloss"

"github.com/MTVersionManager/mtvm/config"
)

var Configuration config.Config

type SuccessMsg string

var CheckMark string = lipgloss.NewStyle().Foreground(lipgloss.Color("2")).SetString("✓").String()

func IsVersionInstalled(tool string, version string) (bool, error) {
_, err := os.Stat(filepath.Join(Configuration.InstallDir, tool, version))
if err != nil {
Expand Down

0 comments on commit 82ddfc0

Please sign in to comment.