Skip to content

Commit

Permalink
Merge pull request #45 from A2-ai/feature/better-glob
Browse files Browse the repository at this point in the history
Better glob handling
  • Loading branch information
andriygm authored Aug 17, 2023
2 parents 2cc63e1 + 7c06905 commit 68db35c
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 146 deletions.
118 changes: 57 additions & 61 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"dvs/internal/config"
"dvs/internal/git"
"dvs/internal/log"
"dvs/internal/meta"
"dvs/internal/storage"
"fmt"
"os"
Expand Down Expand Up @@ -48,84 +49,69 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
}

// Queue file paths
var filesToAdd []string
for _, path := range args {
globMatches, err := filepath.Glob(path)
var queuedPaths []string
for _, glob := range args {
// Remove meta file extension
glob = strings.ReplaceAll(glob, meta.FileExtension, "")

// Skip if already queued
if slices.Contains(queuedPaths, glob) {
continue
}

// Ensure file is inside of the git repo
absPath, err := filepath.Abs(glob)
if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(path), "\n")
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(glob), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: fmt.Sprintf("Skipping invalid path %s", path),
Location: path,
Message: "skipped invalid path",
Location: glob,
})
continue
}
if strings.TrimPrefix(absPath, gitDir) == absPath {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping file outside of git repository", log.ColorFile(glob), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped file outside of git repository",
Location: glob,
})
continue
}

if !slices.Contains(globMatches, path) {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(path), "\n")
// Check if file exists
fileStat, err := os.Stat(glob)
if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(glob), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: fmt.Sprintf("Skipping invalid path %s", path),
Location: path,
Message: "skipped invalid path",
Location: glob,
})

continue
}

for _, match := range globMatches {
// Ensure file is inside of the git repo
absPath, err := filepath.Abs(match)
if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped invalid path",
Location: match,
})
continue
}
if strings.TrimPrefix(absPath, gitDir) == absPath {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping file outside of git repository", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped file outside of git repository",
Location: match,
})
continue
}

// Check if file exists
fileStat, err := os.Stat(match)
if err != nil {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping invalid path", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped invalid path",
Location: match,
})

continue
}

// Skip directories
if fileStat.IsDir() {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping directory", log.ColorFile(match), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped directory",
Location: match,
})

continue
}

// Add the file to the queue
filesToAdd = append(filesToAdd, match)
// Skip directories
if fileStat.IsDir() {
log.Print(log.ColorBold(log.ColorYellow("!")), "Skipping directory", log.ColorFile(glob), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "skipped directory",
Location: glob,
})

continue
}

// Add the file to the queue
queuedPaths = append(queuedPaths, glob)
}

// Add each file to storage
for i, file := range filesToAdd {
log.Print(fmt.Sprint(i+1)+"/"+fmt.Sprint(len(filesToAdd)), " ", log.ColorFile(file))
for i, file := range queuedPaths {
log.Print(fmt.Sprint(i+1)+"/"+fmt.Sprint(len(queuedPaths)), " ", log.ColorFile(file))

_, err := storage.Add(file, conf.StorageDir, gitDir, message, dry)
if err != nil {
Expand All @@ -139,6 +125,16 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
}
}

// Warn if no files were queued
if len(queuedPaths) == 0 {
log.Print(log.ColorBold(log.ColorYellow("!")), "No files were queued")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "no files were queued",
Location: ".",
})
}

return nil
}

Expand Down
64 changes: 17 additions & 47 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"dvs/internal/meta"
"dvs/internal/storage"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
)
Expand All @@ -30,72 +28,45 @@ func runGetCmd(cmd *cobra.Command, args []string) error {
}

// Get flags
recurse, err := cmd.Flags().GetBool("recurse")
if err != nil {
return err
}

dry, err := cmd.Flags().GetBool("dry")
if err != nil {
return err
}

var filesToGet []string

// Parse each path
for _, path := range args {
// If the path is a directory, get all files in the directory
if pathInfo, err := os.Stat(path); err == nil && pathInfo.IsDir() {
var metaFiles []string
if recurse {
// Get all files in the current directory and subdirectories
metaFiles, err = meta.GetAllMetaFiles(path)
if err != nil {
return err
}
} else {
// Get all files in the current directory
metaFiles, err = meta.GetMetaFiles(path)
if err != nil {
return err
}
if len(metaFiles) == 0 {
absPath, _ := filepath.Abs(path)
log.Print(log.ColorBold(log.ColorYellow("!")), "No devious files found in directory, skipping", log.ColorFile(absPath), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "no devious files found in directory",
Location: absPath,
})
}
}
filesToGet = append(filesToGet, metaFiles...)
} else {
filesToGet = append(filesToGet, path)
}
}
// Parse each glob
queuedPaths := meta.ParseGlobs(args)

// Get the queued files
for i, file := range filesToGet {
log.Print(fmt.Sprint(i+1)+"/"+fmt.Sprint(len(filesToGet)), " ", log.ColorFile(file))
for i, path := range queuedPaths {
log.Print(fmt.Sprint(i+1)+"/"+fmt.Sprint(len(queuedPaths)), " ", log.ColorFile(path))

err = storage.Get(file, conf.StorageDir, gitDir, dry)
err = storage.Get(path, conf.StorageDir, gitDir, dry)
if err != nil {
log.Print(log.ColorRed(" ✘"), "Failed to get file", log.ColorFaint(err.Error()), "\n")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "error",
Message: "failed to get file",
Location: file,
Location: path,
})
}
}

