Skip to content

Commit

Permalink
Fix nil pointer dereference
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed Aug 23, 2023
1 parent bc0a34b commit fb2bb7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
16 changes: 6 additions & 10 deletions cmd/garm-cli/cmd/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ var (
poolGitHubRunnerGroup string
)

type poolPayloadGetter interface {
GetPayload() params.Pools
}

// runnerCmd represents the runner command
var poolCmd = &cobra.Command{
Use: "pool",
Expand Down Expand Up @@ -95,34 +99,26 @@ Example:
return errNeedsInitError
}

var pools []params.Pool
var response poolPayloadGetter
var err error

switch len(args) {
case 0:
if cmd.Flags().Changed("repo") {
var response *apiClientRepos.ListRepoPoolsOK
listRepoPoolsReq := apiClientRepos.NewListRepoPoolsParams()
listRepoPoolsReq.RepoID = poolRepository
response, err = apiCli.Repositories.ListRepoPools(listRepoPoolsReq, authToken)
pools = response.Payload
} else if cmd.Flags().Changed("org") {
var response *apiClientOrgs.ListOrgPoolsOK
listOrgPoolsReq := apiClientOrgs.NewListOrgPoolsParams()
listOrgPoolsReq.OrgID = poolOrganization
response, err = apiCli.Organizations.ListOrgPools(listOrgPoolsReq, authToken)
pools = response.Payload
} else if cmd.Flags().Changed("enterprise") {
var response *apiClientEnterprises.ListEnterprisePoolsOK
listEnterprisePoolsReq := apiClientEnterprises.NewListEnterprisePoolsParams()
listEnterprisePoolsReq.EnterpriseID = poolEnterprise
response, err = apiCli.Enterprises.ListEnterprisePools(listEnterprisePoolsReq, authToken)
pools = response.Payload
} else if cmd.Flags().Changed("all") {
var response *apiClientPools.ListPoolsOK
listPoolsReq := apiClientPools.NewListPoolsParams()
response, err = apiCli.Pools.ListPools(listPoolsReq, authToken)
pools = response.Payload
} else {
cmd.Help() //nolint
os.Exit(0)
Expand All @@ -135,7 +131,7 @@ Example:
if err != nil {
return err
}
formatPools(pools)
formatPools(response.GetPayload())
return nil
},
}
Expand Down
18 changes: 7 additions & 11 deletions cmd/garm-cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ list all instances.`,
Run: nil,
}

type instancesPayloadGetter interface {
GetPayload() params.Instances
}

var runnerListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Expand Down Expand Up @@ -80,7 +84,7 @@ Example:
return errNeedsInitError
}

var instances []params.Instance
var response instancesPayloadGetter
var err error

switch len(args) {
Expand All @@ -92,35 +96,25 @@ Example:

return fmt.Errorf("specifying a pool ID and any of [all org repo enterprise] are mutually exclusive")
}
var response *apiClientInstances.ListPoolInstancesOK
listPoolInstancesReq := apiClientInstances.NewListPoolInstancesParams()
listPoolInstancesReq.PoolID = args[0]
response, err = apiCli.Instances.ListPoolInstances(listPoolInstancesReq, authToken)
instances = response.Payload
case 0:
if cmd.Flags().Changed("repo") {
var response *apiClientRepos.ListRepoInstancesOK
listRepoInstancesReq := apiClientRepos.NewListRepoInstancesParams()
listRepoInstancesReq.RepoID = runnerRepository
response, err = apiCli.Repositories.ListRepoInstances(listRepoInstancesReq, authToken)
instances = response.Payload
} else if cmd.Flags().Changed("org") {
var response *apiClientOrgs.ListOrgInstancesOK
listOrgInstancesReq := apiClientOrgs.NewListOrgInstancesParams()
listOrgInstancesReq.OrgID = runnerOrganization
response, err = apiCli.Organizations.ListOrgInstances(listOrgInstancesReq, authToken)
instances = response.Payload
} else if cmd.Flags().Changed("enterprise") {
var response *apiClientEnterprises.ListEnterpriseInstancesOK
listEnterpriseInstancesReq := apiClientEnterprises.NewListEnterpriseInstancesParams()
listEnterpriseInstancesReq.EnterpriseID = runnerEnterprise
response, err = apiCli.Enterprises.ListEnterpriseInstances(listEnterpriseInstancesReq, authToken)
instances = response.Payload
} else if cmd.Flags().Changed("all") {
var response *apiClientInstances.ListInstancesOK
listInstancesReq := apiClientInstances.NewListInstancesParams()
response, err = apiCli.Instances.ListInstances(listInstancesReq, authToken)
instances = response.Payload
} else {
cmd.Help() //nolint
os.Exit(0)
Expand All @@ -133,6 +127,8 @@ Example:
if err != nil {
return err
}

instances := response.GetPayload()
formatInstances(instances)
return nil
},
Expand Down

0 comments on commit fb2bb7f

Please sign in to comment.