progress-enabled zrok enable command (#154)

This commit is contained in:
Michael Quigley 2023-01-11 15:00:08 -05:00
parent 2767280d2c
commit 630cc274af
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 86 additions and 15 deletions

View File

@ -70,5 +70,5 @@ func (cmd *disableCommand) run(_ *cobra.Command, _ []string) {
tui.Error("error removing zrok backend identity", err)
}
}
fmt.Printf("zrok environment '%v' disabled for '%v'\n", zrd.Env.ZId, zrd.Env.Token)
fmt.Println("zrok environment disabled...")
}

View File

@ -2,6 +2,8 @@ package main
import (
"fmt"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/environment"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
@ -9,6 +11,7 @@ import (
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/shirou/gopsutil/v3/host"
"github.com/spf13/cobra"
"os"
user2 "os/user"
)
@ -52,7 +55,6 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
if cmd.description == "<user>@<hostname>" {
cmd.description = fmt.Sprintf("%v@%v", user.Username, hostName)
}
zrok, err := zrd.Client()
if err != nil {
panic(err)
@ -63,29 +65,49 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
Description: cmd.description,
Host: hostDetail,
}
var prg *tea.Program
var mdl enableTuiModel
var done = make(chan struct{})
go func() {
mdl = newEnableTuiModel()
mdl.msg = "contacting the zrok service..."
prg = tea.NewProgram(mdl)
if _, err := prg.Run(); err != nil {
fmt.Println(err)
}
close(done)
if mdl.quitting {
os.Exit(1)
}
}()
resp, err := zrok.Environment.Enable(req, auth)
if err != nil {
if !panicInstead {
tui.Error("the zrok service returned an error", err)
}
panic(err)
prg.Send(fmt.Sprintf("the zrok service returned an error: %v", err))
prg.Quit()
<-done
os.Exit(1)
}
prg.Send("writing the environment details...")
apiEndpoint, _ := zrd.ApiEndpoint()
zrd.Env = &zrokdir.Environment{Token: token, ZId: resp.Payload.Identity, ApiEndpoint: apiEndpoint}
if err := zrd.Save(); err != nil {
if !panicInstead {
tui.Error("there was an error saving the new environment", err)
}
panic(err)
prg.Send(fmt.Sprintf("there was an error saving the new environment: %v", err))
prg.Quit()
<-done
os.Exit(1)
}
if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil {
if !panicInstead {
tui.Error("there was an error writing the environment file", err)
}
panic(err)
prg.Send(fmt.Sprintf("there was an error writing the environment: %v", err))
prg.Quit()
<-done
os.Exit(1)
}
fmt.Printf("zrok environment '%v' enabled for '%v'\n", resp.Payload.Identity, token)
prg.Send(fmt.Sprintf("the zrok environment was successfully enabled..."))
prg.Quit()
<-done
}
func getHost() (string, string, error) {
@ -97,3 +119,52 @@ func getHost() (string, string, error) {
info.Hostname, info.OS, info.Platform, info.PlatformFamily, info.PlatformVersion, info.KernelVersion, info.KernelArch)
return info.Hostname, thisHost, nil
}
type enableTuiModel struct {
spinner spinner.Model
msg string
quitting bool
}
func newEnableTuiModel() enableTuiModel {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = tui.WarningStyle
return enableTuiModel{spinner: s}
}
func (m enableTuiModel) Init() tea.Cmd { return m.spinner.Tick }
func (m enableTuiModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case string:
m.msg = msg
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
default:
return m, nil
}
case struct{}:
return m, tea.Quit
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}
func (m enableTuiModel) View() string {
str := fmt.Sprintf("%s %s\n", m.spinner.View(), m.msg)
if m.quitting {
return str
}
return str
}