Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4676 from hashicorp/cli/fix-shortimg-func-str-trim
Browse files Browse the repository at this point in the history
cli: Ensure that img string passed is proper length
  • Loading branch information
briancain committed May 9, 2023
2 parents 2b00bab + ba3cd06 commit b93232c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
16 changes: 12 additions & 4 deletions internal/cli/deployment_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ type DeploymentListCommand struct {
filterFlags filterFlags
}

func shortImg(img string) string {
func shortImg(img string) (string, error) {
if len(img) < 7 {
return "", fmt.Errorf("string not long enough to obtain short img: %s", img)
}

if strings.HasPrefix(img, "sha256:") {
return img[7:14]
return img[7:14], nil
}

return img[:7]
return img[:7], nil
}

// Add either language: or languages: based on how many values are specified
Expand Down Expand Up @@ -247,7 +251,11 @@ func (c *DeploymentListCommand) Run(args []string) int {
}

if img, ok := build.Labels["common/image-id"]; ok {
img = shortImg(img)
img, err = shortImg(img)
if err != nil {
app.UI.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return err
}

details = append(details, "image:"+img)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/cli/release_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ func (c *ReleaseListCommand) Run(args []string) int {
}

if img, ok := build.Labels["common/image-id"]; ok {
img = shortImg(img)
img, err = shortImg(img)
if err != nil {
app.UI.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return err
}

details = append(details, "image:"+img)
}
Expand Down
12 changes: 10 additions & 2 deletions internal/cli/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,11 @@ func (c *StatusCommand) FormatAppStatus(projectTarget string, appTarget string)
details = artDetails
}
if img, ok := deployBundle.Build.Labels["common/image-id"]; ok {
img = shortImg(img)
img, err = shortImg(img)
if err != nil {
c.ui.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return err
}

details = details + " image:" + img
}
Expand Down Expand Up @@ -705,7 +709,11 @@ func (c *StatusCommand) FormatAppStatus(projectTarget string, appTarget string)
details = artDetails
}
if img, ok := release.Preload.Build.Labels["common/image-id"]; ok {
img = shortImg(img)
img, err = shortImg(img)
if err != nil {
c.ui.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return err
}

details = details + " image:" + img
}
Expand Down

0 comments on commit b93232c

Please sign in to comment.