add new 'headless' config option (#224)

This commit is contained in:
Michael Quigley 2024-09-26 14:34:55 -04:00
parent 73b32031ce
commit b1daabfab8
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
12 changed files with 101 additions and 5 deletions

View File

@ -50,7 +50,11 @@ func newAccessPrivateCommand() *accessPrivateCommand {
Args: cobra.ExactArgs(1),
}
command := &accessPrivateCommand{cmd: cmd}
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
headless := false
if root, err := environment.LoadRoot(); err == nil {
headless, _ = root.Headless()
}
cmd.Flags().BoolVar(&command.headless, "headless", headless, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.subordinate, "subordinate", false, "Enable subordinate mode")
cmd.MarkFlagsMutuallyExclusive("headless", "subordinate")
cmd.Flags().BoolVar(&command.forceLocal, "force-local", false, "Skip agent detection and force local mode")

View File

@ -46,6 +46,12 @@ func (cmd *configGetCommand) run(_ *cobra.Command, args []string) {
} else {
fmt.Println("defaultFrontend = <unset>")
}
case "headless":
if env.Config() != nil {
fmt.Printf("headless = %v\n", env.Config().Headless)
} else {
fmt.Println("headless = <unset>")
}
default:
fmt.Printf("unknown config name '%v'\n", configName)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"
"net/url"
"os"
"strconv"
)
func init() {
@ -77,6 +78,24 @@ func (cmd *configSetCommand) run(_ *cobra.Command, args []string) {
}
fmt.Println("zrok configuration updated")
case "headless":
headless, err := strconv.ParseBool(value)
if err != nil {
tui.Error("unable to parse value for 'headless': %v", err)
}
if env.Config() == nil {
if err := env.SetConfig(&env_core.Config{Headless: headless}); err != nil {
tui.Error("unable to save config", err)
}
} else {
cfg := env.Config()
cfg.Headless = headless
if err := env.SetConfig(cfg); err != nil {
tui.Error("unable to save config", err)
}
}
fmt.Println("zrok configuration updated")
default:
fmt.Printf("unknown config name '%v'\n", configName)
os.Exit(1)

View File

@ -47,6 +47,9 @@ func (cmd *configUnsetCommand) run(_ *cobra.Command, args []string) {
case "defaultFrontend":
cfg.DefaultFrontend = ""
case "headless":
cfg.Headless = false
default:
fmt.Printf("unknown config name '%v'\n", configName)
os.Exit(1)

View File

@ -50,8 +50,12 @@ func newSharePrivateCommand() *sharePrivateCommand {
Args: cobra.RangeArgs(0, 1),
}
command := &sharePrivateCommand{cmd: cmd}
headless := false
if root, err := environment.LoadRoot(); err == nil {
headless, _ = root.Headless()
}
cmd.Flags().StringVarP(&command.backendMode, "backend-mode", "b", "proxy", "The backend mode {proxy, web, tcpTunnel, udpTunnel, caddy, drive, socks, vpn}")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.headless, "headless", headless, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.subordinate, "subordinate", false, "Enable agent mode")
cmd.MarkFlagsMutuallyExclusive("headless", "subordinate")
cmd.Flags().BoolVar(&command.forceLocal, "force-local", false, "Skip agent detection and force local mode")

View File

@ -58,9 +58,13 @@ func newSharePublicCommand() *sharePublicCommand {
defaultFrontend, _ := root.DefaultFrontend()
defaultFrontends = []string{defaultFrontend}
}
headless := false
if root, err := environment.LoadRoot(); err == nil {
headless, _ = root.Headless()
}
cmd.Flags().StringArrayVar(&command.frontendSelection, "frontend", defaultFrontends, "Selected frontends to use for the share")
cmd.Flags().StringVarP(&command.backendMode, "backend-mode", "b", "proxy", "The backend mode {proxy, web, caddy, drive}")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.headless, "headless", headless, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.subordinate, "subordinate", false, "Enable agent mode")
cmd.MarkFlagsMutuallyExclusive("headless", "subordinate")
cmd.Flags().BoolVar(&command.forceLocal, "force-local", false, "Skip agent detection and force local mode")

