junyu-w / corgi

Corgi is a command-line workflow manager that helps with your repetitive command usages by organizing them into reusable snippet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HOME replacement variable in configuration paths

verygoodsoftwarenotvirus opened this issue · comments

When I define a snippet in snippet.json, I want to be able to have something along the lines of:

{
  "snippets": [
    {
      "file_loc": "~/.corgi/snippets/git_new_branch.json",
      "title": "git_new_branch"
    }
  ],
  "is_meta_dirty": false
}

Because I use Corgi on both my work and personal machines, where work dictates what my home directory has to be on that machine, but obviously not on the others. Currently Corgi fails in this case because it doesn't unwrap the tilde.

Solution Suggestions

  1. I'm fine with something like "$HOME" being implemented, where Corgi looks for that value and calls out to an environment variable, but maybe it would make more sense to just treat all values that start with a dollar sign as environment variables and error out from there?
  2. I'm also fine with tilde being replaced if it's found at the beginning of a particular path.

I'm also okay with actually implementing this if others or the maintainer decide it's valuable. I may well issue a PR in that vein.

hi @verygoodsoftwarenotvirus , can you check what is your snippets_dir path in your .corgi/corgi_conf.json? since corgi uses that one as the prefix for snippet file location. And when corgi initializes itself, it will try to unwrap the value of $HOME or $XDG_CONFIG_HOME (as of 0.2.3, if $XDG_CONFIG_HOME is specified) and use them in the configuration file

Hi @DrakeW I saw some references to $XDG_CONFIG_HOME, and had tried that, with disappointing results:

screenshot 2018-06-29 11 09 08

I'm not ruling out the possibility that I'm totally screwing something up here.

this is interesting, because when corgi initializes itself it will try to read the value of either $HOME or $XDG_CONFIG_HOME, instead of use $HOME (or the other one) literally... can you check if $HOME or XDG_CONFIG_HOME exists on your system's env? and to fix the issue - just manually replace the $HOME in your corgi_conf.json with its value should fix the issue

Also can you elaborate on what OS and version of corgi you are using? thanks

So I did some debugging and found that in Load():

however, when that configuration file is loaded, things like config.SnippetsFile contain $HOME, which isn't then unwrapped by subsequent attempts (by, say, the exec command), which results in stat errors.

If I add the following code:

if _, ok := os.LookupEnv("HOME"); ok {
	if strings.Contains(config.SnippetsFile, "$HOME") {
		strings.Replace(config.SnippetsFile, "$HOME", os.Getenv("HOME"), -1)
	}

	if strings.Contains(config.SnippetsDir, "$HOME") {
		strings.Replace(config.SnippetsDir, "$HOME", os.Getenv("HOME"), -1)
	}
}

between these two lines, all my problems are resolved (for whatever that's worth).

I'm willing to issue a PR that includes this fix, but I want to make sure you're okay with the solution (for instance, should I be using XDG_CONFIG_HOME in the above code instead?).

Thanks again for all your hard work and concern. I promise you this PR comes out of an admiration and excitement about the benefits I receive from Corgi rather than anything negative. :)

EDIT: I'm afraid I've also left out some context here. I want to use Dropbox to manage a single collection of tasks available to all of my computers. :)

@verygoodsoftwarenotvirus Do you want to choose the storage location of snippets without the corgi_conf.json file or choose the storage location of all corgi files, including corgi_conf.json ?

I want to take a moment to recognize I'm aware that I am just one user, and as a consequence, am completely unbothered if this conversation leads to the conclusion that my requirements are unique to myself. :)

I'd like to choose the location of snippets in corgi_conf.json, and I'd like to be able to use environment variables in the relevant fields of corgi_conf.json

That said, I suppose if I could wish up an ideal solution, there'd be some environment variable or other setting that corgi looks for to determine where to find the config, and that corgi knows (and handles the case when) the config may itself contain environment variables to point to the path of the actual snippets. We can keep the defaults for users who are content with how it is, merely providing overrides.

sorry guys I've been busy and not very responsive lately...

@verygoodsoftwarenotvirus your request about being able to choose location of snippets so it could be synced up to different machines using services like Dropbox, Box etc is definitely reasonable!
And I think it's a nice addition to the corgi config command, maybe something like corgi config --snippets <dir of your choice>

I will mark this as a feature request, and include it in the next minor release.

I have a corgi_conf.json.template which uses the same config (environment var $XDG_CONFIG_HOME) as above. I just run an extra step in my .bashrc to dynamically create the actual corgi_conf.json as below

envsubst < corgi_conf.json.template > corgi_conf.json

Support for configuring snippet dir location has been merged to develop, please refer to https://github.com/DrakeW/corgi#choose-the-directory-to-store-all-your-snippets for usage, this should enable sharing snippets through cloud services like Dropbox :) let me know if there's any questions, and thanks for all the feedbacks

v0.2.4 has been released with the requested feature!