From bd11891a898094bff73ac6902333ad3fef1169c8 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 20 May 2022 01:59:50 +0300 Subject: [PATCH] major bugfixes --- internal/ignore_files.go | 25 +++++++++++++++++++++---- internal/utils.go | 21 +-------------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/internal/ignore_files.go b/internal/ignore_files.go index cae3287..fb9d534 100644 --- a/internal/ignore_files.go +++ b/internal/ignore_files.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "os/exec" "path/filepath" "strings" @@ -20,8 +21,7 @@ func PrepareGitignores() ([]string, error) { if GetNeedsUpdate() { log.Println("Updating gitignore files...") - RunCmd("git", "pull", "origin", "master") - os.RemoveAll(filepath.Join(gitignoresDir, ".git")) + RunCmd("git", "-C", gitignoresDir, "pull", "origin", "master") } return GetGitignores(gitignoresDir) @@ -29,14 +29,31 @@ func PrepareGitignores() ([]string, error) { func GetCacheDir() string { homeDir, _ := os.UserHomeDir() - gitignoresDir := filepath.Join(homeDir, ".github.gitignore") - return gitignoresDir + return filepath.Join(homeDir, ".github.gitignore") } func GetGitignores(sourceDir string) ([]string, error) { return filepath.Glob(filepath.Join(sourceDir, "*.gitignore")) } +func GetNeedsUpdate() bool { + gitignoresDir := GetCacheDir() + localBytes, localErr := exec.Command("git", "-C", gitignoresDir, "rev-parse", "@").Output() + baseBytes, baseErr := exec.Command("git", "-C", gitignoresDir, "merge-base", "@", "@{u}").Output() + if localErr != nil { + log.Fatal(localErr) + os.Exit(1) + } + if baseErr != nil { + log.Fatal(baseErr) + os.Exit(1) + } + localStr := string(localBytes) + baseStr := string(baseBytes) + + return localStr == baseStr +} + var ignoreLines = []string{ "/*", ".", diff --git a/internal/utils.go b/internal/utils.go index 0faa8ef..84d9083 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -2,7 +2,6 @@ package internal import ( "fmt" - "io/fs" "log" "os" "os/exec" @@ -26,23 +25,6 @@ func GlobExists(path string) bool { return res != nil } -func GetNeedsUpdate() bool { - localBytes, localErr := exec.Command("git", "rev-parse", "@").Output() - baseBytes, baseErr := exec.Command("git", "merge-base", "@", "@{u}").Output() - if localErr != nil { - log.Fatal(localErr) - os.Exit(1) - } - if baseErr != nil { - log.Fatal(baseErr) - os.Exit(1) - } - localStr := string(localBytes) - baseStr := string(baseBytes) - - return localStr == baseStr -} - func RunCmd(cmd string, args ...string) (string, error) { res, err := exec.Command(cmd, args...).Output() return string(res), err @@ -58,9 +40,8 @@ func WriteFile(path string, data string, overwrite bool) bool { var err error if overwrite { // os.Create(path) - err = os.WriteFile(path, []byte(data), fs.ModeAppend) + err = os.WriteFile(path, []byte(data), 0644) HandleErr(err) - } else { f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) HandleErr(err)