Skip to content

Commit

Permalink
Merge pull request #39 from Phillezi/37-nil-pointer-dereference-when-…
Browse files Browse the repository at this point in the history
…trying-to-drop-cache-and-not-logged-in

fixed nil dereference on ps command while not having active session
  • Loading branch information
Phillezi authored Sep 19, 2024
2 parents b7a927d + c6f9a15 commit 14fda5b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
9 changes: 6 additions & 3 deletions pkg/v1/auth/client/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
)

func (c *Client) Deployments() ([]body.DeploymentRead, error) {
if c.Session.Resources != nil && c.Session.Resources.Deployments != nil && !c.Session.Resources.Deployments.IsExpired() {
if c.Session != nil &&
c.Session.Resources != nil &&
c.Session.Resources.Deployments != nil &&
!c.Session.Resources.Deployments.IsExpired() {
return c.Session.Resources.Deployments.Data, nil
}

Expand Down Expand Up @@ -44,7 +47,7 @@ func (c *Client) Deployments() ([]body.DeploymentRead, error) {
}

func (c *Client) DropDeploymentsCache() {
if c.Session.Resources != nil {
c.Session.Resources.DropVmsCache()
if c.Session != nil && c.Session.Resources != nil {
c.Session.Resources.DropDeploymentsCache()
}
}
7 changes: 5 additions & 2 deletions pkg/v1/auth/client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
)

func (c *Client) User() (*body.UserRead, error) {
if c.Session.Resources != nil && c.Session.Resources.User != nil && !c.Session.Resources.User.IsExpired() {
if c.Session != nil &&
c.Session.Resources != nil &&
c.Session.Resources.User != nil &&
!c.Session.Resources.User.IsExpired() {
return c.Session.Resources.User.Data, nil
}

Expand Down Expand Up @@ -44,7 +47,7 @@ func (c *Client) User() (*body.UserRead, error) {
}

func (c *Client) DropUserCache() {
if c.Session.Resources != nil {
if c.Session != nil && c.Session.Resources != nil {
c.Session.Resources.DropUserCache()
}
}
7 changes: 5 additions & 2 deletions pkg/v1/auth/client/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
)

func (c *Client) Vms() ([]body.VmRead, error) {
if c.Session.Resources != nil && c.Session.Resources.Vms != nil && !c.Session.Resources.Vms.IsExpired() {
if c.Session != nil &&
c.Session.Resources != nil &&
c.Session.Resources.Vms != nil &&
!c.Session.Resources.Vms.IsExpired() {
return c.Session.Resources.Vms.Data, nil
}

Expand Down Expand Up @@ -44,7 +47,7 @@ func (c *Client) Vms() ([]body.VmRead, error) {
}

func (c *Client) DropVmsCache() {
if c.Session.Resources != nil {
if c.Session != nil && c.Session.Resources != nil {
c.Session.Resources.DropVmsCache()
}
}
12 changes: 9 additions & 3 deletions pkg/v1/auth/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ func (r *CachedResource[T]) IsExpired() bool {
}

func (r *Resources) DropUserCache() {
r.User = nil
if r.User != nil {
r.User = nil
}
}

func (r *Resources) DropDeploymentsCache() {
r.Deployments = nil
if r.Deployments != nil {
r.Deployments = nil
}
}

func (r *Resources) DropVmsCache() {
r.Vms = nil
if r.Vms != nil {
r.Vms = nil
}
}
3 changes: 3 additions & 0 deletions pkg/v1/commands/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

func Ps(all bool) {
c := client.Get()
if !c.HasValidSession() {
logrus.Fatal("no valid session, log in and try again")
}

c.DropDeploymentsCache()
depls, err := c.Deployments()
Expand Down

0 comments on commit 14fda5b

Please sign in to comment.