version2 (#1)

Co-authored-by: u80864958 <niklas.breitenstein@bit.admin.ch>
Reviewed-on: #1
This commit is contained in:
2025-04-02 17:29:26 +02:00
parent abe9c7d1fb
commit 158e963c7b
11 changed files with 310 additions and 130 deletions

61
pkg/cat/cater.go Normal file
View File

@ -0,0 +1,61 @@
package cat
import (
"fmt"
"os"
"strings"
)
type Cater map[string]string
func Path(paths ...string) (c Cater, err error) {
c = make(Cater)
var p os.FileInfo
for _, path := range paths {
p, err = os.Stat(path)
if err != nil {
return
}
if p.IsDir() {
err = c.dir(path)
} else {
err = c.file(path)
}
if err != nil {
return
}
}
return
}
func (c Cater) Ignored(ignore ignorer) Cater {
cat := make(Cater)
for name, content := range c {
if ignore.Ignore(name) {
continue
}
cat[name] = content
}
return cat
}
func (c Cater) ToString(delemiter string) string {
var sb strings.Builder
for name, content := range c {
sb.WriteString(fmt.Sprintf(delemiter, name))
sb.WriteString(content)
}
return sb.String()
}
type ignorer interface {
// Ignore() returns true when the given path shall be Ignored.
Ignore(path string) bool
}

58
pkg/cat/internal.go Normal file
View File

@ -0,0 +1,58 @@
package cat
import (
"bufio"
"io"
"os"
"path"
"strings"
)
func (c Cater) dir(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
i, err := file.Info()
if err != nil {
continue
}
path := path.Join(dir, i.Name())
if !file.IsDir() {
c.file(path)
} else {
c.dir(path)
}
}
return nil
}
func (c Cater) file(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
// read file into strings.Builder
var sb strings.Builder
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
return err
}
sb.WriteString(line)
if err == io.EOF {
break
}
}
c[filePath] = sb.String()
return nil
}

44
pkg/clip/coppy.go Normal file
View File

@ -0,0 +1,44 @@
package clip
import (
"os"
"os/exec"
"golang.design/x/clipboard"
)
// Copy copies a given string to the clipboard, handling both Wayland and X11 environments.
// If WAYLAND_DISPLAY is set, it uses wl-copy to copy the string in a Wayland environment.
// Otherwise, it initializes the clipboard and writes the string using the clipboard package for X11.
// Parameters:
//
// str: The string to be copied to the clipboard.
//
// Returns:
//
// error: If an error occurs during copying (e.g., command execution or clipboard initialization fails).
func Copy(str string) error {
if os.Getenv("WAYLAND_DISPLAY") != "" {
cmd := exec.Command("wl-copy")
p, err := cmd.StdinPipe()
if err != nil {
return err
}
if err := cmd.Start(); err == nil {
p.Write([]byte(str))
p.Close()
return cmd.Wait()
}
}
if err := clipboard.Init(); err != nil {
return err
}
clipboard.Write(clipboard.FmtText, []byte(str))
return nil
}

9
pkg/ignore/filesystem.go Normal file
View File

@ -0,0 +1,9 @@
package ignore
import "strings"
type Filesystem struct{}
func (f Filesystem) Ignore(name string) bool {
return strings.Contains(name, "/.") || strings.HasPrefix(name, ".")
}

65
pkg/ignore/gitignore.go Normal file
View File

@ -0,0 +1,65 @@
package ignore
import (
"errors"
"os"
"path"
ignore "github.com/denormal/go-gitignore"
)
const (
gitIgnore = ".gitignore"
)
var (
ErrNotFound = errors.New("Not Found")
)
// getIgnore attempts to find and parse a .gitignore file recursively.
// It searches for the .gitignore file starting from the given path and traversing up the directory tree.
// It returns a pointer to the parsed ignore.GitIgnore object if found, otherwise nil.
func FindGitignore(name string) (ignore.GitIgnore, error) {
for {
stat, err := os.Stat(name)
if err != nil {
return nil, err
}
// If the current path is a directory, iterate through its contents.
if stat.IsDir() {
dir, err := os.ReadDir(name)
if err != nil {
name, _ = path.Split(name)
if name == "" {
return nil, ErrNotFound
}
}
for _, e := range dir {
if !e.IsDir() && e.Name() == gitIgnore {
if ignore, err := ignore.NewFromFile(path.Join(name, e.Name())); err == nil {
return ignore, nil
}
return nil, err
}
}
}
// If the current path is the .gitignore file itself.
if stat.Name() == gitIgnore {
if ignore, err := ignore.NewFromFile(name); err == nil {
return ignore, nil
}
return nil, err
}
name, _ = path.Split(name)
if name == "" {
return nil, ErrNotFound
}
}
}