-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from MTVersionManager/removePluginCmd
Add remove plugin command
- Loading branch information
Showing
8 changed files
with
193 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package plugincmds | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/MTVersionManager/mtvm/components/fatalHandler" | ||
"github.com/MTVersionManager/mtvm/plugin" | ||
"github.com/MTVersionManager/mtvm/shared" | ||
"github.com/charmbracelet/bubbles/spinner" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
StatusNone = iota | ||
StatusDone | ||
StatusNotFound | ||
) | ||
|
||
type removeModel struct { | ||
pluginName string | ||
spinner spinner.Model | ||
fileStatus int | ||
entryStatus int | ||
errorHandler fatalHandler.Model | ||
} | ||
|
||
func initialRemoveModel(pluginName string) removeModel { | ||
spin := spinner.New() | ||
spin.Spinner = spinner.Dot | ||
return removeModel{ | ||
pluginName: pluginName, | ||
spinner: spin, | ||
} | ||
} | ||
|
||
func (m removeModel) Init() tea.Cmd { | ||
return tea.Batch(m.spinner.Tick, plugin.RemoveEntryCmd(m.pluginName), plugin.RemoveCmd(m.pluginName)) | ||
} | ||
|
||
func (m removeModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
switch msg := msg.(type) { | ||
case error: | ||
m.errorHandler, cmd = m.errorHandler.Update(msg) | ||
return m, cmd | ||
case shared.SuccessMsg: | ||
if string(msg) == "RemoveEntry" { | ||
m.entryStatus = StatusDone | ||
} else if string(msg) == "Remove" { | ||
m.fileStatus = StatusDone | ||
} | ||
case plugin.NotFoundMsg: | ||
if msg.Source == "RemoveEntry" { | ||
m.entryStatus = StatusNotFound | ||
} else if msg.Source == "Remove" { | ||
m.fileStatus = StatusNotFound | ||
} | ||
} | ||
if m.entryStatus != StatusNone && m.fileStatus != StatusNone { | ||
return m, tea.Quit | ||
} | ||
m.spinner, cmd = m.spinner.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m removeModel) View() string { | ||
if m.fileStatus == StatusNone && m.entryStatus == StatusNone { | ||
return fmt.Sprintf("%v Removing %v...\n", m.spinner.View(), m.pluginName) | ||
} | ||
if m.fileStatus == StatusNotFound && m.entryStatus == StatusNotFound { | ||
return fmt.Sprintf("%v No changes were made as %v is not installed\n", shared.CheckMark, m.pluginName) | ||
} | ||
return fmt.Sprintf("%v Successfully removed %v\n", shared.CheckMark, m.pluginName) | ||
} | ||
|
||
var RemoveCmd = &cobra.Command{ | ||
Use: "remove", | ||
Short: "Remove a plugin", | ||
Long: `Remove the plugin with the name specified`, | ||
Args: cobra.ExactArgs(1), | ||
Aliases: []string{"r", "rm"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
p := tea.NewProgram(initialRemoveModel(args[0])) | ||
if model, err := p.Run(); err != nil { | ||
log.Fatal(err) | ||
} else { | ||
if model, ok := model.(removeModel); ok { | ||
fatalHandler.Handle(model.errorHandler) | ||
} | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
components/fatalhandler/fatalhandler.go → components/fatalHandler/fatalHandler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package fatalhandler | ||
package fatalHandler | ||
|
||
import ( | ||
tea "github.com/charmbracelet/bubbletea" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters