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"
)
var runners = GetScriptsRunners()
var baseDir string
var runners []ScriptRunner
var silent, verbose, listModules, listRunners bool
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(&verbose, "verbose", false, "enable debug output")
rootCmd.PersistentFlags().BoolVar(&listModules, "list", false, "list configured applications and modules")
@@ -30,12 +38,15 @@ func init() {
initLogger(silent, verbose && !silent)
}
if !(folderExists(("./config/"))) {
configPath := filepath.Join(baseDir, "config")
if !folderExists(configPath) {
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")
}
}
runners = GetScriptsRunners()
}
func initLogger(silent bool, verbose bool) {
@@ -43,6 +54,26 @@ func initLogger(silent bool, verbose bool) {
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{
Use: "dtool [application] [action] [module]",
Short: "DengiTool",

View File

@@ -2,6 +2,7 @@ package main
import (
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
@@ -16,12 +17,13 @@ type ScriptRunner struct {
func GetScriptsRunners() []ScriptRunner {
runners := []ScriptRunner{}
if folderExists("./runners") {
entries, err := os.ReadDir("./runners/")
runnersDir := runnersFolder()
if folderExists(runnersDir) {
entries, err := os.ReadDir(runnersDir)
if err == nil {
for _, e := range entries {
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 {
var rnr ScriptRunner
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 {
path := filepath.Join(".", "config", app, module)
path := filepath.Join(configFolder(), app, module)
name := module
check := "SCRIPT"
configPath := filepath.Join(path, "config.yml")
@@ -73,7 +73,7 @@ func readAppModule(app, module string) AppModule {
func readApplication(folderName string) Application {
name := folderName
appFolder := filepath.Join(".", "config", folderName)
appFolder := filepath.Join(configFolder(), folderName)
configPath := filepath.Join(appFolder, "config.yml")
if fileExists(configPath) {
configF, err := os.ReadFile(configPath)
@@ -100,7 +100,7 @@ func readApplication(folderName string) Application {
}
func listApplications() []Application {
entries, err := os.ReadDir("./config")
entries, err := os.ReadDir(configFolder())
if err != nil {
log.Fatalln(err)
}