refactor: init command

This commit is contained in:
DataHearth 2022-08-16 23:18:09 +02:00
parent fbc6db312f
commit 1ed6c6259e
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
2 changed files with 11 additions and 19 deletions

View File

@ -235,8 +235,7 @@ func initFunc(ctx *cli.Context) error {
logger.Level = logrus.DebugLevel
}
logger.Infoln("Initializing doggofetcher...")
return pkg.Init()
return pkg.Init(logger)
}
func uninstall(ctx *cli.Context) error {

View File

@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
)
func UpdateRelease(releaseFolder string) error {
@ -67,31 +69,22 @@ func CopyFolder(src, dst string, init bool) error {
return nil
}
func Init() error {
func Init(logger *logrus.Logger) error {
dgfFolder := strings.Replace(DGF_FOLDER, "~", os.Getenv("HOME"), 1)
logger.WithField("folder", dgfFolder).Infoln("Initializing doggofetcher folder")
if err := os.MkdirAll(dgfFolder, 0755); err != nil {
return err
}
f, err := os.OpenFile(getShellConfigFile(), os.O_WRONLY|os.O_APPEND, 0755)
f, err := os.Create(filepath.Join(dgfFolder, "hashes.txt"))
if err != nil {
return err
}
defer f.Close()
f.Close()
msg := fmt.Sprintln("\n\n# doggofetcher section")
msg += fmt.Sprintf("export PATH=%s:$PATH\n", filepath.Join(dgfFolder, "go", "bin"))
if _, err := f.WriteString(msg); err != nil {
return err
}
logger.Infoln("Add doggofetcher to your shell configuration file with the following command:")
fmt.Fprintln(os.Stdout, "\n# doggofetcher section")
fmt.Fprintf(os.Stdout, "export PATH=%s:$PATH\n", filepath.Join(dgfFolder, "go", "bin"))
return nil
}
func getShellConfigFile() string {
if strings.Contains(os.Getenv("SHELL"), "zsh") {
return fmt.Sprintf("%s/.zshrc", os.Getenv("HOME"))
}
return fmt.Sprintf("%s/.bashrc", os.Getenv("HOME"))
}