update distro profiles

- add release-replace
parent 7be9ad37
......@@ -45,6 +45,66 @@ func processProfile(prof Profile, dryRun bool, res *Result) {
_ = createLinkIfMissing(sys, user, dryRun, res)
}
// ----- release-replace -----
for _, rr := range prof.ReleaseReplace {
src := expandPath(rr.Src, "")
dest := expandPath(rr.Dest, "")
sysVer := getSystemVersion()
if _, err := os.Stat(dest); os.IsNotExist(err) {
res.Replaced = append(res.Replaced,
fmt.Sprintf("Не найден: '%s' — пропущен", dest))
continue
}
data, err := os.ReadFile(dest)
if err != nil {
res.Replaced = append(res.Replaced,
fmt.Sprintf("Не удалось прочитать %s: %v", dest, err))
continue
}
content := string(data)
if strings.Contains(content, "XIMPER_LOCK") {
res.Replaced = append(res.Replaced,
fmt.Sprintf("Заблокирован: %s — пропущен", dest))
continue
}
needReplace := false
fileVer := getFileVersionTag(dest)
if fileVer == "" {
fileVer = "0.9.3"
}
if fileVer == "" || fileVer != sysVer {
needReplace = true
}
if needReplace {
if dryRun {
res.Replaced = append(res.Replaced,
fmt.Sprintf("[Dry-run] Заменён: %s на %s", dest, src))
continue
}
backup := dest + ".old"
_ = os.Rename(dest, backup)
if err := copyFile(src, dest); err != nil {
res.Replaced = append(res.Replaced,
fmt.Sprintf("Ошибка замены %s: %v", dest, err))
continue
}
res.Replaced = append(res.Replaced,
fmt.Sprintf("Обновлён: %s (%s → %s)", dest, fileVer, sysVer))
} else {
res.Replaced = append(res.Replaced,
fmt.Sprintf("Актуально: '%s' — пропущено", dest))
}
}
// ----- hyprvars -----
for _, v := range prof.HyprVars {
_, err := hyprland.HyprlandVarGet(v.Name)
......@@ -224,6 +284,51 @@ func createLinkIfMissing(src, dest string, dryRun bool, res *Result) error {
return nil
}
func getSystemVersion() string {
data, err := os.ReadFile("/etc/os-release")
if err != nil {
return ""
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "VERSION_ID=") {
return strings.Trim(strings.SplitN(line, "=", 2)[1], `"`)
}
}
for _, line := range lines {
if strings.HasPrefix(line, "VERSION=") {
return strings.Trim(strings.SplitN(line, "=", 2)[1], `"`)
}
}
return ""
}
func getFileVersionTag(path string) string {
data, err := os.ReadFile(path)
if err != nil {
return ""
}
content := string(data)
idx := strings.Index(content, "XIMPER_V")
if idx == -1 {
return ""
}
content = content[idx+len("XIMPER_V"):]
var version strings.Builder
for _, c := range content {
if (c >= '0' && c <= '9') || c == '.' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
version.WriteRune(c)
} else {
break
}
}
return version.String()
}
func printTree(title string, lines []string) {
if len(lines) == 0 {
return
......@@ -265,6 +370,7 @@ func CreateConfCommand(ctx context.Context, cmd *cli.Command) error {
printTree("Копируем", res.Copies)
printTree("Создаём ссылки", res.Links)
printTree("Обновляем", res.Replaced)
printTree("Создаём переменные Hyprland", res.HyprVars)
printTree("Синхронизируем раскладку Hyprland", res.SyncSystemLayouts)
......
......@@ -12,15 +12,21 @@ type LinkEntry struct {
User string `json:"user"`
}
type ReleaseReplaceEntry struct {
Src string `json:"src"`
Dest string `json:"dest"`
}
type HyprVar struct {
Name string `json:"name"`
Value string `json:"value"`
}
type Profile struct {
Binary string `json:"binary,omitempty"`
Copy []CopyEntry `json:"copy,omitempty"`
Links []LinkEntry `json:"links"`
Binary string `json:"binary,omitempty"`
Copy []CopyEntry `json:"copy,omitempty"`
Links []LinkEntry `json:"links"`
ReleaseReplace []ReleaseReplaceEntry `json:"release-replace,omitempty"`
// ----- hyprland -----
SyncSystemLayouts bool `json:"sync-system-layouts,omitempty"`
......@@ -34,6 +40,7 @@ type Config struct {
type Result struct {
Links []string
Copies []string
Replaced []string
HyprVars []string
SyncSystemLayouts []string
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment