From 99c48097e2e23755710d08a099918675895192f7 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Fri, 6 Mar 2020 18:26:33 -0500 Subject: [PATCH] feat(installer): platform-correct config templating Modify the "installer" to populate cheatpaths with sensible defaults based on the detection of the user's operating system and environment. --- cmd/cheat/main.go | 27 +++++++++++++++++---------- cmd/cheat/str_config.go | 21 +++++++++++---------- configs/conf.yml | 21 +++++++++++---------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/cmd/cheat/main.go b/cmd/cheat/main.go index c608d73..a696200 100755 --- a/cmd/cheat/main.go +++ b/cmd/cheat/main.go @@ -59,7 +59,6 @@ func main() { // search for the config file in the above paths confpath, err := config.Path(confpaths) if err != nil { - // prompt the user to create a config file yes, err := installer.Prompt( "A config file was not found. Would you like to create one now? [Y/n]", @@ -75,10 +74,21 @@ func main() { os.Exit(0) } - // determine the correct paths for the config file and (optional) community - // cheatsheet download - confpath = confpaths[0] - confdir := path.Dir(confpath) + // read the config template + configs := configs() + + // determine the appropriate paths for config data and (optional) community + // cheatsheets based on the user's platform + confpath = confpaths[0] + confdir := path.Dir(confpath) + + // create paths for community and personal cheatsheets + community := path.Join(confdir, "/cheatsheets/community") + personal := path.Join(confdir, "/cheatsheets/personal") + + // template the above paths into the default configs + configs = strings.Replace(configs, "COMMUNITY_PATH", community, -1) + configs = strings.Replace(configs, "PERSONAL_PATH", personal, -1) // prompt the user to download the community cheatsheets yes, err = installer.Prompt( @@ -92,16 +102,13 @@ func main() { // clone the community cheatsheets if so instructed if yes { - // clone the community cheatsheets - community := path.Join(confdir, "/cheatsheets/community") if err := installer.Clone(community); err != nil { fmt.Fprintf(os.Stderr, "failed to create config: %v\n", err) os.Exit(1) } - // create a directory for personal cheatsheets too - personal := path.Join(confdir, "/cheatsheets/personal") + // also create a directory for personal cheatsheets if err := os.MkdirAll(personal, os.ModePerm); err != nil { fmt.Fprintf( os.Stderr, @@ -113,7 +120,7 @@ func main() { } // the config file does not exist, so we'll try to create one - if err = config.Init(confpath, configs()); err != nil { + if err = config.Init(confpath, configs); err != nil { fmt.Fprintf( os.Stderr, "failed to create config file: %s: %v\n", diff --git a/cmd/cheat/str_config.go b/cmd/cheat/str_config.go index 0cfaf5b..6cfcd2a 100644 --- a/cmd/cheat/str_config.go +++ b/cmd/cheat/str_config.go @@ -41,33 +41,34 @@ cheatpaths: # thus be overridden by more local cheatsheets. That being the case, you # should probably list community cheatsheets first. # - # Note that the paths and tags listed below are just examples. You may freely + # Note that the paths and tags listed below are placeholders. You may freely # change them to suit your needs. # - # TODO: regarding community cheatsheets: these must be installed separately. - # You may download them here: + # Community cheatsheets must be installed separately, though you may have + # downloaded them automatically when installing 'cheat'. If not, you may + # download them here: # # https://github.com/cheat/cheatsheets # # Once downloaded, ensure that 'path' below points to the location at which # you downloaded the community cheatsheets. - name: community - path: ~/.config/cheat/cheatsheets/community + path: COMMUNITY_PATH tags: [ community ] readonly: true # If you have personalized cheatsheets, list them last. They will take # precedence over the more global cheatsheets. - name: personal - path: ~/.config/cheat/cheatsheets/personal + path: PERSONAL_PATH tags: [ personal ] readonly: false - # While it requires no specific configuration here, it's also worth noting - # that 'cheat' will automatically append directories named '.cheat' within - # the current working directory to the 'cheatpath'. This can be very useful - # if you'd like to closely associate cheatsheets with, for example, a - # directory containing source code. + # While it requires no configuration here, it's also worth noting that + # 'cheat' will automatically append directories named '.cheat' within the + # current working directory to the 'cheatpath'. This can be very useful if + # you'd like to closely associate cheatsheets with, for example, a directory + # containing source code. # # Such "directory-scoped" cheatsheets will be treated as the most "local" # cheatsheets, and will override less "local" cheatsheets. Likewise, diff --git a/configs/conf.yml b/configs/conf.yml index c4ee6ff..a622d3e 100644 --- a/configs/conf.yml +++ b/configs/conf.yml @@ -32,33 +32,34 @@ cheatpaths: # thus be overridden by more local cheatsheets. That being the case, you # should probably list community cheatsheets first. # - # Note that the paths and tags listed below are just examples. You may freely + # Note that the paths and tags listed below are placeholders. You may freely # change them to suit your needs. # - # TODO: regarding community cheatsheets: these must be installed separately. - # You may download them here: + # Community cheatsheets must be installed separately, though you may have + # downloaded them automatically when installing 'cheat'. If not, you may + # download them here: # # https://github.com/cheat/cheatsheets # # Once downloaded, ensure that 'path' below points to the location at which # you downloaded the community cheatsheets. - name: community - path: ~/.config/cheat/cheatsheets/community + path: COMMUNITY_PATH tags: [ community ] readonly: true # If you have personalized cheatsheets, list them last. They will take # precedence over the more global cheatsheets. - name: personal - path: ~/.config/cheat/cheatsheets/personal + path: PERSONAL_PATH tags: [ personal ] readonly: false - # While it requires no specific configuration here, it's also worth noting - # that 'cheat' will automatically append directories named '.cheat' within - # the current working directory to the 'cheatpath'. This can be very useful - # if you'd like to closely associate cheatsheets with, for example, a - # directory containing source code. + # While it requires no configuration here, it's also worth noting that + # 'cheat' will automatically append directories named '.cheat' within the + # current working directory to the 'cheatpath'. This can be very useful if + # you'd like to closely associate cheatsheets with, for example, a directory + # containing source code. # # Such "directory-scoped" cheatsheets will be treated as the most "local" # cheatsheets, and will override less "local" cheatsheets. Likewise,