Skip to content
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

Add the current executable dir to the config load path #350

Merged
merged 1 commit into from
Jun 17, 2022

Conversation

GabrielNagy
Copy link
Contributor

@GabrielNagy GabrielNagy commented Jun 15, 2022

In adwatchd we would like to use the executable directory as the default config path.

Update the config API to add the executable path to the config search path, after all the other paths.

@GabrielNagy GabrielNagy requested a review from didrocks June 15, 2022 14:01
@GabrielNagy
Copy link
Contributor Author

I tried to handle this in the adwatchd code by only calling config.Init if the passed config file exists, but then other CLI arguments aren't parsed (like --dirs or -v)

@didrocks
Copy link
Member

Yeah, I’m thinking again about that one. Do we really need it?

Like if you pass:
adwatchd --config foo

For interactive configuration, I would expect foo to exist, then, you prefill the first field entry with it (as it’s the config you want to change). I don’t think we should use that just to prefill the first field entry without the configuration existing beforehand.

Am I missing anything obvious?

@GabrielNagy
Copy link
Contributor Author

@didrocks the issue is that we set a default value for the config flag in adwatchd which causes config.Init to fail hard if the file doesn't exist:

a.rootCmd.PersistentFlags().StringP(
	"config",
	"c",
	defaultConfigPath(), // the executable directory of the app
	i18n.G("`path` to config file"),
)

We can switch to using the InstallConfigFlag helper which will make config.Init search for a config file in the predefined paths (from config.go):

vip.AddConfigPath("./")
vip.AddConfigPath("$HOME/")
vip.AddConfigPath("/etc/")

I managed to get this to work for our case by prepending the executable's directory to viper's searched paths:

vip.SetConfigName(name)

binPath, err := os.Executable()
if err != nil {
	log.Warningf(context.Background(), i18n.G("failed to get executable path, not adding it as a config dir: %v"), err)
} else {
	vip.AddConfigPath(filepath.Dir(binPath))
}

vip.AddConfigPath("./")
vip.AddConfigPath("$HOME/")
vip.AddConfigPath("/etc/")

Does this seem more reasonable?

@GabrielNagy
Copy link
Contributor Author

GabrielNagy commented Jun 16, 2022

It's good to have in mind that, as I mentioned here, the only cases where it's possible for the user to not pass in an explicit config file are:

  • starting the root command for the interactive installer
  • calling adwatchd run from the CLI

If the service is installed, either interactively or through adwatchd service install, the service arguments will always contain the -c argument.

@didrocks
Copy link
Member

@didrocks the issue is that we set a default value for the config flag in adwatchd which causes config.Init to fail hard if the file doesn't exist:

Ah, I do remember now :)

< your other proposal >

Does this seem more reasonable?

It really does to me. This is in line with the path search of local/home/global. Just a note: I would add it after "/etc" to me. ("." is first)

If the service is installed, either interactively or through adwatchd service install, the service arguments will always contain the -c argument.

Yes, until an admin mess with the windows service manager UI :)

@GabrielNagy
Copy link
Contributor Author

Yes, until an admin mess with the windows service manager UI :)

Hehe, for better or worse they actually can't. Due to kardianos/service#82 changes made to the arguments in the Services GUI are not reflected in the actual service.

@didrocks
Copy link
Member

Hehe, for better or worse they actually can't

waow, indeed :) ok I still think your second proposal is better after all.

@GabrielNagy
Copy link
Contributor Author

Sounds good I'll implement that then!

@GabrielNagy GabrielNagy force-pushed the update-config-defaults branch from ad95fb4 to b313fa9 Compare June 16, 2022 15:56
@GabrielNagy GabrielNagy changed the title Update config detection to allow absent files Add the current executable dir to the config load path Jun 16, 2022
@GabrielNagy GabrielNagy force-pushed the update-config-defaults branch 3 times, most recently from 0cb8d67 to 04923cf Compare June 16, 2022 16:56
@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2022

Codecov Report

Merging #350 (04923cf) into main (7383096) will decrease coverage by 0.05%.
The diff coverage is 40.00%.

@@            Coverage Diff             @@
##             main     #350      +/-   ##
==========================================
- Coverage   84.45%   84.40%   -0.06%     
==========================================
  Files          57       57              
  Lines        3925     3930       +5     
==========================================
+ Hits         3315     3317       +2     
- Misses        387      388       +1     
- Partials      223      225       +2     
Impacted Files Coverage Δ
internal/config/config.go 86.53% <40.00%> (-4.96%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7383096...04923cf. Read the comment docs.

@GabrielNagy GabrielNagy force-pushed the update-config-defaults branch from 04923cf to 5faaa4b Compare June 16, 2022 17:06
Copy link
Member

@didrocks didrocks left a comment

Choose a reason for hiding this comment

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

One small change now that the formatting pass (and I’m sure the test will pass ;)

internal/config/config.go Outdated Show resolved Hide resolved
internal/config/config.go Outdated Show resolved Hide resolved
@@ -108,6 +109,12 @@ func TestInit(t *testing.T) {
configFileContent: "value: filecontentvalue",
want: "filecontentvalue", wantCallbackCalled: 1,
},
"Load configuration from executable dir": {
Copy link
Member

Choose a reason for hiding this comment

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

With a test without asking! You got the spirit :) \o/

In adwatchd we would like to use the executable directory as the default
config path.

Update the config API to add the executable path to the config search
path, after all the other paths.
@GabrielNagy GabrielNagy force-pushed the update-config-defaults branch from 5faaa4b to 359ac46 Compare June 16, 2022 17:16
Copy link
Member

@didrocks didrocks left a comment

Choose a reason for hiding this comment

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

I will surely enjoy the "new content" :)

@didrocks didrocks merged commit c4310db into main Jun 17, 2022
@didrocks didrocks deleted the update-config-defaults branch June 17, 2022 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants