Skip to content

Commit addfcbe

Browse files
fix: handle config parse error
1 parent a77ba96 commit addfcbe

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

cmd/semantic-release/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,17 @@ func main() {
4848
}
4949

5050
func cliHandler(c *cli.Context) error {
51-
conf := config.NewConfig(c)
5251

5352
logger := log.New(os.Stderr, "[semantic-release]: ", 0)
5453
exitIfError := errorHandler(logger)
5554

55+
conf, err := config.NewConfig(c)
56+
exitIfError(err)
57+
5658
ci := condition.NewCI()
5759
logger.Printf("detected CI: %s\n", ci.Name())
5860

59-
var (
60-
repo semrel.Repository
61-
err error
62-
)
61+
var repo semrel.Repository
6362

6463
if conf.GitLab {
6564
repo, err = semrel.NewGitLabRepository(c.Context, conf.GitLabBaseURL, conf.Slug, conf.Token, ci.GetCurrentBranch(), conf.GitLabProjectID)

pkg/config/config.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67

78
"github.com/urfave/cli/v2"
@@ -21,7 +22,7 @@ type (
2122
GheHost string
2223
Prerelease bool
2324
TravisCom bool
24-
BetaRelease BetaRelease
25+
BetaRelease *BetaRelease
2526
Match string
2627
AllowInitialDevelopmentVersions bool
2728
GitLab bool
@@ -35,7 +36,7 @@ type (
3536
)
3637

3738
// NewConfig returns a new Config instance
38-
func NewConfig(c *cli.Context) *Config {
39+
func NewConfig(c *cli.Context) (*Config, error) {
3940
conf := &Config{
4041
Token: c.String("token"),
4142
Slug: c.String("slug"),
@@ -53,20 +54,18 @@ func NewConfig(c *cli.Context) *Config {
5354
GitLab: c.Bool("gitlab"),
5455
GitLabBaseURL: c.String("gitlab-base-url"),
5556
GitLabProjectID: c.String("gitlab-project-id"),
57+
BetaRelease: &BetaRelease{},
5658
}
5759

5860
f, err := os.OpenFile(".semrelrc", os.O_RDONLY, 0)
5961
if err != nil {
60-
return conf
62+
return conf, nil
6163
}
6264
defer f.Close()
6365

64-
src := &BetaRelease{}
65-
if err := json.NewDecoder(f).Decode(src); err != nil {
66-
return conf
66+
if err := json.NewDecoder(f).Decode(conf.BetaRelease); err != nil {
67+
return nil, fmt.Errorf("could not parse .semrelrc: %w", err)
6768
}
6869

69-
conf.BetaRelease = *src
70-
71-
return conf
70+
return conf, nil
7271
}

0 commit comments

Comments
 (0)