Git Repository

Stackyard

Stackyard scannt lokale Entwicklungsordner und bündelt Projekte, Git-Status, TODOs, Toolchains, Docker-Compose-Setups, Datei- und Launcher-Funktionen sowie Operations-Informationen in einer lokalen WebUI.

Projektseite ↗
HTTPShttps://zanvex.de/git/stackyard.git

internal/todo/todo.go

Zum Verzeichnis
package todo

import (
	"fmt"
	"os"
	"path/filepath"
)

const PreferredPath = ".stackyard/todo.md"

var FallbackFiles = []string{
	"TODO.md",
	".todo.md",
	"todo.md",
}

func Resolve(projectDir string) (string, bool, error) {
	preferred := filepath.Join(projectDir, PreferredPath)
	if fileExists(preferred) {
		return preferred, true, nil
	}

	for _, candidate := range FallbackFiles {
		fullPath := filepath.Join(projectDir, candidate)
		if fileExists(fullPath) {
			return fullPath, true, nil
		}
	}

	return preferred, false, nil
}

func Read(projectDir string) (string, string, error) {
	path, exists, err := Resolve(projectDir)
	if err != nil {
		return "", "", err
	}
	if !exists {
		return path, "", nil
	}

	data, err := os.ReadFile(path)
	if err != nil {
		return path, "", fmt.Errorf("read todo %q: %w", path, err)
	}
	return path, string(data), nil
}

func Ensure(projectDir string) (string, error) {
	path, exists, err := Resolve(projectDir)
	if err != nil {
		return "", err
	}
	if exists {
		return path, nil
	}

	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
		return "", fmt.Errorf("create todo directory: %w", err)
	}
	if err := os.WriteFile(path, []byte("# TODO\n\n"), 0o644); err != nil {
		return "", fmt.Errorf("create todo file: %w", err)
	}
	return path, nil
}

func Write(projectDir, body string) (string, error) {
	path, err := Ensure(projectDir)
	if err != nil {
		return "", err
	}
	if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
		return "", fmt.Errorf("write todo %q: %w", path, err)
	}
	return path, nil
}

func fileExists(path string) bool {
	info, err := os.Stat(path)
	if err != nil {
		return false
	}
	return !info.IsDir()
}