mirror of
https://github.com/sbrow/envr.git
synced 2025-12-29 23:47:39 -05:00
Compare commits
4 Commits
v0.1.1
...
3a80c77793
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a80c77793 | |||
| af0a9f9c4c | |||
| 4273fa5895 | |||
| bb3c0cdeee |
121
app/config.go
121
app/config.go
@@ -24,12 +24,15 @@ type SshKeyPair struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type scanConfig struct {
|
type scanConfig struct {
|
||||||
|
// TODO: Support multiple matchers
|
||||||
Matcher string `json:"matcher"`
|
Matcher string `json:"matcher"`
|
||||||
Exclude string `json:"exclude"`
|
// TODO: Support multiple excludes
|
||||||
Include string `json:"include"`
|
Exclude string `json:"exclude"`
|
||||||
|
Include []string `json:"include"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a fresh config with sensible defaults.
|
// Create a fresh config with sensible defaults.
|
||||||
|
// TODO: fd '\.env' -HI ~/ -E '*\.envrc' -E '\.env\.example' -E '\.local/' -E 'vendor' -E 'node_modules'
|
||||||
func NewConfig(privateKeyPaths []string) Config {
|
func NewConfig(privateKeyPaths []string) Config {
|
||||||
var keys = []SshKeyPair{}
|
var keys = []SshKeyPair{}
|
||||||
|
|
||||||
@@ -47,7 +50,7 @@ func NewConfig(privateKeyPaths []string) Config {
|
|||||||
ScanConfig: scanConfig{
|
ScanConfig: scanConfig{
|
||||||
Matcher: "\\.env",
|
Matcher: "\\.env",
|
||||||
Exclude: "*.envrc",
|
Exclude: "*.envrc",
|
||||||
Include: "~",
|
Include: []string{"~"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,71 +112,77 @@ func (c *Config) Save() error {
|
|||||||
|
|
||||||
// Use fd to find all ignored .env files that match the config's parameters
|
// Use fd to find all ignored .env files that match the config's parameters
|
||||||
func (c Config) scan() (paths []string, err error) {
|
func (c Config) scan() (paths []string, err error) {
|
||||||
searchPath, err := c.searchPath()
|
searchPaths, err := c.searchPaths()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find all files (including ignored ones)
|
for _, searchPath := range searchPaths {
|
||||||
fmt.Printf("Searching for all files in \"%s\"...\n", searchPath)
|
// Find all files (including ignored ones)
|
||||||
allCmd := exec.Command("fd", "-a", c.ScanConfig.Matcher, "-E", c.ScanConfig.Exclude, "-HI", searchPath)
|
fmt.Printf("Searching for all files in \"%s\"...\n", searchPath)
|
||||||
allOutput, err := allCmd.Output()
|
allCmd := exec.Command("fd", "-a", c.ScanConfig.Matcher, "-E", c.ScanConfig.Exclude, "-HI", searchPath)
|
||||||
if err != nil {
|
allOutput, err := allCmd.Output()
|
||||||
return []string{}, err
|
if err != nil {
|
||||||
}
|
return paths, err
|
||||||
|
|
||||||
allFiles := strings.Split(strings.TrimSpace(string(allOutput)), "\n")
|
|
||||||
if len(allFiles) == 1 && allFiles[0] == "" {
|
|
||||||
allFiles = []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find unignored files
|
|
||||||
fmt.Printf("Search for unignored fies in \"%s\"...\n", searchPath)
|
|
||||||
unignoredCmd := exec.Command("fd", "-a", c.ScanConfig.Matcher, "-E", c.ScanConfig.Exclude, "-H", searchPath)
|
|
||||||
unignoredOutput, err := unignoredCmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
return []string{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
unignoredFiles := strings.Split(strings.TrimSpace(string(unignoredOutput)), "\n")
|
|
||||||
if len(unignoredFiles) == 1 && unignoredFiles[0] == "" {
|
|
||||||
unignoredFiles = []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a map for faster lookup
|
|
||||||
unignoredMap := make(map[string]bool)
|
|
||||||
for _, file := range unignoredFiles {
|
|
||||||
unignoredMap[file] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter to get only ignored files
|
|
||||||
var ignoredFiles []string
|
|
||||||
for _, file := range allFiles {
|
|
||||||
if !unignoredMap[file] {
|
|
||||||
ignoredFiles = append(ignoredFiles, file)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
allFiles := strings.Split(strings.TrimSpace(string(allOutput)), "\n")
|
||||||
|
if len(allFiles) == 1 && allFiles[0] == "" {
|
||||||
|
allFiles = []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find unignored files
|
||||||
|
fmt.Printf("Search for unignored fies in \"%s\"...\n", searchPath)
|
||||||
|
unignoredCmd := exec.Command("fd", "-a", c.ScanConfig.Matcher, "-E", c.ScanConfig.Exclude, "-H", searchPath)
|
||||||
|
unignoredOutput, err := unignoredCmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
unignoredFiles := strings.Split(strings.TrimSpace(string(unignoredOutput)), "\n")
|
||||||
|
if len(unignoredFiles) == 1 && unignoredFiles[0] == "" {
|
||||||
|
unignoredFiles = []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a map for faster lookup
|
||||||
|
unignoredMap := make(map[string]bool)
|
||||||
|
for _, file := range unignoredFiles {
|
||||||
|
unignoredMap[file] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter to get only ignored files
|
||||||
|
var ignoredFiles []string
|
||||||
|
for _, file := range allFiles {
|
||||||
|
if !unignoredMap[file] {
|
||||||
|
ignoredFiles = append(ignoredFiles, file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
paths = append(paths, ignoredFiles...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ignoredFiles, nil
|
return paths, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) searchPath() (path string, err error) {
|
func (c Config) searchPaths() (paths []string, err error) {
|
||||||
include := c.ScanConfig.Include
|
homeDir, err := os.UserHomeDir()
|
||||||
|
|
||||||
if include == "~" {
|
|
||||||
homeDir, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return homeDir, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
absPath, err := filepath.Abs(include)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return paths, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return absPath, nil
|
includes := c.ScanConfig.Include
|
||||||
|
|
||||||
|
for _, include := range includes {
|
||||||
|
path := strings.Replace(include, "~", homeDir, 1)
|
||||||
|
absPath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
return paths, err
|
||||||
|
}
|
||||||
|
|
||||||
|
paths = append(paths, absPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return paths, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Should this be private?
|
// TODO: Should this be private?
|
||||||
|
|||||||
@@ -61,7 +61,7 @@
|
|||||||
|
|
||||||
packages.default = pkgs.buildGoModule rec {
|
packages.default = pkgs.buildGoModule rec {
|
||||||
pname = "envr";
|
pname = "envr";
|
||||||
version = "0.1.0";
|
version = "0.1.1";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
# If the build complains, uncomment this line
|
# If the build complains, uncomment this line
|
||||||
# vendorHash = "sha256:0000000000000000000000000000000000000000000000000000";
|
# vendorHash = "sha256:0000000000000000000000000000000000000000000000000000";
|
||||||
|
|||||||
Reference in New Issue
Block a user