-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
451 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cmd | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
"github.com/treeverse/lakefs/pkg/git" | ||
"github.com/treeverse/lakefs/pkg/local" | ||
) | ||
|
||
const ( | ||
localListMinArgs = 0 | ||
localListMaxArgs = 1 | ||
|
||
indicesListTemplate = `{{.IndicesListTable | table -}}` | ||
) | ||
|
||
var localListCmd = &cobra.Command{ | ||
Use: "list [directory]", | ||
Short: "find and list directories that are synced with lakeFS", | ||
Args: cobra.RangeArgs(localListMinArgs, localListMaxArgs), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
dir := "." | ||
if len(args) > 0 { | ||
dir = args[0] | ||
} | ||
abs, err := filepath.Abs(dir) | ||
if err != nil { | ||
DieErr(err) | ||
} | ||
gitRoot, err := git.GetRepositoryPath(abs) | ||
if err == nil { | ||
abs = gitRoot | ||
} else if !(errors.Is(err, git.ErrNotARepository) || errors.Is(err, git.ErrNoGit)) { // allow support in environments with no git | ||
DieErr(err) | ||
} | ||
|
||
dirs, err := local.FindIndices(abs) | ||
if err != nil { | ||
DieErr(err) | ||
} | ||
|
||
var rows [][]interface{} | ||
for _, d := range dirs { | ||
idx, err := local.ReadIndex(d) | ||
if err != nil { | ||
DieErr(err) | ||
} | ||
remote, err := idx.GetCurrentURI() | ||
if err != nil { | ||
DieErr(err) | ||
} | ||
rows = append(rows, table.Row{d, remote, idx.AtHead}) | ||
} | ||
data := struct { | ||
IndicesListTable *Table | ||
}{ | ||
IndicesListTable: &Table{ | ||
Headers: []interface{}{ | ||
"Directory", | ||
"Remote URI", | ||
"Last synced HEAD", | ||
}, | ||
Rows: rows, | ||
}, | ||
} | ||
Write(indicesListTemplate, data) | ||
}, | ||
} | ||
|
||
//nolint:gochecknoinits | ||
func init() { | ||
localCmd.AddCommand(localListCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package fileutil_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/treeverse/lakefs/pkg/fileutil" | ||
) | ||
|
||
func TestFindInParents(t *testing.T) { | ||
root := t.TempDir() | ||
dirTree := filepath.Join(root, "foo", "bar", "baz", "taz") | ||
require.NoError(t, os.MkdirAll(dirTree, fileutil.DefaultDirectoryMask)) | ||
t.Run("does not exist", func(t *testing.T) { | ||
found, err := fileutil.FindInParents(root, ".doesnotexist21348329043289") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if found != "" { | ||
t.Errorf("expected found to be empty, got %v", found) | ||
} | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
deep string | ||
filename string | ||
filepath string | ||
find bool | ||
}{ | ||
{ | ||
name: "find_at_leaf", | ||
deep: filepath.Join(root, "foo", "bar", "baz"), | ||
filename: "some_file0", | ||
filepath: filepath.Join(root, "foo", "bar", "baz", "some_file0"), | ||
find: true, | ||
}, | ||
{ | ||
name: "find_at_root", | ||
deep: filepath.Join(root, "foo", "bar", "baz"), | ||
filename: "some_file1", | ||
filepath: filepath.Join(root, "some_file1"), | ||
find: true, | ||
}, | ||
{ | ||
name: "find_at_subpath", | ||
deep: filepath.Join(root, "foo", "bar", "baz", "taz"), | ||
filename: "some_file2", | ||
filepath: filepath.Join(root, "foo", "some_file2"), | ||
find: true, | ||
}, | ||
{ | ||
name: "not_found_above", | ||
deep: filepath.Join(root, "foo", "bar", "baz"), | ||
filename: "some_file3", | ||
filepath: filepath.Join(root, "foo", "bar", "baz", "taz", "some_file3"), | ||
find: false, | ||
}, | ||
{ | ||
name: "doesnt_exist", | ||
deep: filepath.Join(root, "foo", "bar", "baz"), | ||
filename: ".doesnotexist21348329043289", | ||
filepath: filepath.Join(root, "foo", "bar", "some_file4"), | ||
find: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f, err := os.Create(tt.filepath) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
|
||
found, err := fileutil.FindInParents(tt.deep, tt.filename) | ||
require.NoError(t, err) | ||
if tt.find { | ||
require.Equal(t, tt.filepath, found) | ||
} else { | ||
require.Equal(t, "", found) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.