mirror of
https://github.com/openziti/zrok.git
synced 2025-06-23 19:22:19 +02:00
progress-enabled zrok enable command (#154)
This commit is contained in:
parent
2767280d2c
commit
630cc274af
@ -70,5 +70,5 @@ func (cmd *disableCommand) run(_ *cobra.Command, _ []string) {
|
|||||||
tui.Error("error removing zrok backend identity", err)
|
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...")
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
httptransport "github.com/go-openapi/runtime/client"
|
httptransport "github.com/go-openapi/runtime/client"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/environment"
|
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/environment"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
||||||
@ -9,6 +11,7 @@ import (
|
|||||||
"github.com/openziti-test-kitchen/zrok/zrokdir"
|
"github.com/openziti-test-kitchen/zrok/zrokdir"
|
||||||
"github.com/shirou/gopsutil/v3/host"
|
"github.com/shirou/gopsutil/v3/host"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"os"
|
||||||
user2 "os/user"
|
user2 "os/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -52,7 +55,6 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
|
|||||||
if cmd.description == "<user>@<hostname>" {
|
if cmd.description == "<user>@<hostname>" {
|
||||||
cmd.description = fmt.Sprintf("%v@%v", user.Username, hostName)
|
cmd.description = fmt.Sprintf("%v@%v", user.Username, hostName)
|
||||||
}
|
}
|
||||||
|
|
||||||
zrok, err := zrd.Client()
|
zrok, err := zrd.Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -63,29 +65,49 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
|
|||||||
Description: cmd.description,
|
Description: cmd.description,
|
||||||
Host: hostDetail,
|
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)
|
resp, err := zrok.Environment.Enable(req, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !panicInstead {
|
prg.Send(fmt.Sprintf("the zrok service returned an error: %v", err))
|
||||||
tui.Error("the zrok service returned an error", err)
|
prg.Quit()
|
||||||
}
|
<-done
|
||||||
panic(err)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
prg.Send("writing the environment details...")
|
||||||
apiEndpoint, _ := zrd.ApiEndpoint()
|
apiEndpoint, _ := zrd.ApiEndpoint()
|
||||||
zrd.Env = &zrokdir.Environment{Token: token, ZId: resp.Payload.Identity, ApiEndpoint: apiEndpoint}
|
zrd.Env = &zrokdir.Environment{Token: token, ZId: resp.Payload.Identity, ApiEndpoint: apiEndpoint}
|
||||||
if err := zrd.Save(); err != nil {
|
if err := zrd.Save(); err != nil {
|
||||||
if !panicInstead {
|
prg.Send(fmt.Sprintf("there was an error saving the new environment: %v", err))
|
||||||
tui.Error("there was an error saving the new environment", err)
|
prg.Quit()
|
||||||
}
|
<-done
|
||||||
panic(err)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil {
|
if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil {
|
||||||
if !panicInstead {
|
prg.Send(fmt.Sprintf("there was an error writing the environment: %v", err))
|
||||||
tui.Error("there was an error writing the environment file", err)
|
prg.Quit()
|
||||||
}
|
<-done
|
||||||
panic(err)
|
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) {
|
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)
|
info.Hostname, info.OS, info.Platform, info.PlatformFamily, info.PlatformVersion, info.KernelVersion, info.KernelArch)
|
||||||
return info.Hostname, thisHost, nil
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user