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 #4867 from hashicorp/job_guard_nil_app
Browse files Browse the repository at this point in the history
Add guards against nil derefernce panics in job/task CLI commands
  • Loading branch information
catsby committed Aug 7, 2023
2 parents 88abbbc + 5ecb2b2 commit 7958a15
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .changelog/4867.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Fix possible issues with deleted applications or projects failing to render in output
```
1 change: 0 additions & 1 deletion internal/cli/job_get_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (c *JobGetStreamCommand) Run(args []string) int {

func (c *JobGetStreamCommand) Flags() *flag.Sets {
return c.flagSet(flagSetOperation, func(set *flag.Sets) {
//f := set.NewSet("Command Options")
})
}

Expand Down
18 changes: 15 additions & 3 deletions internal/cli/job_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ func (c *JobInspectCommand) Run(args []string) int {
}

c.ui.Output("Job Configuration", terminal.WithHeaderStyle())
appProjectName := resp.GetApplication().GetProject()
if appProjectName == "" {
appProjectName = "deleted"
}
appName := resp.GetApplication().GetApplication()
if appName == "" {
appName = "deleted"
}
workspaceName := resp.GetWorkspace().GetWorkspace()
if workspaceName == "" {
workspaceName = "deleted"
}
c.ui.NamedValues([]terminal.NamedValue{
{
Name: "ID", Value: resp.Id,
Expand All @@ -183,13 +195,13 @@ func (c *JobInspectCommand) Run(args []string) int {
Name: "Target Runner", Value: targetRunner,
},
{
Name: "Workspace", Value: resp.Workspace.Workspace,
Name: "Workspace", Value: workspaceName,
},
{
Name: "Project", Value: resp.Application.Project,
Name: "Project", Value: appProjectName,
},
{
Name: "Application", Value: resp.Application.Application,
Name: "Application", Value: appName,
},
}, terminal.WithInfoStyle())

Expand Down
12 changes: 9 additions & 3 deletions internal/cli/job_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,26 @@ func (c *JobListCommand) Run(args []string) int {
pipeline = "name: " + j.Pipeline.PipelineName + ", run: " + strconv.FormatUint(j.Pipeline.RunSequence, 10) + ", step: " + j.Pipeline.Step
}

appProject := j.GetApplication().GetProject()
if appProject == "" {
appProject = "deleted"
}
appWorkspace := j.GetWorkspace().GetWorkspace()

tblColumn := []string{
j.Id,
op,
jobState,
queueTime,
completeTime,
targetRunner,
j.Application.Project,
j.Workspace.Workspace,
appProject,
appWorkspace,
}

if c.flagVerbose {
tblColumn = append(tblColumn, []string{
j.Application.Application,
appProject,
pipeline,
}...)
}
Expand Down
20 changes: 16 additions & 4 deletions internal/cli/task_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func (c *TaskInspectCommand) Run(args []string) int {
return 1
}

var workspace, project, application string
var workspace string
project := "deleted"
application := "deleted"
if taskResp.TaskJob.Workspace != nil {
workspace = taskResp.TaskJob.Workspace.Workspace
}
Expand Down Expand Up @@ -259,6 +261,16 @@ func (c *TaskInspectCommand) FormatJob(job *pb.Job) error {
errMsg = job.Error.Message
}

var workspace string
project := "deleted"
application := "deleted"
if job.Workspace != nil {
workspace = job.Workspace.Workspace
}
if job.Application != nil {
project = job.Application.Project
application = job.Application.Application
}
c.ui.Output("Job Configuration:", terminal.WithInfoStyle())
c.ui.NamedValues([]terminal.NamedValue{
{
Expand All @@ -274,13 +286,13 @@ func (c *TaskInspectCommand) FormatJob(job *pb.Job) error {
Name: "Target Runner", Value: targetRunner,
},
{
Name: "Workspace", Value: job.Workspace.Workspace,
Name: "Workspace", Value: workspace,
},
{
Name: "Project", Value: job.Application.Project,
Name: "Project", Value: project,
},
{
Name: "Application", Value: job.Application.Application,
Name: "Application", Value: application,
},
}, terminal.WithInfoStyle())

Expand Down

0 comments on commit 7958a15

Please sign in to comment.