first commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package bundle
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"cliprepo/internal/config"
|
||||
"github.com/atotto/clipboard"
|
||||
)
|
||||
|
||||
const maxFileSize = 1 * 1024 * 1024 // 1MB
|
||||
|
||||
func Build(key string) (string, error) {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
files, ok := cfg.Bundles[key]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("bundle not found: %s", key)
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
for _, file := range files {
|
||||
info, err := os.Stat(file)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if info.Size() > maxFileSize {
|
||||
continue
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
builder.WriteString(fmt.Sprintf("===== %s =====\n", file))
|
||||
builder.Write(data)
|
||||
builder.WriteString("\n\n")
|
||||
}
|
||||
|
||||
return builder.String(), nil
|
||||
}
|
||||
|
||||
func Copy(content string) error {
|
||||
return clipboard.WriteAll(content)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Bundles map[string][]string `yaml:"bundles"`
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
data, err := os.ReadFile("cliprepo.yaml")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
err = yaml.Unmarshal(data, &cfg)
|
||||
return &cfg, err
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package gitignore
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
ignore "github.com/sabhiram/go-gitignore"
|
||||
)
|
||||
|
||||
func Load(root string) (*ignore.GitIgnore, error) {
|
||||
path := filepath.Join(root, ".gitignore")
|
||||
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return ignore.CompileIgnoreLines(), nil
|
||||
}
|
||||
|
||||
return ignore.CompileIgnoreFile(path)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package tree
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"cliprepo/internal/gitignore"
|
||||
"github.com/atotto/clipboard"
|
||||
)
|
||||
|
||||
func Generate(root string) (string, error) {
|
||||
ig, _ := gitignore.Load(root)
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if path == root {
|
||||
builder.WriteString(".\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
rel, _ := filepath.Rel(root, path)
|
||||
|
||||
if ig.MatchesPath(rel) {
|
||||
if info.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
level := strings.Count(rel, string(os.PathSeparator))
|
||||
prefix := strings.Repeat("│ ", level)
|
||||
|
||||
builder.WriteString(fmt.Sprintf("%s├── %s\n", prefix, info.Name()))
|
||||
return nil
|
||||
})
|
||||
|
||||
return builder.String(), err
|
||||
}
|
||||
|
||||
func Copy(content string) error {
|
||||
return clipboard.WriteAll(content)
|
||||
}
|
||||
|
||||
func Write(path, content string) error {
|
||||
return os.WriteFile(path, []byte(content), 0644)
|
||||
}
|
||||
Reference in New Issue
Block a user