Skip to content

Commit

Permalink
Merge pull request #758 from openziti/more_config
Browse files Browse the repository at this point in the history
More Config (Headless Mode) (#224)
  • Loading branch information
michaelquigley authored Sep 30, 2024
2 parents f693a89 + 10121b8 commit 70f5a23
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/zrok/accessPrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions cmd/zrok/configGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions cmd/zrok/configSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"
"net/url"
"os"
"strconv"
)

func init() {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions cmd/zrok/configUnset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion cmd/zrok/sharePrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion cmd/zrok/sharePublic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion cmd/zrok/shareReserved.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions cmd/zrok/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 2 additions & 0 deletions environment/env_core/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,6 +40,7 @@ type Environment struct {
type Config struct {
ApiEndpoint string
DefaultFrontend string
Headless bool
}

type Metadata struct {
Expand Down
21 changes: 21 additions & 0 deletions environment/env_v0_3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
)

func (r *Root) Metadata() *env_core.Metadata {
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions environment/env_v0_4/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
)

func (r *Root) Metadata() *env_core.Metadata {
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion environment/env_v0_4/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 70f5a23

Please sign in to comment.