Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ var CliClientOpenCommand = cli.Command{
}

serverConfig := cfg.Servers[endpoint.GivenHost]
if err := nvrh_config.ApplyPrecedence(cmd, serverConfig); err != nil {
if err := nvrh_config.ApplyPrecedence(cmd, cfg.Default, serverConfig); err != nil {
return err
}

Expand Down Expand Up @@ -441,7 +441,7 @@ var CliClientReconnectCommand = cli.Command{
}

serverConfig := cfg.Servers[endpoint.GivenHost]
if err := nvrh_config.ApplyPrecedence(cmd, serverConfig); err != nil {
if err := nvrh_config.ApplyPrecedence(cmd, cfg.Default, serverConfig); err != nil {
return err
}

Expand Down
87 changes: 68 additions & 19 deletions src/nvrh_config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
)

type NvrhConfigServer struct {
NvimCmd []string `yaml:"nvim-cmd,omitempty"`
UsePorts *bool `yaml:"use-ports,omitempty"`
SshArg []string `yaml:"ssh-arg,omitempty"`
NvimCmd []string `yaml:"nvim-cmd,omitempty"`
UsePorts *bool `yaml:"use-ports,omitempty"`
SshArg []string `yaml:"ssh-arg,omitempty"`
SshPath string `yaml:"ssh-path,omitempty"`

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given where ApplyPrecedence is called in the flow, getSshPath isn't applied to it. So in yaml ssh-path: binary would look for a literal executable named binary in your $PATH, and not actually use the default.

LocalEditor []string `yaml:"local-editor,omitempty"`
ServerEnv map[string]string `yaml:"server-env,omitempty"`

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer if this was just []string:

  • via cli arg, they're FOO=bar
  • via env vars, they're FOO=bar
  • via yaml, they're FOO: bar

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, more changes are needed to pipe these through is the issue.

}

type NvrhConfig struct {
Default NvrhConfigServer `yaml:"default,omitempty"`
Servers map[string]NvrhConfigServer `yaml:"servers"`
}

Expand Down Expand Up @@ -49,43 +53,88 @@ func LoadConfig(path string) (*NvrhConfig, error) {
}

var envIndex = map[string][]string{
"ssh-path": {"NVRH_CLIENT_SSH_PATH"},

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When adding to this, we need to remove the source from the CLI command, similar to

nvrh/src/client/main.go

Lines 67 to 72 in 720d3d7

&cli.BoolFlag{
Name: "use-ports",
Usage: "Use ports instead of sockets. Defaults to true on Windows [$NVRH_CLIENT_USE_PORTS]",
// Sources: cli.EnvVars("NVRH_CLIENT_USE_PORTS"),
Value: runtime.GOOS == "windows",
},

"local-editor": {"NVRH_CLIENT_LOCAL_EDITOR"},
"server-env": {"NVRH_CLIENT_SERVER_ENV"},
"nvim-cmd": {"NVRH_CLIENT_NVIM_CMD"},
"use-ports": {"NVRH_CLIENT_USE_PORTS"},
"ssh-arg": {"NVRH_CLIENT_SSH_ARG"},
}

func ApplyPrecedence(c *cli.Command, sc NvrhConfigServer) error {
func ApplyPrecedence(c *cli.Command,
defaultServerConfig NvrhConfigServer,
serverConfig NvrhConfigServer) error {
// Use values from YAML if not set in command.
if !c.IsSet("nvim-cmd") && len(sc.NvimCmd) > 0 {
for _, v := range sc.NvimCmd {
if err := c.Set("nvim-cmd", v); err != nil {

// First apply the specific server config, then the default config.
err := applyServerConfig(c, serverConfig)
if err != nil {
return err
}

err = applyServerConfig(c, defaultServerConfig)
if err != nil {
return err
}

// Fall back to environment variables if still not set.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I can see that was was just moved to DRY the yaml -> CLI stuff, so I might have had a bug here. I have $NVRH_CLIENT_SERVER_ENV set in my environment and that's taking precedence over anything in the YAML.

for name, keys := range envIndex {
if c.IsSet(name) {
continue
}

if raw, ok := lookupFirst(keys); ok {
if err := c.Set(name, raw); err != nil {
return err
}
}
}

if !c.IsSet("use-ports") && sc.UsePorts != nil {
if err := c.Set("use-ports", fmt.Sprintf("%v", *sc.UsePorts)); err != nil {
return err
return nil
}

func applyServerConfig(c *cli.Command, serverConfig NvrhConfigServer) error {
if !c.IsSet("ssh-path") {
if serverConfig.SshPath != "" {
if err := c.Set("ssh-path", serverConfig.SshPath); err != nil {
return err
}
}
}

if !c.IsSet("ssh-arg") && len(sc.SshArg) > 0 {
for _, v := range sc.SshArg {
if err := c.Set("ssh-arg", v); err != nil {
if !c.IsSet("local-editor") && len(serverConfig.LocalEditor) > 0 {
for _, v := range serverConfig.LocalEditor {
if err := c.Set("local-editor", v); err != nil {
return err
}
}
}

// Fall back to environment variables if still not set.
for name, keys := range envIndex {
if c.IsSet(name) {
continue
if !c.IsSet("server-env") && len(serverConfig.ServerEnv) > 0 {
for k, v := range serverConfig.ServerEnv {
if err := c.Set("server-env", fmt.Sprintf("%s=%s", k, v)); err != nil {
return err
}
}
}

if raw, ok := lookupFirst(keys); ok {
if err := c.Set(name, raw); err != nil {
if !c.IsSet("nvim-cmd") && len(serverConfig.NvimCmd) > 0 {
for _, v := range serverConfig.NvimCmd {
if err := c.Set("nvim-cmd", v); err != nil {
return err
}
}
}

if !c.IsSet("use-ports") && serverConfig.UsePorts != nil {
if err := c.Set("use-ports", fmt.Sprintf("%v", *serverConfig.UsePorts)); err != nil {
return err
}
}

if !c.IsSet("ssh-arg") && len(serverConfig.SshArg) > 0 {
for _, v := range serverConfig.SshArg {
if err := c.Set("ssh-arg", v); err != nil {
return err
}
}
Expand Down