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.
This commit is contained in:
Chris Lane 2020-03-06 18:26:33 -05:00
parent 8e1580ff5a
commit 99c48097e2
3 changed files with 39 additions and 30 deletions

View File

@ -59,7 +59,6 @@ func main() {
// search for the config file in the above paths // search for the config file in the above paths
confpath, err := config.Path(confpaths) confpath, err := config.Path(confpaths)
if err != nil { if err != nil {
// prompt the user to create a config file // prompt the user to create a config file
yes, err := installer.Prompt( yes, err := installer.Prompt(
"A config file was not found. Would you like to create one now? [Y/n]", "A config file was not found. Would you like to create one now? [Y/n]",
@ -75,11 +74,22 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// determine the correct paths for the config file and (optional) community // read the config template
// cheatsheet download configs := configs()
// determine the appropriate paths for config data and (optional) community
// cheatsheets based on the user's platform
confpath = confpaths[0] confpath = confpaths[0]
confdir := path.Dir(confpath) 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 // prompt the user to download the community cheatsheets
yes, err = installer.Prompt( yes, err = installer.Prompt(
"Would you like to download the community cheatsheets? [Y/n]", "Would you like to download the community cheatsheets? [Y/n]",
@ -92,16 +102,13 @@ func main() {
// clone the community cheatsheets if so instructed // clone the community cheatsheets if so instructed
if yes { if yes {
// clone the community cheatsheets // clone the community cheatsheets
community := path.Join(confdir, "/cheatsheets/community")
if err := installer.Clone(community); err != nil { if err := installer.Clone(community); err != nil {
fmt.Fprintf(os.Stderr, "failed to create config: %v\n", err) fmt.Fprintf(os.Stderr, "failed to create config: %v\n", err)
os.Exit(1) os.Exit(1)
} }
// create a directory for personal cheatsheets too // also create a directory for personal cheatsheets
personal := path.Join(confdir, "/cheatsheets/personal")
if err := os.MkdirAll(personal, os.ModePerm); err != nil { if err := os.MkdirAll(personal, os.ModePerm); err != nil {
fmt.Fprintf( fmt.Fprintf(
os.Stderr, os.Stderr,
@ -113,7 +120,7 @@ func main() {
} }
// the config file does not exist, so we'll try to create one // 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( fmt.Fprintf(
os.Stderr, os.Stderr,
"failed to create config file: %s: %v\n", "failed to create config file: %s: %v\n",

View File

@ -41,33 +41,34 @@ cheatpaths:
# thus be overridden by more local cheatsheets. That being the case, you # thus be overridden by more local cheatsheets. That being the case, you
# should probably list community cheatsheets first. # 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. # change them to suit your needs.
# #
# TODO: regarding community cheatsheets: these must be installed separately. # Community cheatsheets must be installed separately, though you may have
# You may download them here: # downloaded them automatically when installing 'cheat'. If not, you may
# download them here:
# #
# https://github.com/cheat/cheatsheets # https://github.com/cheat/cheatsheets
# #
# Once downloaded, ensure that 'path' below points to the location at which # Once downloaded, ensure that 'path' below points to the location at which
# you downloaded the community cheatsheets. # you downloaded the community cheatsheets.
- name: community - name: community
path: ~/.config/cheat/cheatsheets/community path: COMMUNITY_PATH
tags: [ community ] tags: [ community ]
readonly: true readonly: true
# If you have personalized cheatsheets, list them last. They will take # If you have personalized cheatsheets, list them last. They will take
# precedence over the more global cheatsheets. # precedence over the more global cheatsheets.
- name: personal - name: personal
path: ~/.config/cheat/cheatsheets/personal path: PERSONAL_PATH
tags: [ personal ] tags: [ personal ]
readonly: false readonly: false
# While it requires no specific configuration here, it's also worth noting # While it requires no configuration here, it's also worth noting that
# that 'cheat' will automatically append directories named '.cheat' within # 'cheat' will automatically append directories named '.cheat' within the
# the current working directory to the 'cheatpath'. This can be very useful # current working directory to the 'cheatpath'. This can be very useful if
# if you'd like to closely associate cheatsheets with, for example, a # you'd like to closely associate cheatsheets with, for example, a directory
# directory containing source code. # containing source code.
# #
# Such "directory-scoped" cheatsheets will be treated as the most "local" # Such "directory-scoped" cheatsheets will be treated as the most "local"
# cheatsheets, and will override less "local" cheatsheets. Likewise, # cheatsheets, and will override less "local" cheatsheets. Likewise,

View File

@ -32,33 +32,34 @@ cheatpaths:
# thus be overridden by more local cheatsheets. That being the case, you # thus be overridden by more local cheatsheets. That being the case, you
# should probably list community cheatsheets first. # 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. # change them to suit your needs.
# #
# TODO: regarding community cheatsheets: these must be installed separately. # Community cheatsheets must be installed separately, though you may have
# You may download them here: # downloaded them automatically when installing 'cheat'. If not, you may
# download them here:
# #
# https://github.com/cheat/cheatsheets # https://github.com/cheat/cheatsheets
# #
# Once downloaded, ensure that 'path' below points to the location at which # Once downloaded, ensure that 'path' below points to the location at which
# you downloaded the community cheatsheets. # you downloaded the community cheatsheets.
- name: community - name: community
path: ~/.config/cheat/cheatsheets/community path: COMMUNITY_PATH
tags: [ community ] tags: [ community ]
readonly: true readonly: true
# If you have personalized cheatsheets, list them last. They will take # If you have personalized cheatsheets, list them last. They will take
# precedence over the more global cheatsheets. # precedence over the more global cheatsheets.
- name: personal - name: personal
path: ~/.config/cheat/cheatsheets/personal path: PERSONAL_PATH
tags: [ personal ] tags: [ personal ]
readonly: false readonly: false
# While it requires no specific configuration here, it's also worth noting # While it requires no configuration here, it's also worth noting that
# that 'cheat' will automatically append directories named '.cheat' within # 'cheat' will automatically append directories named '.cheat' within the
# the current working directory to the 'cheatpath'. This can be very useful # current working directory to the 'cheatpath'. This can be very useful if
# if you'd like to closely associate cheatsheets with, for example, a # you'd like to closely associate cheatsheets with, for example, a directory
# directory containing source code. # containing source code.
# #
# Such "directory-scoped" cheatsheets will be treated as the most "local" # Such "directory-scoped" cheatsheets will be treated as the most "local"
# cheatsheets, and will override less "local" cheatsheets. Likewise, # cheatsheets, and will override less "local" cheatsheets. Likewise,