-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add Default config which applies to all servers #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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"` | ||||||||||||||
| LocalEditor []string `yaml:"local-editor,omitempty"` | ||||||||||||||
| ServerEnv map[string]string `yaml:"server-env,omitempty"` | ||||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would prefer if this was just
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"` | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -49,43 +53,88 @@ func LoadConfig(path string) (*NvrhConfig, error) { | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| var envIndex = map[string][]string{ | ||||||||||||||
| "ssh-path": {"NVRH_CLIENT_SSH_PATH"}, | ||||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Lines 67 to 72 in 720d3d7
|
||||||||||||||
| "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. | ||||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||
| 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 | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given where
ApplyPrecedenceis called in the flow,getSshPathisn't applied to it. So in yamlssh-path: binarywould look for a literal executable namedbinaryin your$PATH, and not actually use the default.