summaryrefslogtreecommitdiff
path: root/configureENV.go
blob: 55b60eaf7808303c903f66a24b12048dec3663d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package fhelp

import (
	"os"
	"path/filepath"
)

// THIS IS THE DEFAULT:
//
// ~/.config/    # put things you might care about here
// ~/.cache/     # this is never imporant and can be deleted at any time
//
// Additionally:
//
//    NEVER WRITE OUT ANYTHING TO ~/
//    NEVER EVER
//

func getConfigDir() (string, error) {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return "/tmp", err
	}
	fullpath := filepath.Join(homeDir, ".config/forge")
	err = os.MkdirAll(fullpath, os.ModePerm)
	if err != nil {
		return "/tmp", err
	}

	return fullpath, nil
}

func getCacheDir() (string, error) {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return "/tmp", err
	}
	fullpath := filepath.Join(homeDir, ".cache/forge")
	err = os.MkdirAll(fullpath, os.ModePerm)
	if err != nil {
		return "/tmp", err
	}

	return fullpath, nil
}

func getReposDir() (string, error) {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return "/tmp", err
	}
	fullpath := filepath.Join(homeDir, "go/src")
	err = os.MkdirAll(fullpath, os.ModePerm)
	if err != nil {
		return "/tmp", err
	}

	return fullpath, nil
}

func getCachegDir() (string, error) {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return "/tmp", err
	}
	fullpath := filepath.Join(homeDir, ".cache/forge")
	err = os.MkdirAll(fullpath, os.ModePerm)
	if err != nil {
		return "/tmp", err
	}

	return fullpath, nil
}

// this is the 'old way" and works fine for me. I use it because I like the ~/go/src directory
// because I know exactly what is in it: GO stuff & nothing else
func useGoSrc() (string, error) {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}
	pwd := filepath.Join(homeDir, "go/src")
	err = os.MkdirAll(pwd, os.ModePerm)
	return pwd, err
}

func GoWorkExists(dir string) bool {
	var err error
	workFilePath := filepath.Join(dir, "go.work")
	if _, err = os.Stat(workFilePath); err == nil {
		// log.Info("f.goWorkExists() found", workFilePath)
		return true
	} else if !os.IsNotExist(err) {
		// log.Info("f.goWorkExists() missing", workFilePath)
		return false
	}
	// probably false, but some other error
	// log.Info("f.goWorkExists() os.Stat() error", err, workFilePath)
	return false
}

// findGoWork searches for a "go.work" file starting from the current directory
// and moving up the directory tree. It returns the path to the directory containing
// the file and a boolean indicating whether the file was found.
func FindGoWork() (string, bool) {
	dir, err := os.Getwd()
	if err != nil {
		return "", false
	}

	for {
		workFilePath := filepath.Join(dir, "go.work")
		if _, err := os.Stat(workFilePath); err == nil {
			return dir, true
		}
		parent := filepath.Dir(dir)
		if parent == dir {
			break // Reached root
		}
		dir = parent
	}

	return "", false
}