From 55b628a7f137841a345bce57c829f39ef265fbea Mon Sep 17 00:00:00 2001 From: Caleb Albers Date: Fri, 27 Jan 2023 11:43:19 -0800 Subject: [PATCH] Fix a panic when repo has no license on init cmd Fixes #9 Previously, the `copywrite init` command retrieved a repo's license info in such a way that it accessed an invalid memory address when a repo had no current license. Additional checks now fix this condition, as well as fix another previously unhandled error in the same section of code. --- cmd/init.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index daa6403..1d125a2 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -71,16 +71,19 @@ for any unknown values. If you are running this command in CI, please use the // Try to autodiscover license and year if repo, err := github.DiscoverRepo(); err == nil { client := github.NewGHClient().Raw() - data, _, _ := client.Repositories.Get(context.Background(), repo.Owner, repo.Name) - - // fall back to GitHub repo creation year if --year wasn't set - if !cmd.Flags().Changed("year") { - newConfig.Project.CopyrightYear = data.CreatedAt.Year() - } + data, _, err := client.Repositories.Get(context.Background(), repo.Owner, repo.Name) + if err == nil { + cobra.CheckErr(err) + // fall back to GitHub repo creation year if --year wasn't set + if !cmd.Flags().Changed("year") { + newConfig.Project.CopyrightYear = data.CreatedAt.Year() + } - // fall back to GitHub's reported SPDX identifier if --spdx wasn't set - if !cmd.Flags().Changed("spdx") { - newConfig.Project.License = *data.GetLicense().SPDXID + // fall back to GitHub's reported SPDX identifier if --spdx wasn't set + if !cmd.Flags().Changed("spdx") { + license := data.GetLicense() + newConfig.Project.License = license.GetSPDXID() + } } }