73 lines
1.3 KiB
Go
73 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
)
|
|
|
|
type Config struct {
|
|
Name string
|
|
File string
|
|
Content string
|
|
}
|
|
|
|
func GetConfigs(directory string) []*Config {
|
|
configs := []*Config{}
|
|
directory = fmt.Sprintf("%s/", strings.TrimRight(directory, "/"))
|
|
|
|
filepath.WalkDir(directory, func(path string, info fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
basename := string(info.Name())
|
|
|
|
if !strings.HasSuffix(basename, ".conf") {
|
|
return nil
|
|
}
|
|
|
|
if strings.Contains(strings.ReplaceAll(path, directory, ""), "/") {
|
|
return nil
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
ExitIfErr(err, "os.ReadFile")
|
|
|
|
configs = append(configs, &Config{
|
|
Name: strings.ReplaceAll(basename, ".conf", ""),
|
|
File: path,
|
|
Content: string(data),
|
|
})
|
|
|
|
return nil
|
|
})
|
|
|
|
return configs
|
|
}
|
|
|
|
func SaveConfig(textarea *gtk.TextView, config *Config) {
|
|
buffer, err := textarea.GetBuffer()
|
|
ExitIfErr(err, "textarea.GetBuffer")
|
|
|
|
start, end := buffer.GetBounds()
|
|
|
|
text, err := buffer.GetText(start, end, true)
|
|
ExitIfErr(err, "buffer.GetText")
|
|
|
|
if config.Content != text {
|
|
config.Content = text
|
|
os.WriteFile(config.File, []byte(text), 600)
|
|
Notify("Success", "Configuration updated")
|
|
} else {
|
|
Notify("Info", "Configuration not updated")
|
|
}
|
|
}
|