2020-11-28 16:32:37 +01:00
|
|
|
package installer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-07-04 22:55:57 +02:00
|
|
|
"path/filepath"
|
2020-11-28 16:32:37 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cheat/cheat/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run runs the installer
|
|
|
|
func Run(configs string, confpath string) error {
|
|
|
|
|
|
|
|
// determine the appropriate paths for config data and (optional) community
|
|
|
|
// cheatsheets based on the user's platform
|
2022-07-04 22:55:57 +02:00
|
|
|
confdir := filepath.Dir(confpath)
|
2020-11-28 16:32:37 +01:00
|
|
|
|
|
|
|
// create paths for community and personal cheatsheets
|
2022-07-04 22:55:57 +02:00
|
|
|
community := filepath.Join(confdir, "cheatsheets", "community")
|
|
|
|
personal := filepath.Join(confdir, "cheatsheets", "personal")
|
2020-11-28 16:32:37 +01:00
|
|
|
|
|
|
|
// template the above paths into the default configs
|
|
|
|
configs = strings.Replace(configs, "COMMUNITY_PATH", community, -1)
|
|
|
|
configs = strings.Replace(configs, "PERSONAL_PATH", personal, -1)
|
2022-08-07 16:16:52 +02:00
|
|
|
configs = strings.Replace(configs, "PAGER_PATH", config.Pager(), -1)
|
2020-11-28 16:32:37 +01:00
|
|
|
|
|
|
|
// prompt the user to download the community cheatsheets
|
|
|
|
yes, err := Prompt(
|
|
|
|
"Would you like to download the community cheatsheets? [Y/n]",
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to prompt: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clone the community cheatsheets if so instructed
|
|
|
|
if yes {
|
|
|
|
// clone the community cheatsheets
|
2022-07-04 22:55:57 +02:00
|
|
|
fmt.Printf("Cloning community cheatsheets to %s.\n", community)
|
2020-11-28 16:32:37 +01:00
|
|
|
if err := clone(community); err != nil {
|
|
|
|
return fmt.Errorf("failed to clone cheatsheets: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// also create a directory for personal cheatsheets
|
2022-07-04 22:55:57 +02:00
|
|
|
fmt.Printf("Cloning personal cheatsheets to %s.\n", personal)
|
2020-11-28 16:32:37 +01:00
|
|
|
if err := os.MkdirAll(personal, os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("failed to create directory: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the config file does not exist, so we'll try to create one
|
|
|
|
if err = config.Init(confpath, configs); err != nil {
|
|
|
|
return fmt.Errorf("failed to create config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|