--headless for enable (#246)

This commit is contained in:
Michael Quigley 2023-02-16 11:48:03 -05:00
parent 405e1c847c
commit 38190f45e3
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -24,6 +24,7 @@ func init() {
type enableCommand struct { type enableCommand struct {
description string description string
headless bool
cmd *cobra.Command cmd *cobra.Command
} }
@ -34,6 +35,7 @@ func newEnableCommand() *enableCommand {
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
} }
command := &enableCommand{cmd: cmd} command := &enableCommand{cmd: cmd}
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Flags().StringVarP(&command.description, "description", "d", "<user>@<hostname>", "Description of this environment") cmd.Flags().StringVarP(&command.description, "description", "d", "<user>@<hostname>", "Description of this environment")
cmd.Run = command.run cmd.Run = command.run
return command return command
@ -75,8 +77,9 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
} }
var prg *tea.Program var prg *tea.Program
var mdl enableTuiModel
var done = make(chan struct{}) var done = make(chan struct{})
if !cmd.headless {
var mdl enableTuiModel
go func() { go func() {
mdl = newEnableTuiModel() mdl = newEnableTuiModel()
mdl.msg = "contacting the zrok service..." mdl.msg = "contacting the zrok service..."
@ -89,18 +92,24 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
os.Exit(1) os.Exit(1)
} }
}() }()
} else {
logrus.Infof("contacting the zrok service...")
}
resp, err := zrok.Environment.Enable(req, auth) resp, err := zrok.Environment.Enable(req, auth)
//Switch on err type (401, 400, 500, etc...) //Switch on err type (401, 400, 500, etc...)
if err != nil { if err != nil {
time.Sleep(250 * time.Millisecond) time.Sleep(250 * time.Millisecond)
if prg != nil { if !cmd.headless && prg != nil {
prg.Send(fmt.Sprintf("the zrok service returned an error: %v\n", err)) prg.Send(fmt.Sprintf("the zrok service returned an error: %v\n", err))
prg.Quit() prg.Quit()
} else { } else {
logrus.Errorf("the zrok service returned an error: %v", err) logrus.Errorf("the zrok service returned an error: %v", err)
} }
<-done select {
case <-done:
case <-time.After(1 * time.Second):
}
cmd.endpointError(zrd.ApiEndpoint()) cmd.endpointError(zrd.ApiEndpoint())
os.Exit(1) os.Exit(1)
} }
@ -110,27 +119,42 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
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 prg != nil { if !cmd.headless && prg != nil {
prg.Send(fmt.Sprintf("there was an error saving the new environment: %v", err)) prg.Send(fmt.Sprintf("there was an error saving the new environment: %v", err))
prg.Quit() prg.Quit()
} else {
logrus.Errorf("there was an error saving the new environment: %v", err)
}
select {
case <-done:
case <-time.After(1 * time.Second):
} }
<-done
os.Exit(1) os.Exit(1)
} }
if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil { if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil {
if prg != nil { if !cmd.headless && prg != nil {
prg.Send(fmt.Sprintf("there was an error writing the environment: %v", err)) prg.Send(fmt.Sprintf("there was an error writing the environment: %v", err))
prg.Quit() prg.Quit()
} else {
logrus.Errorf("there was an error writing the environment: %v", err)
}
select {
case <-done:
case <-time.After(1 * time.Second):
} }
<-done
os.Exit(1) os.Exit(1)
} }
if prg != nil { if !cmd.headless && prg != nil {
prg.Send(fmt.Sprintf("the zrok environment was successfully enabled...")) prg.Send(fmt.Sprintf("the zrok environment was successfully enabled..."))
prg.Quit() prg.Quit()
} else {
logrus.Infof("the zrok environment was successfully enabled...")
}
select {
case <-done:
case <-time.After(1 * time.Second):
} }
<-done
} }
func (cmd *enableCommand) endpointError(apiEndpoint, _ string) { func (cmd *enableCommand) endpointError(apiEndpoint, _ string) {