View File

@ -47,8 +47,12 @@ func newShareReservedCommand() *shareReservedCommand {
Args: cobra.ExactArgs(1),
}
command := &shareReservedCommand{cmd: cmd}
headless := false
if root, err := environment.LoadRoot(); err == nil {
headless, _ = root.Headless()
}
cmd.Flags().StringVar(&command.overrideEndpoint, "override-endpoint", "", "Override the stored target endpoint with a replacement")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.headless, "headless", headless, "Disable TUI and run headless")
cmd.Flags().BoolVar(&command.subordinate, "subordinate", false, "Enable agent mode")
cmd.MarkFlagsMutuallyExclusive("headless", "subordinate")
cmd.Flags().BoolVar(&command.forceLocal, "force-local", false, "Skip agent detection and force local mode")

View File

@ -52,6 +52,8 @@ func (cmd *statusCommand) run(_ *cobra.Command, _ []string) {
t.AppendRow(table.Row{"apiEndpoint", apiEndpoint, apiEndpointFrom})
defaultFrontend, defaultFrontendFrom := env.DefaultFrontend()
t.AppendRow(table.Row{"defaultFrontend", defaultFrontend, defaultFrontendFrom})
headless, headlessFrom := env.Headless()
t.AppendRow(table.Row{"headless", headless, headlessFrom})
t.Render()
_, _ = fmt.Fprintf(os.Stderr, "\n")

View File

@ -14,6 +14,7 @@ type Root interface {
Client() (*rest_client_zrok.Zrok, error)
ApiEndpoint() (string, string)
DefaultFrontend() (string, string)
Headless() (bool, string)
IsEnabled() bool
Environment() *Environment
@ -39,6 +40,7 @@ type Environment struct {
type Config struct {
ApiEndpoint string
DefaultFrontend string
Headless bool
}
type Metadata struct {

View File

@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
)
func (r *Root) Metadata() *env_core.Metadata {
@ -103,6 +104,26 @@ func (r *Root) DefaultFrontend() (string, string) {
return defaultFrontend, from
}
func (r *Root) Headless() (bool, string) {
headless := false
from := "binary"
if r.Config() != nil {
headless = r.Config().Headless
from = "config"
}
env := os.Getenv("ZROK_HEADLESS")
if env != "" {
if v, err := strconv.ParseBool(env); err == nil {
headless = v
from = "ZROK_HEADLESS"
}
}
return headless, from
}
func (r *Root) Environment() *env_core.Environment {
return r.env
}

View File

@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
)
func (r *Root) Metadata() *env_core.Metadata {
@ -103,6 +104,26 @@ func (r *Root) DefaultFrontend() (string, string) {
return defaultFrontend, from
}
func (r *Root) Headless() (bool, string) {
headless := false
from := "binary"
if r.Config() != nil {
headless = r.Config().Headless
from = "config"
}
env := os.Getenv("ZROK_HEADLESS")
if env != "" {
if v, err := strconv.ParseBool(env); err == nil {
headless = v
from = "ZROK_HEADLESS"
}
}
return headless, from
}
func (r *Root) Environment() *env_core.Environment {
return r.env
}

View File

@ -225,12 +225,17 @@ func loadConfig() (*env_core.Config, error) {
out := &env_core.Config{
ApiEndpoint: cfg.ApiEndpoint,
DefaultFrontend: cfg.DefaultFrontend,
Headless: cfg.Headless,
}
return out, nil
}
func saveConfig(cfg *env_core.Config) error {
in := &config{ApiEndpoint: cfg.ApiEndpoint, DefaultFrontend: cfg.DefaultFrontend}
in := &config{
ApiEndpoint: cfg.ApiEndpoint,
DefaultFrontend: cfg.DefaultFrontend,
Headless: cfg.Headless,
}
data, err := json.MarshalIndent(in, "", " ")
if err != nil {
return errors.Wrap(err, "error marshaling config")
@ -326,6 +331,7 @@ type metadata struct {
type config struct {
ApiEndpoint string `json:"api_endpoint"`
DefaultFrontend string `json:"default_frontend"`
Headless bool `json:"headless"`
}
type environment struct {