77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
// Standard
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
)
|
|
|
|
// getConfigDir figures out the path of the configuration directory.
|
|
// Uses env variables appropriate for the OS, or uses the user provided path.
|
|
func getConfigDir() (string, error) {
|
|
var envName string
|
|
var path []string
|
|
|
|
// User-provided directory takes priority over default directory.
|
|
if flagConfigDir != "" {
|
|
return flagConfigDir, nil
|
|
}
|
|
|
|
switch runtime.GOOS {
|
|
case "linux", "darwin":
|
|
envName = "HOME"
|
|
path = []string{".config", "launcher"}
|
|
case "windows":
|
|
envName = "USERPROFILE"
|
|
path = []string{"launcher"}
|
|
default:
|
|
return "", fmt.Errorf("Unknown OS: %s", runtime.GOOS)
|
|
}
|
|
|
|
home := os.Getenv(envName)
|
|
if home == "" {
|
|
return "", fmt.Errorf("%s is not set", envName)
|
|
}
|
|
path = append([]string{home}, path...)
|
|
configDir := filepath.Join(path...)
|
|
return configDir, nil
|
|
}
|
|
|
|
|
|
// getCommandsetFiles find the home directory and retrieves YAML files
|
|
// not hidden.
|
|
func getCommandsetFiles() ([]string, error) {
|
|
dir, err := getConfigDir()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fileInfos, err := ioutil.ReadDir(dir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// No filenamer starting with '.', and no filenames not ending in
|
|
// ".yaml" or ".yml".
|
|
var filenames []string
|
|
var filename string
|
|
for _, entry := range fileInfos {
|
|
fName := entry.Name()
|
|
// Shortest considered file would by something like 'a.yml',
|
|
// 5 characters.
|
|
if len(fName) < 5 || fName[0] == '.' {
|
|
continue
|
|
}
|
|
|
|
if m, _ := regexp.Match("(?i)\\.ya?ml", []byte(fName)); m {
|
|
filename = filepath.Join(dir, entry.Name())
|
|
filenames = append(filenames, filename)
|
|
}
|
|
}
|
|
return filenames, nil
|
|
}
|