cheat/cmd/cheat/main.go

158 lines
3.4 KiB
Go
Raw Normal View History

package main
//go:generate go run ../../build/embed.go
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/docopt/docopt-go"
feat(installer): implement "installer" Squashed commit of the following: commit 5c322e79b7b6fa6bef51cac2f9b03fe2eaf08163 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit 803e1f014cc89c30bfb80549b64195235b8b9f62 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit 99c48097e2e23755710d08a099918675895192f7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 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. commit 8e1580ff5a30ae3241c3f02b806666583c5d44b2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 fix(tests): fix `config.Paths` tests Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests. commit a08dca70d90bb6d568d44abf76b2bd911d929001 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commit e15bc6c966bfdca3b507504c95a34b74e264be11 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commit efd09575df4422449d6178cd922a680e0f871c21 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commit ec10244ebeb2a48e5c694aeabb8bcbc177a77369 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commit ebd9ec62873d7f286586ea0af9113d74b8a1d2f2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commit ecac5a09717a4a4714637946d3699da5db97fcc7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
2020-03-07 02:17:26 +01:00
"github.com/mitchellh/go-homedir"
"github.com/cheat/cheat/internal/cheatpath"
"github.com/cheat/cheat/internal/config"
feat(installer): implement "installer" Squashed commit of the following: commit 5c322e79b7b6fa6bef51cac2f9b03fe2eaf08163 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit 803e1f014cc89c30bfb80549b64195235b8b9f62 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit 99c48097e2e23755710d08a099918675895192f7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 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. commit 8e1580ff5a30ae3241c3f02b806666583c5d44b2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 fix(tests): fix `config.Paths` tests Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests. commit a08dca70d90bb6d568d44abf76b2bd911d929001 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commit e15bc6c966bfdca3b507504c95a34b74e264be11 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commit efd09575df4422449d6178cd922a680e0f871c21 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commit ec10244ebeb2a48e5c694aeabb8bcbc177a77369 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commit ebd9ec62873d7f286586ea0af9113d74b8a1d2f2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commit ecac5a09717a4a4714637946d3699da5db97fcc7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
2020-03-07 02:17:26 +01:00
"github.com/cheat/cheat/internal/installer"
)
2022-11-05 17:17:53 +01:00
const version = "4.4.0"
func main() {
// initialize options
opts, err := docopt.ParseArgs(usage(), nil, version)
if err != nil {
// panic here, because this should never happen
panic(fmt.Errorf("docopt failed to parse: %v", err))
}
// if --init was passed, we don't want to attempt to load a config file.
// Instead, just execute cmd_init and exit
if opts["--init"] != nil && opts["--init"] == true {
cmdInit()
os.Exit(0)
}
feat(installer): implement "installer" Squashed commit of the following: commit 5c322e79b7b6fa6bef51cac2f9b03fe2eaf08163 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit 803e1f014cc89c30bfb80549b64195235b8b9f62 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit 99c48097e2e23755710d08a099918675895192f7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 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. commit 8e1580ff5a30ae3241c3f02b806666583c5d44b2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 fix(tests): fix `config.Paths` tests Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests. commit a08dca70d90bb6d568d44abf76b2bd911d929001 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commit e15bc6c966bfdca3b507504c95a34b74e264be11 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commit efd09575df4422449d6178cd922a680e0f871c21 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commit ec10244ebeb2a48e5c694aeabb8bcbc177a77369 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commit ebd9ec62873d7f286586ea0af9113d74b8a1d2f2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commit ecac5a09717a4a4714637946d3699da5db97fcc7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
2020-03-07 02:17:26 +01:00
// get the user's home directory
home, err := homedir.Dir()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get user home directory: %v\n", err)
os.Exit(1)
}
// read the envvars into a map of strings
envvars := map[string]string{}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
2021-09-28 18:33:59 +02:00
if runtime.GOOS == "windows" {
pair[0] = strings.ToUpper(pair[0])
}
envvars[pair[0]] = pair[1]
}
feat(installer): implement "installer" Squashed commit of the following: commit 5c322e79b7b6fa6bef51cac2f9b03fe2eaf08163 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit 803e1f014cc89c30bfb80549b64195235b8b9f62 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit 99c48097e2e23755710d08a099918675895192f7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 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. commit 8e1580ff5a30ae3241c3f02b806666583c5d44b2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 fix(tests): fix `config.Paths` tests Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests. commit a08dca70d90bb6d568d44abf76b2bd911d929001 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commit e15bc6c966bfdca3b507504c95a34b74e264be11 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commit efd09575df4422449d6178cd922a680e0f871c21 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commit ec10244ebeb2a48e5c694aeabb8bcbc177a77369 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commit ebd9ec62873d7f286586ea0af9113d74b8a1d2f2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commit ecac5a09717a4a4714637946d3699da5db97fcc7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
2020-03-07 02:17:26 +01:00
// identify the os-specifc paths at which configs may be located
confpaths, err := config.Paths(runtime.GOOS, home, envvars)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
os.Exit(1)
}
// search for the config file in the above paths
confpath, err := config.Path(confpaths)
if err != nil {
feat(installer): implement "installer" Squashed commit of the following: commit 5c322e79b7b6fa6bef51cac2f9b03fe2eaf08163 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit 803e1f014cc89c30bfb80549b64195235b8b9f62 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit 99c48097e2e23755710d08a099918675895192f7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 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. commit 8e1580ff5a30ae3241c3f02b806666583c5d44b2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 fix(tests): fix `config.Paths` tests Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests. commit a08dca70d90bb6d568d44abf76b2bd911d929001 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commit e15bc6c966bfdca3b507504c95a34b74e264be11 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commit efd09575df4422449d6178cd922a680e0f871c21 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commit ec10244ebeb2a48e5c694aeabb8bcbc177a77369 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commit ebd9ec62873d7f286586ea0af9113d74b8a1d2f2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commit ecac5a09717a4a4714637946d3699da5db97fcc7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
2020-03-07 02:17:26 +01:00
// 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]",
true,
)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create config: %v\n", err)
os.Exit(1)
}
// exit early on a negative answer
if !yes {
os.Exit(0)
}
// choose a confpath
feat(installer): implement "installer" Squashed commit of the following: commit 5c322e79b7b6fa6bef51cac2f9b03fe2eaf08163 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit 803e1f014cc89c30bfb80549b64195235b8b9f62 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit 99c48097e2e23755710d08a099918675895192f7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 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. commit 8e1580ff5a30ae3241c3f02b806666583c5d44b2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 fix(tests): fix `config.Paths` tests Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests. commit a08dca70d90bb6d568d44abf76b2bd911d929001 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commit e15bc6c966bfdca3b507504c95a34b74e264be11 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commit efd09575df4422449d6178cd922a680e0f871c21 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commit ec10244ebeb2a48e5c694aeabb8bcbc177a77369 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commit ebd9ec62873d7f286586ea0af9113d74b8a1d2f2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commit ecac5a09717a4a4714637946d3699da5db97fcc7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
2020-03-07 02:17:26 +01:00
confpath = confpaths[0]
// run the installer
if err := installer.Run(configs(), confpath); err != nil {
fmt.Fprintf(os.Stderr, "failed to run installer: %v\n", err)
os.Exit(1)
}
// notify the user and exit
fmt.Printf("Created config file: %s\n", confpath)
feat(installer): implement "installer" Squashed commit of the following: commit 5c322e79b7b6fa6bef51cac2f9b03fe2eaf08163 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit 803e1f014cc89c30bfb80549b64195235b8b9f62 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit 99c48097e2e23755710d08a099918675895192f7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 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. commit 8e1580ff5a30ae3241c3f02b806666583c5d44b2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 fix(tests): fix `config.Paths` tests Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests. commit a08dca70d90bb6d568d44abf76b2bd911d929001 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commit e15bc6c966bfdca3b507504c95a34b74e264be11 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commit efd09575df4422449d6178cd922a680e0f871c21 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commit ec10244ebeb2a48e5c694aeabb8bcbc177a77369 Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commit ebd9ec62873d7f286586ea0af9113d74b8a1d2f2 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commit ecac5a09717a4a4714637946d3699da5db97fcc7 Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
2020-03-07 02:17:26 +01:00
fmt.Println("Please read this file for advanced configuration information.")
os.Exit(0)
}
// initialize the configs
conf, err := config.New(opts, confpath, true)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
os.Exit(1)
}
// assert that the configs are valid
if err := conf.Validate(); err != nil {
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
os.Exit(1)
}
// filter the cheatpaths if --path was passed
if opts["--path"] != nil {
conf.Cheatpaths, err = cheatpath.Filter(
conf.Cheatpaths,
opts["--path"].(string),
)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid option --path: %v\n", err)
os.Exit(1)
}
}
// determine which command to execute
var cmd func(map[string]interface{}, config.Config)
switch {
case opts["--conf"].(bool):
cmd = cmdConf
case opts["--directories"].(bool):
cmd = cmdDirectories
case opts["--edit"] != nil:
cmd = cmdEdit
case opts["--list"].(bool):
cmd = cmdList
case opts["--tags"].(bool):
cmd = cmdTags
case opts["--search"] != nil:
cmd = cmdSearch
case opts["--rm"] != nil:
cmd = cmdRemove
case opts["<cheatsheet>"] != nil:
cmd = cmdView
case opts["--tag"] != nil && opts["--tag"].(string) != "":
cmd = cmdList
default:
fmt.Println(usage())
os.Exit(0)
}
// execute the command
cmd(opts, conf)
}