2023-01-06 20:19:00 +01:00
package main
import (
"fmt"
2023-01-06 20:40:56 +01:00
"github.com/jedib0t/go-pretty/v6/table"
2023-07-13 20:26:35 +02:00
"github.com/openziti/zrok/environment"
2023-01-13 21:01:34 +01:00
"github.com/openziti/zrok/tui"
2023-01-06 20:19:00 +01:00
"github.com/spf13/cobra"
"os"
)
func init ( ) {
rootCmd . AddCommand ( newStatusCommand ( ) . cmd )
}
type statusCommand struct {
2023-02-16 19:00:33 +01:00
secrets bool
cmd * cobra . Command
2023-01-06 20:19:00 +01:00
}
func newStatusCommand ( ) * statusCommand {
cmd := & cobra . Command {
Use : "status" ,
Short : "Show the current environment status" ,
Aliases : [ ] string { "st" } ,
Args : cobra . ExactArgs ( 0 ) ,
}
command := & statusCommand { cmd : cmd }
2023-02-16 19:00:33 +01:00
cmd . Flags ( ) . BoolVar ( & command . secrets , "secrets" , false , "Show secrets in status output" )
2023-01-06 20:19:00 +01:00
cmd . Run = command . run
return command
}
func ( cmd * statusCommand ) run ( _ * cobra . Command , _ [ ] string ) {
_ , _ = fmt . Fprintf ( os . Stderr , "\n" )
2023-07-13 20:26:35 +02:00
env , err := environment . LoadRoot ( )
2023-01-06 20:19:00 +01:00
if err != nil {
2023-07-10 22:41:16 +02:00
tui . Error ( "error loading environment" , err )
2023-01-09 20:16:08 +01:00
}
2023-07-14 20:30:35 +02:00
if ! environment . IsLatest ( env ) {
tui . Warning ( fmt . Sprintf ( "Your environment is out of date ('%v'), use '%v' to update (make a backup before updating!)\n" , env . Metadata ( ) . V , tui . Code . Render ( "zrok update" ) ) )
}
2023-01-13 20:36:07 +01:00
_ , _ = fmt . Fprintf ( os . Stdout , tui . Code . Render ( "Config" ) + ":\n\n" )
2023-01-09 20:33:17 +01:00
t := table . NewWriter ( )
t . SetOutputMirror ( os . Stdout )
t . SetStyle ( table . StyleColoredDark )
2023-01-09 21:24:01 +01:00
t . AppendHeader ( table . Row { "Config" , "Value" , "Source" } )
2023-07-13 20:26:35 +02:00
apiEndpoint , from := env . ApiEndpoint ( )
2023-01-09 21:24:01 +01:00
t . AppendRow ( table . Row { "apiEndpoint" , apiEndpoint , from } )
2023-01-09 20:33:17 +01:00
t . Render ( )
_ , _ = fmt . Fprintf ( os . Stderr , "\n" )
2023-07-13 20:26:35 +02:00
if ! env . IsEnabled ( ) {
2023-01-13 20:36:07 +01:00
_ , _ = fmt . Fprintf ( os . Stderr , "To create a local environment use the %v command.\n" , tui . Code . Render ( "zrok enable" ) )
2023-01-06 20:40:56 +01:00
} else {
2023-01-13 20:36:07 +01:00
_ , _ = fmt . Fprintf ( os . Stdout , tui . Code . Render ( "Environment" ) + ":\n\n" )
2023-01-06 20:40:56 +01:00
t := table . NewWriter ( )
t . SetOutputMirror ( os . Stdout )
t . SetStyle ( table . StyleColoredDark )
t . AppendHeader ( table . Row { "Property" , "Value" } )
2023-02-16 19:00:33 +01:00
if cmd . secrets {
2023-07-13 20:26:35 +02:00
t . AppendRow ( table . Row { "Secret Token" , env . Environment ( ) . Token } )
t . AppendRow ( table . Row { "Ziti Identity" , env . Environment ( ) . ZitiIdentity } )
2023-02-16 19:00:33 +01:00
} else {
secretToken := "<<SET>>"
2023-07-13 20:26:35 +02:00
if env . Environment ( ) . Token == "" {
2023-02-16 19:00:33 +01:00
secretToken = "<<UNSET>>"
}
t . AppendRow ( table . Row { "Secret Token" , secretToken } )
zId := "<<SET>>"
2023-07-13 20:26:35 +02:00
if env . Environment ( ) . ZitiIdentity == "" {
2023-02-16 19:00:33 +01:00
zId = "<<UNSET>>"
}
t . AppendRow ( table . Row { "Ziti Identity" , zId } )
}
2023-01-06 20:40:56 +01:00
t . Render ( )
2023-01-06 20:19:00 +01:00
}
2023-01-09 20:33:17 +01:00
_ , _ = fmt . Fprintf ( os . Stdout , "\n" )
2023-01-06 20:19:00 +01:00
}