Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E List cmd and added API calls #3

Merged
merged 9 commits into from
Jun 22, 2022
Merged

Conversation

t-dedah
Copy link
Contributor

@t-dedah t-dedah commented Jun 19, 2022

Aim for the PR is to get early feedback and allow Sankalp to integrate his UI side changes with delete API

Progress

  1. Added functions for getting:-

    • Cache Usage Data
    • List of caches
    • Delete caches
  2. Added utils functions to:-

    • Parse input to query map
    • Get repo struct from repo flag and current directory
    • Format cache size for printing
  3. Completed happy path for List Command with basic stdout.

Upcoming changes

  1. Remove additional API call logs from stdout
  2. Improve Error Handling
  3. Add tests

@t-dedah t-dedah requested a review from a team as a code owner June 19, 2022 15:49
@t-dedah t-dedah changed the title Completed List cmd and added API calls E2E List cmd and added API calls Jun 19, 2022
cmd/client.go Outdated Show resolved Hide resolved
cmd/list.go Outdated Show resolved Hide resolved
ghRepo "github.com/cli/go-gh/pkg/repository"
)

type cacheInfo struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I see this is the only data model. Do we need a separate class to maintain models?

Copy link
Contributor

@kotewar kotewar Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should, we'd need one more wrapper model for parsing the API response as we also have total count in our list api response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another good yardstick would be to see if same model is being used in multiple files/packages. If yes, then it may need to moved to dedicated directory/class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this may seem redundant today but it may be important to define the layout of the project cleanly.

cmd/list.go Outdated Show resolved Hide resolved
cmd/list.go Outdated Show resolved Hide resolved
@@ -0,0 +1,90 @@
package cmd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason to name the package as cmd. If that is to specify that this package contains comands, then client.go and utils.go should not be part of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd was the default package created by cobra. We can update this to sometime like actions-cache.

Reason for keeping client.go and util.go part of the same package was thats how it was being done in the gh-s we have been trying to use as reference.

We can remove them from this package but then we will have to explicitly import them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would personally prefer to keep client and utils out and have cmd package only for commands code. This seems redundant today given the simplicity of the project, but once it starts growing it will get harder to manage.

cmd name seems okay as it points to "command" module if I get it right which is essentially the command handler.

@@ -0,0 +1,63 @@
package cmd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another similar suggestion - rather than one single utils file containing 3 different types of utils, would it be better to have a utils package which will have repoUtils, parseUtils and formatter as different files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will refactor and create multiple packages in the next PR. Its already in draft state

cmd/utils.go Outdated Show resolved Hide resolved
cmd/utils.go Outdated Show resolved Hide resolved
cmd/utils.go Outdated Show resolved Hide resolved
Comment on lines 32 to 33
order, _ := cmd.Flags().GetString("order")
sort, _ := cmd.Flags().GetString("sort")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the validation will be added later. Isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added now

@@ -21,20 +25,38 @@ var listCmd = &cobra.Command{
Short: "Lists the actions cache",
Long: `Lists the actions cache`,
Run: func(cmd *cobra.Command, args []string) {
repo, _ := cmd.Flags().GetString("repo")
COMMAND = "list"
r, _ := cmd.Flags().GetString("repo")
branch, _ := cmd.Flags().GetString("branch")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API expects the branch to be in full ref format. CLI should prepend that if user has not specified that.

cc @anuragc617 who may have better thoughts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is being handled while creating query params in generateQueryParams


fmt.Printf("Showing %d of %d cache entries in %s/%s\n", totalShownCacheEntry(len(caches)), len(caches), repo.Owner(), repo.Name())
for _, cache := range caches {
fmt.Printf("%s\t [%s]\t %s\t %s\n", cache.Key, formatCacheSize(cache.Size), cache.Ref, cache.LastAccessedAt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are not showing version here, there might be duplicate lines in the output here (same key and ref). Is that expected? Should it be changed?

Or: Add a TODO to display version here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be taken in next PR after confirmation from the designer.

cmd/utils.go Outdated Show resolved Hide resolved
@@ -6,11 +6,13 @@ import (
"github.com/spf13/cobra"
)

const VERSION = "0.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this version number expected to come from? Will cli tell us which extension version is installed?

cmd/list.go Outdated Show resolved Hide resolved
Comment on lines +64 to +67
if totalCaches < limit {
return totalCaches
}
return limit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ternary operator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Golang doesnt have ternary operators.

Comment on lines +48 to +58
actionsCachesResult := apiResults["actions_caches"].([]interface{})

var caches []cacheInfo
for _, item := range actionsCachesResult {
caches = append(caches, cacheInfo{
Key: item.(map[string]interface{})["key"].(string),
Ref: item.(map[string]interface{})["ref"].(string),
LastAccessedAt: item.(map[string]interface{})["last_accessed_at"].(string),
Size: item.(map[string]interface{})["size_in_bytes"].(float64),
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not use unmarshalling here to directly convert to the target datatype?

cc @anuragc617 @tiwarishub

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in deleteCaches as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are doing that in the next draft PR.

@bishal-pdMSFT
Copy link
Contributor

@anuragc617 / @tiwarishub you may want to review this from golang best practices perspective.

@t-dedah
Copy link
Contributor Author

t-dedah commented Jun 22, 2022

Pending Items

  1. Refactorization by creating separate packages
  2. Add Models
  3. Add Test
  4. Reuse Sankalp's function to print cache list.
  5. Improve Error handling

We will take this up in next PR as the initial PR was to create MVP.

@t-dedah t-dedah merged commit 08af75f into main Jun 22, 2022
@t-dedah t-dedah deleted the t-dedah/list-and-delete-basic branch June 22, 2022 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants