.fluxignore

This commit is contained in:
Zoe
2024-12-09 03:20:14 -06:00
parent ee8e3a253f
commit 54fbb02ad8
2 changed files with 77 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"archive/tar" "archive/tar"
"bufio"
"bytes" "bytes"
"compress/gzip" "compress/gzip"
_ "embed" _ "embed"
@@ -13,6 +14,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -30,10 +32,71 @@ type Config struct {
DeamonURL string `json:"deamon_url"` DeamonURL string `json:"deamon_url"`
} }
func matchesIgnorePattern(path string, info os.FileInfo, patterns []string) bool {
normalizedPath := filepath.ToSlash(path)
normalizedPath = strings.TrimPrefix(normalizedPath, "./")
for _, pattern := range patterns {
pattern = strings.TrimSpace(pattern)
if pattern == "" || strings.HasPrefix(pattern, "#") {
continue
}
regexPattern := convertGitignorePatternToRegex(pattern)
matched, err := regexp.MatchString(regexPattern, normalizedPath)
if err == nil && matched {
if strings.HasSuffix(pattern, "/") && info.IsDir() {
return true
}
if !info.IsDir() {
dir := filepath.Dir(normalizedPath)
for dir != "." && dir != "/" {
dirPattern := convertGitignorePatternToRegex(pattern)
if matched, _ := regexp.MatchString(dirPattern, filepath.ToSlash(dir)); matched {
return true
}
dir = filepath.Dir(dir)
}
}
return true
}
}
return false
}
func convertGitignorePatternToRegex(pattern string) string {
pattern = strings.TrimSuffix(pattern, "/")
pattern = regexp.QuoteMeta(pattern)
pattern = strings.ReplaceAll(pattern, "\\*\\*", ".*")
pattern = strings.ReplaceAll(pattern, "\\*", "[^/]*")
pattern = strings.ReplaceAll(pattern, "\\?", ".")
pattern = "(^|.*/)" + pattern + "(/.*)?$"
return pattern
}
func compressDirectory(compression pkg.Compression) ([]byte, error) { func compressDirectory(compression pkg.Compression) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
var err error var err error
var ignoredFiles []string
fluxIgnore, err := os.Open(".fluxignore")
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
}
if fluxIgnore != nil {
defer fluxIgnore.Close()
scanner := bufio.NewScanner(fluxIgnore)
for scanner.Scan() {
ignoredFiles = append(ignoredFiles, scanner.Text())
}
}
var gzWriter *gzip.Writer var gzWriter *gzip.Writer
if compression.Enabled { if compression.Enabled {
gzWriter, err = gzip.NewWriterLevel(&buf, compression.Level) gzWriter, err = gzip.NewWriterLevel(&buf, compression.Level)
@@ -54,7 +117,7 @@ func compressDirectory(compression pkg.Compression) ([]byte, error) {
return err return err
} }
if path == "flux.json" || info.IsDir() { if path == "flux.json" || info.IsDir() || matchesIgnorePattern(path, info, ignoredFiles) {
return nil return nil
} }
@@ -100,6 +163,15 @@ func compressDirectory(compression pkg.Compression) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func arrayContains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
func getProjectName(command string, args []string) (string, error) { func getProjectName(command string, args []string) (string, error) {
var projectName string var projectName string

View File

@@ -1,9 +1,9 @@
{ {
"name": "Go Project", "name": "Flux",
"version": "0.0.1", "version": "0.0.1",
"description": "Example description", "description": "Flux is a lightweight self-hosted pseudo-paas for golang web apps.",
"author": "you", "author": "juls0730",
"license": "BSL-1.0", "license": "MIT",
"scripts": { "scripts": {
"build:daemon": "go build -o fluxd cmd/daemon/main.go", "build:daemon": "go build -o fluxd cmd/daemon/main.go",
"build:cli": "go build -o flux cmd/cli/main.go", "build:cli": "go build -o flux cmd/cli/main.go",