change to normal folder seek

This commit is contained in:
2026-05-22 19:18:50 +03:00
parent d28a4f02f9
commit 6bd886f4ee
3 changed files with 42 additions and 9 deletions

37
main.go
View File

@@ -15,11 +15,19 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var runners = GetScriptsRunners() var baseDir string
var runners []ScriptRunner
var silent, verbose, listModules, listRunners bool var silent, verbose, listModules, listRunners bool
func init() { func init() {
var err error
baseDir, err = exeDir()
if err != nil {
log.Error("Unable to determine executable folder, falling back to current directory:", err)
baseDir = "."
}
rootCmd.PersistentFlags().BoolVar(&silent, "silent", false, "suppress all output") rootCmd.PersistentFlags().BoolVar(&silent, "silent", false, "suppress all output")
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "enable debug output") rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "enable debug output")
rootCmd.PersistentFlags().BoolVar(&listModules, "list", false, "list configured applications and modules") rootCmd.PersistentFlags().BoolVar(&listModules, "list", false, "list configured applications and modules")
@@ -30,12 +38,15 @@ func init() {
initLogger(silent, verbose && !silent) initLogger(silent, verbose && !silent)
} }
if !(folderExists(("./config/"))) { configPath := filepath.Join(baseDir, "config")
if !folderExists(configPath) {
log.Info("No config folder found, creating new...") log.Info("No config folder found, creating new...")
if err := os.Mkdir("./config/", 0750); err != nil { if err := os.Mkdir(configPath, 0750); err != nil {
log.Fatalln("Failed to create config folder") log.Fatalln("Failed to create config folder")
} }
} }
runners = GetScriptsRunners()
} }
func initLogger(silent bool, verbose bool) { func initLogger(silent bool, verbose bool) {
@@ -43,6 +54,26 @@ func initLogger(silent bool, verbose bool) {
verboseMode = verbose verboseMode = verbose
} }
func exeDir() (string, error) {
exe, err := os.Executable()
if err != nil {
return "", err
}
exe, err = filepath.EvalSymlinks(exe)
if err != nil {
return "", err
}
return filepath.Dir(exe), nil
}
func configFolder() string {
return filepath.Join(baseDir, "config")
}
func runnersFolder() string {
return filepath.Join(baseDir, "runners")
}
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "dtool [application] [action] [module]", Use: "dtool [application] [action] [module]",
Short: "DengiTool", Short: "DengiTool",

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"os" "os"
"path/filepath"
"strings" "strings"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -16,12 +17,13 @@ type ScriptRunner struct {
func GetScriptsRunners() []ScriptRunner { func GetScriptsRunners() []ScriptRunner {
runners := []ScriptRunner{} runners := []ScriptRunner{}
if folderExists("./runners") { runnersDir := runnersFolder()
entries, err := os.ReadDir("./runners/") if folderExists(runnersDir) {
entries, err := os.ReadDir(runnersDir)
if err == nil { if err == nil {
for _, e := range entries { for _, e := range entries {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".yml") { if !e.IsDir() && strings.HasSuffix(e.Name(), ".yml") {
runner, err := os.ReadFile("./runners/" + e.Name()) runner, err := os.ReadFile(filepath.Join(runnersDir, e.Name()))
if err == nil { if err == nil {
var rnr ScriptRunner var rnr ScriptRunner
if err := yaml.Unmarshal(runner, &rnr); err == nil { if err := yaml.Unmarshal(runner, &rnr); err == nil {

View File

@@ -49,7 +49,7 @@ func joinOutput(outputs ...string) string {
} }
func readAppModule(app, module string) AppModule { func readAppModule(app, module string) AppModule {
path := filepath.Join(".", "config", app, module) path := filepath.Join(configFolder(), app, module)
name := module name := module
check := "SCRIPT" check := "SCRIPT"
configPath := filepath.Join(path, "config.yml") configPath := filepath.Join(path, "config.yml")
@@ -73,7 +73,7 @@ func readAppModule(app, module string) AppModule {
func readApplication(folderName string) Application { func readApplication(folderName string) Application {
name := folderName name := folderName
appFolder := filepath.Join(".", "config", folderName) appFolder := filepath.Join(configFolder(), folderName)
configPath := filepath.Join(appFolder, "config.yml") configPath := filepath.Join(appFolder, "config.yml")
if fileExists(configPath) { if fileExists(configPath) {
configF, err := os.ReadFile(configPath) configF, err := os.ReadFile(configPath)
@@ -100,7 +100,7 @@ func readApplication(folderName string) Application {
} }
func listApplications() []Application { func listApplications() []Application {
entries, err := os.ReadDir("./config") entries, err := os.ReadDir(configFolder())
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }