From aaaddcf566c2fabded892d55d2df90804d25ce27 Mon Sep 17 00:00:00 2001 From: Mohammed Diaa Date: Tue, 2 Jul 2024 13:34:00 +0300 Subject: [PATCH 1/5] Implement pagination in files.getMetadata --- cmd/files/files.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/cmd/files/files.go b/cmd/files/files.go index 29272ab..ddb863f 100644 --- a/cmd/files/files.go +++ b/cmd/files/files.go @@ -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") @@ -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 } From 422fa96e768d6b8869443c99494621a8f3e10c7a Mon Sep 17 00:00:00 2001 From: Mohammed Diaa Date: Tue, 2 Jul 2024 13:35:18 +0300 Subject: [PATCH 2/5] Make --file a local (required) flag to the commands that need it only --- cmd/files/filesCreate.go | 3 +++ cmd/files/filesDelete.go | 3 +++ cmd/files/filesGet.go | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/files/filesCreate.go b/cmd/files/filesCreate.go index 20c6be8..37b93f1 100644 --- a/cmd/files/filesCreate.go +++ b/cmd/files/filesCreate.go @@ -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 { diff --git a/cmd/files/filesDelete.go b/cmd/files/filesDelete.go index 1274d55..87a9182 100644 --- a/cmd/files/filesDelete.go +++ b/cmd/files/filesDelete.go @@ -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 { diff --git a/cmd/files/filesGet.go b/cmd/files/filesGet.go index 5726a66..5a8705c 100644 --- a/cmd/files/filesGet.go +++ b/cmd/files/filesGet.go @@ -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") } From ba090f8f79eae0065a5f496715c7bfceb4b4eb4c Mon Sep 17 00:00:00 2001 From: Mohammed Diaa Date: Tue, 2 Jul 2024 13:37:47 +0300 Subject: [PATCH 3/5] Simplify file definition --- types/files.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/types/files.go b/types/files.go index 3735cf6..83fe41c 100644 --- a/types/files.go +++ b/types/files.go @@ -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"` From fa91bfc4730bc24325ce8feb47e171ae282d07be Mon Sep 17 00:00:00 2001 From: Mohammed Diaa Date: Tue, 2 Jul 2024 13:52:30 +0300 Subject: [PATCH 4/5] Add files list command --- cmd/files/filesList.go | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 cmd/files/filesList.go diff --git a/cmd/files/filesList.go b/cmd/files/filesList.go new file mode 100644 index 0000000..458583d --- /dev/null +++ b/cmd/files/filesList.go @@ -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) +} From 913f2b946546a56aeee065b6c653147aa5e0eb7a Mon Sep 17 00:00:00 2001 From: Mohammed Diaa Date: Tue, 2 Jul 2024 13:56:56 +0300 Subject: [PATCH 5/5] Add files list to README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index e35bca1..45266d8 100644 --- a/README.md +++ b/README.md @@ -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/)