From fb2bb7fb08d534f53cc3c5c3b558303f8ad77d24 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Wed, 23 Aug 2023 08:20:55 +0000 Subject: [PATCH] Fix nil pointer dereference Signed-off-by: Gabriel Adrian Samfira --- cmd/garm-cli/cmd/pool.go | 16 ++++++---------- cmd/garm-cli/cmd/runner.go | 18 +++++++----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/cmd/garm-cli/cmd/pool.go b/cmd/garm-cli/cmd/pool.go index 024a58b7..3db8f7d8 100644 --- a/cmd/garm-cli/cmd/pool.go +++ b/cmd/garm-cli/cmd/pool.go @@ -55,6 +55,10 @@ var ( poolGitHubRunnerGroup string ) +type poolPayloadGetter interface { + GetPayload() params.Pools +} + // runnerCmd represents the runner command var poolCmd = &cobra.Command{ Use: "pool", @@ -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) @@ -135,7 +131,7 @@ Example: if err != nil { return err } - formatPools(pools) + formatPools(response.GetPayload()) return nil }, } diff --git a/cmd/garm-cli/cmd/runner.go b/cmd/garm-cli/cmd/runner.go index b4cfbcfe..0d7971bd 100644 --- a/cmd/garm-cli/cmd/runner.go +++ b/cmd/garm-cli/cmd/runner.go @@ -47,6 +47,10 @@ list all instances.`, Run: nil, } +type instancesPayloadGetter interface { + GetPayload() params.Instances +} + var runnerListCmd = &cobra.Command{ Use: "list", Aliases: []string{"ls"}, @@ -80,7 +84,7 @@ Example: return errNeedsInitError } - var instances []params.Instance + var response instancesPayloadGetter var err error switch len(args) { @@ -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) @@ -133,6 +127,8 @@ Example: if err != nil { return err } + + instances := response.GetPayload() formatInstances(instances) return nil },