-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
343 additions
and
174 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,29 @@ | ||
package tui | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Madh93/tpm/internal/terraform" | ||
"github.com/Madh93/tpm/internal/tpm" | ||
"github.com/charmbracelet/bubbles/spinner" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type installer struct { | ||
spinner spinner.Model | ||
providers []*terraform.Provider | ||
index int | ||
err error | ||
} | ||
type InstallRunner struct{} | ||
|
||
func NewInstallerModel(providers []*terraform.Provider) installer { | ||
return installer{ | ||
spinner: DefaultSpinner, | ||
providers: providers, | ||
index: 0, | ||
func (r *InstallRunner) RunCmd(job ProviderJob) tea.Cmd { | ||
return func() tea.Msg { | ||
job.err = tpm.Install(job.provider, viper.GetBool("force")) | ||
return processedJobMsg(job) | ||
} | ||
} | ||
|
||
func (r InstallRunner) String() string { | ||
return "Installing" | ||
} | ||
|
||
func RunInstaller(providers []*terraform.Provider) (err error) { | ||
if _, err = tea.NewProgram(NewInstallerModel(providers)).Run(); err != nil { | ||
model := NewModel(&InstallRunner{}, providers) | ||
if _, err = tea.NewProgram(model).Run(); err != nil { | ||
return | ||
} | ||
return nil | ||
} | ||
|
||
func (i installer) Init() tea.Cmd { | ||
return tea.Batch( | ||
installProvider(i.providers[i.index]), | ||
i.spinner.Tick, | ||
) | ||
} | ||
|
||
func (i installer) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
|
||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "ctrl+c": | ||
return i, tea.Quit | ||
} | ||
|
||
case spinner.TickMsg: | ||
var cmd tea.Cmd | ||
i.spinner, cmd = i.spinner.Update(msg) | ||
return i, cmd | ||
|
||
case providerMsg: | ||
if i.index >= len(i.providers)-1 { | ||
return i, tea.Sequence( | ||
tea.Printf("%s %s", CheckMark, i.providers[i.index]), | ||
tea.Quit, | ||
) | ||
} | ||
i.index++ | ||
return i, tea.Batch( | ||
tea.Printf("%s %s", CheckMark, i.providers[i.index-1]), | ||
installProvider(i.providers[i.index]), | ||
) | ||
|
||
case errMsg: | ||
i.err = msg | ||
if i.index >= len(i.providers)-1 { | ||
return i, tea.Sequence( | ||
tea.Printf("%s %s Reason: %s", CrossMark, i.providers[i.index], i.err), | ||
tea.Quit, | ||
) | ||
} | ||
i.index++ | ||
return i, tea.Batch( | ||
tea.Printf("%s %s Reason: %s", CrossMark, i.providers[i.index-1], i.err), | ||
installProvider(i.providers[i.index]), | ||
) | ||
} | ||
|
||
return i, nil | ||
} | ||
|
||
func (i installer) View() string { | ||
return fmt.Sprintf("%s Installing %s", i.spinner.View(), i.providers[i.index]) | ||
} | ||
|
||
func installProvider(provider *terraform.Provider) tea.Cmd { | ||
return func() tea.Msg { | ||
err := tpm.Install(provider, viper.GetBool("force")) | ||
if err != nil { | ||
return errMsg{err} | ||
} | ||
return providerMsg(provider.String()) | ||
} | ||
} |
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,30 @@ | ||
package tui | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Madh93/tpm/internal/terraform" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type ProviderJob struct { | ||
id int | ||
provider *terraform.Provider | ||
done bool | ||
err error | ||
} | ||
|
||
type JobRunner interface { | ||
RunCmd(job ProviderJob) tea.Cmd | ||
String() string | ||
} | ||
|
||
func NewRunner(runner string) (JobRunner, error) { | ||
switch runner { | ||
case "install": | ||
return &InstallRunner{}, nil | ||
case "uninstall": | ||
return &UninstallRunner{}, nil | ||
} | ||
return nil, fmt.Errorf("unsupported '%s' job runner", runner) | ||
} |
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
Oops, something went wrong.