// Warn if no files were queued
if len(queuedPaths) == 0 {
log.Print(log.ColorBold(log.ColorYellow("!")), "No files were queued")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "no files were queued",
Location: ".",
})
}

return nil
}

func getGetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get <path> [another-path] ...",
Use: "get <glob> [another-glob] ...",
Short: "Gets file(s) from storage",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
Expand All @@ -104,7 +75,6 @@ func getGetCmd() *cobra.Command {
RunE: runGetCmd,
}

cmd.Flags().BoolP("recurse", "r", false, "include subdirectories")
cmd.Flags().BoolP("dry", "d", false, "run without actually getting files")

return cmd
Expand Down
42 changes: 27 additions & 15 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {
return err
}
} else {
metaPaths = args
// Get meta files from globs
metaPaths = meta.ParseGlobs(args)
}

// Create colors
Expand All @@ -62,11 +63,20 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {
numFilesNotPulled := 0

// Print info about each file
log.Print(color.New(color.Bold).Sprint("file info "),
iconPulled, "up to date ",
iconOutdated, "out of date ",
iconNotPulled, "not present ",
)
if len(metaPaths) == 0 {
log.Print(log.ColorBold(log.ColorYellow("!")), "No files were queued")
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "warning",
Message: "no files queued",
Location: ".",
})
} else {
log.Print(color.New(color.Bold).Sprint("file info "),
iconPulled, "up to date ",
iconOutdated, "out of date ",
iconNotPulled, "not present ",
)
}
for _, path := range metaPaths {
relPath, err := git.GetRelativePath(".", path)
if err != nil {
Expand All @@ -83,7 +93,7 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {
// Get file info
metadata, err := meta.Load(path)
if err != nil {
log.Print(log.ColorRed("\n✘"), "File not in devious", log.ColorFile(relPath))
log.Print(log.ColorRed(" ✘"), "File not in devious", log.ColorFile(relPath))
log.JsonLogger.Issues = append(log.JsonLogger.Issues, log.JsonIssue{
Severity: "error",
Message: "file not in devious",
Expand Down Expand Up @@ -138,12 +148,14 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {
}

// Print overview
log.Print(color.New(color.Bold).Sprint("\ntotals"))
log.Print(
colorFilePulled(numFilesPulled), "up to date ",
colorFileOutdated(numFilesOutdated), "out of date ",
colorFileNotPulled(numFilesNotPulled), "not present ",
)
if len(metaPaths) > 0 {
log.Print(color.New(color.Bold).Sprint("\ntotals"))
log.Print(
colorFilePulled(numFilesPulled), "up to date ",
colorFileOutdated(numFilesOutdated), "out of date ",
colorFileNotPulled(numFilesNotPulled), "not present ",
)
}

log.Dump(jsonLogger)

Expand All @@ -152,9 +164,9 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {

func getStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status [path]",
Use: "status [glob] [another-glob]",
Short: "Gets the status of files tracked by devious",
Long: "Gets the status of files tracked by devious. If path(s) are provided, only those files will be checked. Otherwise, all files in the current git repository will be checked.",
Long: "Gets the status of files tracked by devious. If glob(s) are provided, only those globs will be checked. Otherwise, all files in the current git repository will be checked.",
PreRun: func(cmd *cobra.Command, args []string) {
log.PrintLogo()
},
Expand Down
2 changes: 2 additions & 0 deletions internal/git/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func AddIgnoreEntry(gitDir string, path string, dry bool) error {
if err != nil {
return err
}
ignoreEntry = "/" + ignoreEntry // Add leading slash

// Open the gitignore file, creating one if it doesn't exist
ignoreFile, err := os.OpenFile(filepath.Join(gitDir, ".gitignore"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
Expand Down Expand Up @@ -49,6 +50,7 @@ func RemoveIgnoreEntry(gitDir string, path string, dry bool) error {
if err != nil {
return err
}
ignoreEntry = "/" + ignoreEntry // Add leading slash

// Open the gitignore file
// if the gitignore file doesn't exist, there's nothing to do
Expand Down
7 changes: 5 additions & 2 deletions internal/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

// Gets a file path relative to the root
// Gets a file path relative to the provided root directory stripped of leading slashes
func GetRelativePath(rootDir string, filePath string) (string, error) {
absRootDir, err := filepath.Abs(rootDir)
if err != nil {
Expand All @@ -18,7 +18,10 @@ func GetRelativePath(rootDir string, filePath string) (string, error) {
return "", err
}

return strings.TrimPrefix(absFilePath, absRootDir), nil
relPath := strings.TrimPrefix(absFilePath, absRootDir)
relPathNoSlash := strings.TrimPrefix(relPath, "/")

return relPathNoSlash, nil
}

// Checks if the supplied path
Expand Down
4 changes: 2 additions & 2 deletions internal/git/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestGetRelativePath(t *testing.T) {
if err != nil {
t.Error(err)
}
if relativePath != "/subdir" && relativePath != "\\subdir" {
t.Error("Expected relative path to be /subdir or \\subdir")
if relativePath != "subdir" {
t.Error("Expected relative path to be subdir")
}
}

Expand Down
Loading

0 comments on commit 68db35c

Please sign in to comment.