Skip to content

Commit

Permalink
feat: Add autocomplete for file path of artifact files inspect (#947)
Browse files Browse the repository at this point in the history
## Description:
This PR adds a couple of improvements to the recent `file inspect`:

1. Autocomplete for files inside the artifact
<img width="1727" alt="Screenshot 2023-07-19 at 13 16 57"
src="https://github.com/kurtosis-tech/kurtosis/assets/1737857/cb27c5ed-5c70-4b12-bd64-a6c8d0176cf9">
2. Prettier coloring of files that contain a text description
<img width="1154" alt="Screenshot 2023-07-19 at 13 25 22"
src="https://github.com/kurtosis-tech/kurtosis/assets/1737857/fb148e5b-90a4-45f3-a67e-7d9cd365f027">
3. Render correctly root level files
4. Drop artifact name on file tree

## Is this change user facing?
YES
<!-- If yes, please add the "user facing" label to the PR -->
<!-- If yes, don't forget to include docs changes where relevant -->

## References (if applicable):
<!-- Add relevant Github Issues, Discord threads, or other helpful
information. -->
  • Loading branch information
victorcolombo committed Jul 20, 2023
1 parent fbb0aee commit f72dfce
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
73 changes: 63 additions & 10 deletions cli/cli/commands/files/inspect/inspect.go
Expand Up @@ -41,8 +41,8 @@ const (
kurtosisBackendCtxKey = "kurtosis-backend"
engineClientCtxKey = "engine-client"
emptyFileStr = ""

byteGroup = 1024
rootLevelFileStr = ""
byteGroup = 1024
)

var sizeSuffix = []byte{'K', 'M', 'G', 'T', 'P'}
Expand Down Expand Up @@ -72,7 +72,7 @@ var FilesInspectCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosis
IsOptional: isFilePathArgOptional,
IsGreedy: isFilePathArgGreedy,
DefaultValue: emptyFilePath,
ArgCompletionProvider: nil,
ArgCompletionProvider: args.NewManualCompletionsProvider(getCompletionFunc(enclaveIdentifierArgKey, artifactIdentifierArgKey)),
},
},
RunFunc: run,
Expand Down Expand Up @@ -118,7 +118,7 @@ func run(
fileDescriptions := filesInspectResponse.GetFileDescriptions()

if filePath == "" {
logrus.Infof("Artifact '%v' contents:\n%v", artifactIdentifierName, buildTree(artifactIdentifierName, fileDescriptions))
logrus.Infof("Artifact '%v' contents:%v", artifactIdentifierName, buildTree(fileDescriptions))
return nil
}
index := slices.IndexFunc(fileDescriptions, func(desc *kurtosis_core_rpc_api_bindings.FileArtifactContentsFileDescription) bool {
Expand Down Expand Up @@ -160,18 +160,26 @@ func (nm *treeMap) addNodeIfNotPresent(s string) *treeMap {
}

// Assembles a file tree string
func buildTree(artifactIdentifierName string, fileDescritions []*kurtosis_core_rpc_api_bindings.FileArtifactContentsFileDescription) string {
tree := treeprint.NewWithRoot(color.BlueString(artifactIdentifierName))
func buildTree(fileDescritions []*kurtosis_core_rpc_api_bindings.FileArtifactContentsFileDescription) string {
tree := treeprint.NewWithRoot("")
tMap := &treeMap{map[string]*treeMap{}, tree}
for _, fileDescription := range fileDescritions {
dir, file := filepath.Split(fileDescription.GetPath())
subdirs := strings.Split(filepath.Clean(dir), string(filepath.Separator))
curTree := tMap
for _, subdir := range subdirs {
curTree = curTree.addBranchIfNotPresent(color.GreenString(subdir))
if dir != rootLevelFileStr {
subdirs := strings.Split(filepath.Clean(dir), string(filepath.Separator))
for _, subdir := range subdirs {
curTree = curTree.addBranchIfNotPresent(color.GreenString(subdir))
}
}
if file != emptyFileStr {
curTree.addNodeIfNotPresent(fmt.Sprintf("%v [%s]", file, humanReadableSize(fileDescription.GetSize())))
var coloredFile string
if fileDescription.TextPreview != nil {
coloredFile = color.BlueString(file)
} else {
coloredFile = file
}
curTree.addNodeIfNotPresent(fmt.Sprintf("%v [%s]", coloredFile, humanReadableSize(fileDescription.GetSize())))
}
}
return tree.String()
Expand All @@ -188,3 +196,48 @@ func humanReadableSize(size uint64) string {
}
return fmt.Sprintf("%3.1f%c", fSize, sizeSuffix[suffixIdx])
}

func getCompletionFunc(enclaveArgKey string, artifactArgKey string) func(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
return func(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine()
if err != nil {
return nil, stacktrace.Propagate(
err,
"An error occurred connecting to the Kurtosis engine for retrieving the names for tab completion",
)
}

enclaveIdentifier, err := previousArgs.GetNonGreedyArg(enclaveArgKey)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the enclave ID using key '%v'", enclaveArgKey)
}
enclave, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier)
if err != nil {
return nil, stacktrace.Propagate(
err,
"An error occurred getting the enclave identifiers",
)
}

artifactIdentifier, err := previousArgs.GetNonGreedyArg(artifactArgKey)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the artifact ID using key '%v'", artifactArgKey)
}
fileArtifactContents, err := enclave.InspectFilesArtifact(ctx, services.FileArtifactName(artifactIdentifier))
if err != nil {
return nil, stacktrace.Propagate(
err,
"An error occurred getting the file artifacts",
)
}
fileArtifactContentPaths := []string{}
for _, fileArtifactDescription := range fileArtifactContents.GetFileDescriptions() {
fileArtifactContentPath := fileArtifactDescription.GetPath()
if fileArtifactDescription.TextPreview != nil {
fileArtifactContentPaths = append(fileArtifactContentPaths, fileArtifactContentPath)
}
}

return fileArtifactContentPaths, nil
}
}
19 changes: 12 additions & 7 deletions cli/cli/commands/files/inspect/inspect_test.go
Expand Up @@ -6,16 +6,17 @@ import (
"testing"
)

const expectedTreeStr = `artifact
└── path
├── to
│ ├── file.txt [2.0K]
│ └── another.txt [ 123]
└── yet_another.txt [2.3M]
const expectedTreeStr = `
├── path
│ ├── to
│ │ ├── file.txt [2.0K]
│ │ └── another.txt [ 123]
│ └── yet_another.txt [2.3M]
└── root.txt [ 1]
`

func TestTreeBuilding(t *testing.T) {
treeStr := buildTree("artifact", []*kurtosis_core_rpc_api_bindings.FileArtifactContentsFileDescription{
treeStr := buildTree([]*kurtosis_core_rpc_api_bindings.FileArtifactContentsFileDescription{
{
Path: "path/to/file.txt",
Size: 2000,
Expand All @@ -28,6 +29,10 @@ func TestTreeBuilding(t *testing.T) {
Path: "path/yet_another.txt",
Size: 2*1024*1024 + 300*1024,
},
{
Path: "root.txt",
Size: 1,
},
})
require.Equal(t, treeStr, expectedTreeStr)
}

0 comments on commit f72dfce

Please sign in to comment.