mirror of
https://github.com/chenasraf/gi_gen.git
synced 2026-05-17 17:48:01 +00:00
cleanups
This commit is contained in:
35
internal/cache.go
Normal file
35
internal/cache.go
Normal 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")
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user