This commit is contained in:
Chen Asraf
2022-05-25 00:06:55 +03:00
parent 6bdd38e6c6
commit 06660849a7
4 changed files with 39 additions and 34 deletions

35
internal/cache.go Normal file
View File

@@ -0,0 +1,35 @@
package internal
import (
"fmt"
"os"
"path/filepath"
)
func initCache() ([]string, error) {
gitignoresDir := getCacheDir()
if !FileExists(gitignoresDir) {
fmt.Println("Getting gitignore files...")
RunCmd("git", "clone", "--depth=2", repoUrl, gitignoresDir)
fmt.Println()
} else if isCacheNeedsUpdate() {
fmt.Println("Updating gitignore files...")
RunCmd("git", "-C", gitignoresDir, "pull", "origin", "main")
fmt.Println()
}
return getGitignoreFiles(gitignoresDir)
}
func RemoveCacheDir() {
cacheDir := getCacheDir()
fmt.Printf("Removing cache directory: %s...\n", cacheDir)
os.RemoveAll(cacheDir)
fmt.Println("Done")
}
func getCacheDir() string {
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, ".github.gitignore")
}

View File

@@ -11,12 +11,12 @@ func GIGen() {
handleErr(err)
outFile := filepath.Join(wd, ".gitignore")
allFiles, err := prepareGitignores()
allFiles, err := initCache()
handleErr(err)
fileNames, files := autoDiscover(allFiles)
selected, selectedKeys := getLanguageSelections(files, fileNames)
selected, selectedKeys := getLanguages(files, fileNames)
cleanupSelection := AskCleanup()
outContents := Ternary(cleanupSelection, cleanupMultipleFiles(selected, selectedKeys), getAllRaw(selected, selectedKeys))

View File

@@ -10,34 +10,6 @@ import (
"golang.org/x/exp/maps"
)
func prepareGitignores() ([]string, error) {
gitignoresDir := getCacheDir()
if !FileExists(gitignoresDir) {
fmt.Println("Getting gitignore files...")
RunCmd("git", "clone", "--depth=2", repoUrl, gitignoresDir)
fmt.Println()
} else if isCacheNeedsUpdate() {
fmt.Println("Updating gitignore files...")
RunCmd("git", "-C", gitignoresDir, "pull", "origin", "main")
fmt.Println()
}
return getGitignoreFiles(gitignoresDir)
}
func RemoveCacheDir() {
cacheDir := getCacheDir()
fmt.Printf("Removing cache directory: %s...\n", cacheDir)
os.RemoveAll(cacheDir)
fmt.Println("Done")
}
func getCacheDir() string {
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, ".github.gitignore")
}
func getGitignoreFiles(sourceDir string) ([]string, error) {
return filepath.Glob(filepath.Join(sourceDir, "*.gitignore"))
}
@@ -151,7 +123,7 @@ func gatherPreviousCommentGroup(i int, lastTakenIdx int, lines []string, keep []
return keep
}
func getLanguageSelections(files map[string]string, fileNames []string) ([]string, []string) {
func getLanguages(files map[string]string, fileNames []string) ([]string, []string) {
selected := []string{}
allKeys := maps.Keys(files)
selectedKeys := maps.Keys(files)

View File

@@ -7,9 +7,6 @@ import (
"path/filepath"
)
// UNUSED allows unused variables to be included in Go programs
func UNUSED(x ...interface{}) {}
var repoUrl = "https://github.com/github/gitignore"
func FileExists(path string) bool {
@@ -102,6 +99,7 @@ func HandleFileOverwrite(path string, contents string) {
func KeyInterrupt() {
Quit("KeyInterrupt: Quitting")
}
func Quit(message string) {
fmt.Println()
fmt.Println(message)