Skip to content

Commit

Permalink
Merge pull request #119 from trickest/feat/files-list
Browse files Browse the repository at this point in the history
Add files list command
  • Loading branch information
mhmdiaa authored Jul 2, 2024
2 parents c1e5d96 + 913f2b9 commit 515a096
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 16 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,19 @@ trickest files delete --file delete_me.txt
| --file | string | / | File or files (comma-separated) |


#### List files
Use the **list** command with the optional **--query** flag to list or search for files

```
trickest files list
```

| Flag | Type | Default | Description |
|----------------------|---------|----------|---------------------------------------------------------------------|
| --query | string | / | Filter listed files using the specified search query |
| --json | boolean | false | Display output in JSON format |


## Tools command
Manage [private tools](https://trickest.com/docs/tutorials/private-tools/private-tools-library/)

Expand Down
34 changes: 22 additions & 12 deletions cmd/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ var FilesCmd = &cobra.Command{
}

func init() {
FilesCmd.PersistentFlags().StringVar(&Files, "file", "", "File or files (comma-separated)")
FilesCmd.MarkPersistentFlagRequired("file")

FilesCmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
_ = FilesCmd.Flags().MarkHidden("workflow")
_ = FilesCmd.Flags().MarkHidden("project")
Expand All @@ -40,16 +37,29 @@ func init() {
}

func getMetadata(searchQuery string) ([]types.File, error) {
resp := request.Trickest.Get().DoF("file/?search=%s&vault=%s", searchQuery, util.GetVault())
if resp == nil || resp.Status() != http.StatusOK {
return nil, fmt.Errorf("unexpected response status code: %d", resp.Status())
}
var metadata types.Files
pageSize := 100

page := 1
var allFiles []types.File
for {
resp := request.Trickest.Get().DoF("file/?search=%s&vault=%s&page_size=%d&page=%d", searchQuery, util.GetVault(), pageSize, page)
if resp == nil || resp.Status() != http.StatusOK {
return nil, fmt.Errorf("unexpected response status code: %d", resp.Status())
}

var metadata types.Files
err := json.Unmarshal(resp.Body(), &metadata)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal file IDs response: %s", err)
}

allFiles = append(allFiles, metadata.Results...)

err := json.Unmarshal(resp.Body(), &metadata)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal file IDs response: %s", err)
if metadata.Next == "" {
break
}
page++
}

return metadata.Results, nil
return allFiles, nil
}
3 changes: 3 additions & 0 deletions cmd/files/filesCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var filesCreateCmd = &cobra.Command{

func init() {
FilesCmd.AddCommand(filesCreateCmd)

filesCreateCmd.Flags().StringVar(&Files, "file", "", "File or files (comma-separated)")
filesCreateCmd.MarkFlagRequired("file")
}

func createFile(filePath string) error {
Expand Down
3 changes: 3 additions & 0 deletions cmd/files/filesDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var filesDeleteCmd = &cobra.Command{

func init() {
FilesCmd.AddCommand(filesDeleteCmd)

filesDeleteCmd.Flags().StringVar(&Files, "file", "", "File or files (comma-separated)")
filesDeleteCmd.MarkFlagRequired("file")
}

func deleteFile(fileName string) error {
Expand Down
3 changes: 2 additions & 1 deletion cmd/files/filesGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ var filesGetCmd = &cobra.Command{
func init() {
FilesCmd.AddCommand(filesGetCmd)

filesGetCmd.Flags().StringVar(&Files, "file", "", "File or files (comma-separated)")
filesGetCmd.MarkFlagRequired("file")
filesGetCmd.Flags().StringVar(&outputDir, "output-dir", ".", "Path to directory which should be used to store files")

filesGetCmd.Flags().BoolVar(&partialNameMatch, "partial-name-match", false, "Get all files with a partial name match")
}

Expand Down
62 changes: 62 additions & 0 deletions cmd/files/filesList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package files

import (
"encoding/json"
"fmt"

"github.com/spf13/cobra"
"github.com/trickest/trickest-cli/types"
"github.com/xlab/treeprint"
)

var (
searchQuery string
jsonOutput bool
)

// filesListCmd represents the filesGet command
var filesListCmd = &cobra.Command{
Use: "list",
Short: "List files in the Trickest file storage",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
files, err := getMetadata(searchQuery)
if err != nil {
fmt.Printf("Error: %s\n", err)
} else {
printFiles(files, jsonOutput)
}
},
}

func init() {
FilesCmd.AddCommand(filesListCmd)

filesListCmd.Flags().StringVar(&searchQuery, "query", "", "Filter listed files using the specified search query")
filesListCmd.Flags().BoolVar(&jsonOutput, "json", false, "Display output in JSON format")
}

func printFiles(files []types.File, jsonOutput bool) {
var output string

if jsonOutput {
data, err := json.Marshal(files)
if err != nil {
fmt.Println("Error marshalling response data")
return
}
output = string(data)
} else {
tree := treeprint.New()
tree.SetValue("Files")
for _, file := range files {
fileSubBranch := tree.AddBranch("\U0001f4c4 " + file.Name) //📄
fileSubBranch.AddNode("\U0001f522 " + file.PrettySize) //🔢
fileSubBranch.AddNode("\U0001f4c5 " + file.ModifiedDate.Format("2006-01-02 15:04:05")) //📅
}

output = tree.String()
}

fmt.Println(output)
}
3 changes: 0 additions & 3 deletions types/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ type Files struct {
type File struct {
ID string `json:"id"`
Name string `json:"name"`
Vault string `json:"vault"`
TweID string `json:"twe_id"`
ArtifactID string `json:"artifact_id"`
Size int `json:"size"`
PrettySize string `json:"pretty_size"`
ModifiedDate time.Time `json:"modified_date"`
Expand Down

0 comments on commit 515a096

Please sign in to comment.