Added test cases for Delete (#8)

* Completed List cmd and added API calls

* Minor comments and add delete code to pass linting

* Typo in descriptions

* Minor comments

* Validations

* Validations-1

* improved branch flag validation

* removed build

* working after refactory with bad names

* Command working, test not working

* Corrected creation of service

* Finalized structure using service

* Deleted tests

* cleanup

* cleanup

* cleanup

* removed space with tab

* aligned types in model.go

* Update model.go

* resolved comments

* Refactor

* removed long descriptions

* Working incomplete tests

* Completed tests

* cleanup

* checks

* PR comments

* PR comments

* minor comment issue

* minor comment issue

* Added test cases for Delete

* updated tests to work with workflow

* Added missing error condition

* Updated tests to support new option service

* Improved eror handling for list

* Fixed test case

* Improved error handling

* Error handling and test cases for delete API calls

* Added test case for user confirmation delete.

* Removed unused import from test

* Fixed test case for error scenario

* Upgraded go-gh

* reusing rest client error

* Fix for failing windows test cases

* help cmd removed when cache isnt present on delete

* Pretty print ratio and space between cols modified

* Error handling wrapping

* Reverted back error message after silencing help

Co-authored-by: t-dedah <t-dedah@github.com>
Co-authored-by: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com>
This commit is contained in:
Sankalp Kotewar
2022-07-14 02:33:36 +05:30
committed by GitHub
parent db34270ecb
commit 790f19d8a6
5 changed files with 237 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
package cmd
import (
"errors"
"fmt"
"net/url"
"strings"
@@ -9,9 +10,12 @@ import (
"github.com/actions/gh-actions-cache/internal"
"github.com/actions/gh-actions-cache/service"
"github.com/actions/gh-actions-cache/types"
"github.com/cli/go-gh/pkg/api"
"github.com/spf13/cobra"
)
var choice string = ""
func NewCmdDelete() *cobra.Command {
COMMAND = "delete"
f := types.DeleteOptions{}
@@ -31,11 +35,12 @@ func NewCmdDelete() *cobra.Command {
return err
}
// This will silence the usage (help) message as they are not needed for errors beyond this point
cmd.SilenceUsage = true
artifactCache, err := service.NewArtifactCache(repo, COMMAND, VERSION)
if err != nil {
fmt.Printf("error connecting to %s\n", repo.Host())
fmt.Println("check your internet connection or https://githubstatus.com")
return nil
return types.HandledError{Message: err.Error(), InnerError: err}
}
queryParams := url.Values{}
@@ -44,7 +49,14 @@ func NewCmdDelete() *cobra.Command {
if !f.Confirm {
matchedCaches, err := getCacheListWithExactMatch(f, artifactCache)
if err != nil {
return err
var httpError api.HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
return types.HandledError{Message: "The given repo does not exist.", InnerError: err}
} else if errors.As(err, &httpError) && httpError.StatusCode >= 400 && httpError.StatusCode < 500 {
return types.HandledError{Message: httpError.Message, InnerError: err}
} else {
return types.HandledError{Message: "We could not process your request due to internal error.", InnerError: err}
}
}
matchedCachesLen := len(matchedCaches)
if matchedCachesLen == 0 {
@@ -52,22 +64,31 @@ func NewCmdDelete() *cobra.Command {
}
fmt.Printf("You're going to delete %s", internal.PrintSingularOrPlural(matchedCachesLen, "cache entry\n\n", "cache entries\n\n"))
internal.PrettyPrintTrimmedCacheList(matchedCaches)
choice := ""
prompt := &survey.Select{
Message: "Are you sure you want to delete the cache entries?",
Options: []string{"Delete", "Cancel"},
}
err = survey.AskOne(prompt, &choice)
if err != nil {
return fmt.Errorf("Error occured while taking input from user while trying to delete cache")
fmt.Println("Error occured while taking input from user while trying to delete cache")
return types.HandledError{Message: "Error occured while taking input from user while trying to delete cache.", InnerError: err}
}
f.Confirm = choice == "Delete"
fmt.Println()
}
if f.Confirm {
cachesDeleted, err := artifactCache.DeleteCaches(queryParams)
if err != nil {
return err
var httpError api.HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
return types.HandledError{Message: fmt.Sprintf("Cache with input key '%s' does not exist", f.Key), InnerError: err}
} else if errors.As(err, &httpError) && httpError.StatusCode >= 400 && httpError.StatusCode < 500 {
return types.HandledError{Message: httpError.Message, InnerError: err}
} else {
return types.HandledError{Message: "We could not process your request due to internal error.", InnerError: err}
}
}
if cachesDeleted > 0 {

171
cmd/delete_test.go Normal file
View File

@@ -0,0 +1,171 @@
package cmd
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/actions/gh-actions-cache/internal"
"github.com/actions/gh-actions-cache/types"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestDeleteWithIncorrectArguments(t *testing.T) {
t.Cleanup(gock.Off)
cmd := NewCmdDelete()
cmd.SetArgs([]string{})
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, err, fmt.Errorf("accepts 1 arg(s), received 0"))
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
}
func TestDeleteWithIncorrectRepo(t *testing.T) {
t.Cleanup(gock.Off)
cmd := NewCmdDelete()
cmd.SetArgs([]string{"--repo", "testOrg/testRepo/123/123", "cacheName"})
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, err, fmt.Errorf("expected the \"[HOST/]OWNER/REPO\" format, got \"testOrg/testRepo/123/123\""))
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
}
func TestDeleteWithIncorrectRepoForDeleteCaches(t *testing.T) {
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Get("/repos/testOrg/testRepo/actions/caches").
MatchParam("key", "cacheName").
Reply(404).
JSON(`{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/actions/cache#list-github-actions-caches-for-a-repository"
}`)
cmd := NewCmdDelete()
cmd.SetArgs([]string{"--repo", "testOrg/testRepo", "cacheName"})
err := cmd.Execute()
assert.NotNil(t, err)
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
var customError types.HandledError
errors.As(err, &customError)
assert.Equal(t, customError.Message, "The given repo does not exist.")
}
func TestDeleteSuccessWithConfirmFlagProvided(t *testing.T) {
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Delete("/repos/testOrg/testRepo/actions/caches").
MatchParam("key", "2022-06-29T13:33:49").
Reply(200).
JSON(`{
"total_count": 1,
"actions_caches": [
{
"id": 1293,
"ref": "refs/heads/main",
"key": "2022-06-29T13:33:49",
"version": "803758043e242677f6b8650742372d82ded436d99b2a8a09bc3b6ed77cd6aec2",
"last_accessed_at": "2022-06-29T13:33:52.280000000Z",
"created_at": "2022-06-29T13:33:52.280000000Z",
"size_in_bytes": 29747
}
]
}`)
cmd := NewCmdDelete()
cmd.SetArgs([]string{"--repo", "testOrg/testRepo", "2022-06-29T13:33:49", "--confirm"})
err := cmd.Execute()
assert.Nil(t, err)
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
}
func TestDeleteFailureWhileTakingUserInput(t *testing.T) {
t.Cleanup(gock.Off)
choice = "Delete"
gock.New("https://api.github.com").
Get("/repos/testOrg/testRepo/actions/caches").
MatchParam("key", "2022-06-29T13:33:49").
Reply(200).
JSON(`{
"total_count": 1,
"actions_caches": [
{
"id": 1293,
"ref": "refs/heads/main",
"key": "2022-06-29T13:33:49",
"version": "803758043e242677f6b8650742372d82ded436d99b2a8a09bc3b6ed77cd6aec2",
"last_accessed_at": "2022-06-29T13:33:52.280000000Z",
"created_at": "2022-06-29T13:33:52.280000000Z",
"size_in_bytes": 29747
}
]
}`)
cmd := NewCmdDelete()
cmd.SetArgs([]string{"--repo", "testOrg/testRepo", "2022-06-29T13:33:49"})
err := cmd.Execute()
assert.NotNil(t, err)
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
}
func TestDeleteWithUnauthorizedRequestForDeleteCaches(t *testing.T) {
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Delete("/repos/testOrg/testRepo/actions/caches").
Reply(401).
JSON(`{
"message": "Must have admin rights to Repository.",
"documentation_url": "https://docs.github.com/rest/actions/cache#delete-a-github-actions-cache-for-a-repository-using-a-cache-id"
}`)
cmd := NewCmdDelete()
cmd.SetArgs([]string{"--repo", "testOrg/testRepo", "cacheKey", "--confirm"})
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, reflect.TypeOf(err), reflect.TypeOf(types.HandledError{}))
var customError types.HandledError
errors.As(err, &customError)
assert.Equal(t, customError.Message, "Must have admin rights to Repository.")
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
}
func TestDeleteWithInternalServerErrorForDeleteCaches(t *testing.T) {
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Delete("/repos/testOrg/testRepo/actions/caches").
Reply(500).
JSON(`{
"message": "Internal Server Error",
"documentation_url": "https://docs.github.com/rest/reference/actions#get-github-actions-cache-Delete-for-a-repository"
}`)
cmd := NewCmdDelete()
cmd.SetArgs([]string{"--repo", "testOrg/testRepo", "cacheKey", "--confirm"})
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, reflect.TypeOf(err), reflect.TypeOf(types.HandledError{}))
var customError types.HandledError
errors.As(err, &customError)
assert.Equal(t, customError.Message, "We could not process your request due to internal error.")
assert.True(t, gock.IsDone(), internal.PrintPendingMocks(gock.Pending()))
}

10
go.mod
View File

@@ -8,14 +8,13 @@ require (
github.com/cli/go-gh v0.0.4-0.20220623035622-91ca4ef447d4
github.com/nleeper/goment v1.4.4
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.0
)
require github.com/pmezard/go-difflib v1.0.0 // indirect
require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/safeexec v1.0.0
github.com/cli/shurcooL-graphql v0.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
@@ -23,14 +22,13 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mattn/go-isatty v0.0.8
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
github.com/spf13/pflag v1.0.5 // indirect
github.com/tkuchiki/go-timezone v0.2.0 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
golang.org/x/text v0.3.6 // indirect
gopkg.in/h2non/gock.v1 v1.1.2
gopkg.in/yaml.v3 v3.0.1 // indirect

33
go.sum
View File

@@ -1,15 +1,11 @@
github.com/AlecAivazis/survey/v2 v2.3.5 h1:A8cYupsAZkjaUmhtTYv3sSqc7LO5mp1XDfqe5E/9wRQ=
github.com/AlecAivazis/survey/v2 v2.3.5/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/TwiN/go-color v1.1.0 h1:yhLAHgjp2iAxmNjDiVb6Z073NE65yoaPlcki1Q22yyQ=
github.com/TwiN/go-color v1.1.0/go.mod h1:aKVf4e1mD4ai2FtPifkDPP5iyoCwiK08YGzGwerjKo0=
github.com/cli/go-gh v0.0.3 h1:GcVgUa7q0SeauIRbch3VSUXVij6+c49jtAHv7WuWj5c=
github.com/cli/go-gh v0.0.3/go.mod h1:J1eNgrPJYAUy7TwPKj7GW1ibqI+WCiMndtyzrCyZIiQ=
github.com/cli/go-gh v0.0.4-0.20220623035622-91ca4ef447d4 h1:6WrekNBE2Y+Xl9OCl7vsg49SSN68hwaVryfEawQevaQ=
github.com/cli/go-gh v0.0.4-0.20220623035622-91ca4ef447d4/go.mod h1:Y/QFb/VxnXQH0W4VlP+507HVxMzQ430x8kdjUuVcono=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
@@ -17,15 +13,11 @@ github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5
github.com/cli/shurcooL-graphql v0.0.1 h1:/9J3t9O6p1B8zdBBtQighq5g7DQRItBwuwGh3SocsKM=
github.com/cli/shurcooL-graphql v0.0.1/go.mod h1:U7gCSuMZP/Qy7kbqkk5PrqXEeDgtfG5K+W+u8weorps=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
@@ -47,51 +39,40 @@ github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/nleeper/goment v1.4.4 h1:GlMTpxvhueljArSunzYjN9Ri4SOmpn0Vh2hg2z/IIl8=
github.com/nleeper/goment v1.4.4/go.mod h1:zDl5bAyDhqxwQKAvkSXMRLOdCowrdZz53ofRJc4VhTo=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tkuchiki/go-timezone v0.2.0 h1:yyZVHtQRVZ+wvlte5HXvSpBkR0dPYnPEIgq9qqAqltk=
github.com/tkuchiki/go-timezone v0.2.0/go.mod h1:b1Ean9v2UXtxSq4TZF0i/TU9NuoWa9hOzOKoGCV2zqY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -99,9 +80,5 @@ gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"math"
"os"
"os/exec"
"strconv"
"strings"
"unicode/utf8"
@@ -11,8 +13,10 @@ import (
"github.com/actions/gh-actions-cache/types"
gh "github.com/cli/go-gh"
ghRepo "github.com/cli/go-gh/pkg/repository"
"github.com/moby/term"
"github.com/cli/safeexec"
"github.com/mattn/go-isatty"
"github.com/nleeper/goment"
"golang.org/x/term"
)
const MB_IN_BYTES = 1024 * 1024
@@ -45,14 +49,13 @@ func FormatCacheSize(size_in_bytes float64) string {
}
func PrettyPrintCacheList(caches []types.ActionsCache) {
fd := os.Stdout.Fd()
ws, _ := term.GetWinsize(fd)
width := math.Max(math.Min(float64(ws.Width), 180), 100)
width, _, _ := getTerminalWidth(os.Stdout)
width = int(math.Max(float64(width), 100))
sizeWidth := SIZE_COLUMN_WIDTH // hard-coded size as the content is scoped
timeWidth := LAST_ACCESSED_AT_COLUMN_WIDTH // hard-coded size as the content is scoped
keyWidth := int(math.Floor(0.75 * (width - 15 - 20)))
refWidth := int(math.Floor(0.25 * (width - 15 - 20)))
keyWidth := int(math.Floor(0.65 * float64(width-15-20)))
refWidth := int(math.Floor(0.20 * float64(width-15-20)))
for _, cache := range caches {
var formattedRow string = getFormattedCacheInfo(cache, keyWidth, sizeWidth, refWidth, timeWidth)
@@ -90,7 +93,7 @@ func getFormattedCacheInfo(cache types.ActionsCache, keyWidth int, sizeWidth int
size := trimOrPad(fmt.Sprintf("[%s]", FormatCacheSize(cache.SizeInBytes)), sizeWidth)
ref := trimOrPad(cache.Ref, refWidth)
time := trimOrPad(lastAccessedTime(cache.LastAccessedAt), timeWidth)
return fmt.Sprintf("%s %s %s %s", key, size, ref, time)
return fmt.Sprintf("%s %s %s %s", key, size, ref, time)
}
func RedTick() string {
@@ -106,3 +109,21 @@ func PrintSingularOrPlural(count int, singularStr string, pluralStr string) stri
}
return fmt.Sprintf("%d %s", count, pluralStr)
}
func getTerminalWidth(f *os.File) (int, int, error) {
if !isatty.IsCygwinTerminal(f.Fd()) {
return term.GetSize(int(f.Fd()))
}
tputExe, err := safeexec.LookPath("tput")
if err != nil {
return 100, 100, nil
}
tputCmd := exec.Command(tputExe, "cols")
tputCmd.Stdin = os.Stdin
if out, err := tputCmd.Output(); err == nil {
if w, err := strconv.Atoi(strings.TrimSpace(string(out))); err == nil {
return w, 100, nil
}
}
return 100, 100, nil
}