Skip to content

Commit f8204d5

Browse files
committed
Moved away from panics to instead return errors using RunE
Signed-off-by: Raghav Roy <[email protected]>
1 parent 78c7c69 commit f8204d5

File tree

7 files changed

+46
-35
lines changed

7 files changed

+46
-35
lines changed

cmd/audit.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cmd
1818

1919
import (
2020
"fmt"
21-
"github.com/pkg/errors"
2221
"io/ioutil"
2322
"net/http"
2423
"os"
@@ -30,6 +29,8 @@ import (
3029
"sync"
3130
"time"
3231

32+
"github.com/pkg/errors"
33+
3334
"github.com/spf13/cobra"
3435
"k8s.io/apimachinery/pkg/util/sets"
3536

@@ -59,11 +60,11 @@ var auditCmd = &cobra.Command{
5960
Args: cobra.MinimumNArgs(1),
6061
Short: "ensure OWNERS, OWNERS_ALIASES and sigs.yaml have the correct data structure",
6162
Long: ``,
62-
Run: func(cmd *cobra.Command, args []string) {
63+
RunE: func(cmd *cobra.Command, args []string) error {
6364
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
6465
pwd, err := os.Getwd()
6566
if err != nil {
66-
panic(err)
67+
return err
6768
}
6869

6970
if _, err := os.Stat(kubernetesDirectory); errors.Is(err, os.ErrNotExist) {
@@ -74,18 +75,19 @@ var auditCmd = &cobra.Command{
7475

7576
sigsYamlPath, err := utils.GetSigsYamlFile(pwd)
7677
if err != nil {
77-
panic(fmt.Errorf("ERROR: unable to find sigs.yaml file: %w", err))
78+
return err
7879
}
7980
context, err := utils.GetSigsYaml(sigsYamlPath)
8081
if err != nil {
81-
panic(fmt.Errorf("ERROR: parsing file: %s - %w", sigsYamlPath, err))
82+
return err
8283
}
8384

8485
if auditSpecifiedGroups(pwd, context, args) {
8586
auditGithubIDs(context)
8687
auditLocalOwnersFiles(context, args)
8788
}
8889
fmt.Printf("Done.\n")
90+
return nil
8991
},
9092
}
9193

cmd/checkurl.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,24 @@ var checkURLsCmd = &cobra.Command{
4040
Use: "check-urls",
4141
Short: "ensure all the urls in yaml file are still valid",
4242
Long: ``,
43-
Run: func(cmd *cobra.Command, args []string) {
43+
RunE: func(cmd *cobra.Command, args []string) error {
4444
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
4545
fmt.Printf("Processing %s\n", yamlFile)
4646
sourceYaml, err := ioutil.ReadFile(yamlFile)
4747
if err != nil {
48-
panic(err)
48+
return err
4949
}
5050
rootNode := yaml.Node{}
5151
err = yaml.Unmarshal(sourceYaml, &rootNode)
5252
if err != nil {
53-
panic(err)
53+
return err
5454
}
5555
ok := processNode(&rootNode)
5656
fmt.Println("done")
5757
if !ok {
5858
os.Exit(1)
5959
}
60+
return nil
6061
},
6162
}
6263

cmd/export.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package cmd
1818

1919
import (
2020
"fmt"
21-
"github.com/spf13/cobra"
2221
"os"
2322
"sort"
2423
"strings"
2524
"time"
2625

26+
"github.com/spf13/cobra"
27+
2728
"k8s.io/apimachinery/pkg/util/sets"
2829

2930
"github.com/dims/maintainers/pkg/utils"
@@ -34,16 +35,17 @@ var exportCmd = &cobra.Command{
3435
Use: "export",
3536
Short: "export contents of OWNERS and OWNERS_ALIASES as parsable csv file",
3637
Long: ``,
37-
Run: func(cmd *cobra.Command, args []string) {
38+
RunE: func(cmd *cobra.Command, args []string) error {
3839
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
3940
pwd, err := os.Getwd()
4041
if err != nil {
41-
panic(err)
42+
return err
4243
}
4344
err = exportOwnersAndAliases(pwd)
4445
if err != nil {
45-
panic(err)
46+
return err
4647
}
48+
return nil
4749
},
4850
}
4951

cmd/labels.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package cmd
1818

1919
import (
2020
"fmt"
21-
"github.com/spf13/cobra"
2221
"os"
2322
"sort"
2423
"time"
2524

25+
"github.com/spf13/cobra"
26+
2627
"k8s.io/apimachinery/pkg/util/sets"
2728

2829
"github.com/dims/maintainers/pkg/utils"
@@ -33,16 +34,17 @@ var labelsCmd = &cobra.Command{
3334
Use: "labels",
3435
Short: "print a list of OWNERS files for labels",
3536
Long: ``,
36-
Run: func(cmd *cobra.Command, args []string) {
37+
RunE: func(cmd *cobra.Command, args []string) error {
3738
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
3839
pwd, err := os.Getwd()
3940
if err != nil {
40-
panic(err)
41+
return err
4142
}
4243
err = printFilesForLabels(pwd)
4344
if err != nil {
44-
panic(err)
45+
return err
4546
}
47+
return nil
4648
},
4749
}
4850

cmd/prettify.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ var prettifyCmd = &cobra.Command{
4242
Use: "prettify",
4343
Short: "ensure all OWNERS related files are valid yaml and look the same",
4444
Long: ``,
45-
Run: func(cmd *cobra.Command, args []string) {
45+
RunE: func(cmd *cobra.Command, args []string) error {
4646
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
4747
pwd, err := os.Getwd()
4848
if err != nil {
49-
panic(err)
49+
return err
5050
}
5151

5252
files, err := utils.GetOwnerFiles(pwd)
5353
if err != nil {
54-
panic(err)
54+
return err
5555
}
5656

5757
aliasPath, err := utils.GetOwnersAliasesFile(pwd)
@@ -69,21 +69,22 @@ var prettifyCmd = &cobra.Command{
6969
for _, path := range files {
7070
sourceYaml, err := ioutil.ReadFile(path)
7171
if err != nil {
72-
panic(err)
72+
return err
7373
}
7474
rootNode, err := fetchYaml(sourceYaml)
7575
if err != nil {
76-
panic(err)
76+
return err
7777
}
7878
writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
7979
if err != nil {
80-
panic(err)
80+
return err
8181
}
8282
err = streamYaml(writer, indent, rootNode)
8383
if err != nil {
84-
panic(err)
84+
return err
8585
}
8686
}
87+
return nil
8788
},
8889
}
8990

cmd/prune.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ package cmd
1818

1919
import (
2020
"fmt"
21-
"github.com/spf13/cobra"
2221
"os"
2322
"path/filepath"
2423
"sort"
2524
"strings"
2625
"time"
2726

27+
"github.com/spf13/cobra"
28+
2829
"k8s.io/apimachinery/pkg/util/sets"
2930

3031
"github.com/dims/maintainers/pkg/utils"
@@ -54,16 +55,16 @@ var pruneCmd = &cobra.Command{
5455
Use: "prune",
5556
Short: "Remove stale github ids from OWNERS and OWNERS_ALIASES",
5657
Long: ``,
57-
Run: func(cmd *cobra.Command, args []string) {
58+
RunE: func(cmd *cobra.Command, args []string) error {
5859
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
5960
pwd, err := os.Getwd()
6061
if err != nil {
61-
panic(err)
62+
return err
6263
}
6364

6465
userIDs, repoAliases, files, err := getOwnersAndAliases(pwd)
6566
if err != nil {
66-
panic(err)
67+
return err
6768
}
6869
for _, file := range files {
6970
fmt.Printf("Processed %s\n", file)
@@ -77,7 +78,7 @@ var pruneCmd = &cobra.Command{
7778
if !skipDS {
7879
err, contribs := utils.GetContributionsForAYear(repositoryDS, periodDS)
7980
if err != nil {
80-
panic(err)
81+
return err
8182
}
8283
if contribs == nil || len(contribs) == 0 {
8384
panic("unable to find any contributions in repository : " + repositoryDS)
@@ -139,11 +140,12 @@ var pruneCmd = &cobra.Command{
139140
if !dryRun {
140141
err = fixupOwnersFiles(files, missingIDs, lowPRComments)
141142
if err != nil {
142-
panic(err)
143+
return err
143144
}
144145
} else {
145146
fmt.Printf("--dryrun is set to true, will skip updating OWNERS and OWNER_ALIASES")
146147
}
148+
return nil
147149
},
148150
}
149151

cmd/validate.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ var validateCmd = &cobra.Command{
3232
Use: "validate",
3333
Short: "ensure OWNERS, OWNERS_ALIASES and sigs.yaml have the correct data structure",
3434
Long: ``,
35-
Run: func(cmd *cobra.Command, args []string) {
35+
RunE: func(cmd *cobra.Command, args []string) error {
3636
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
3737
pwd, err := os.Getwd()
3838
if err != nil {
39-
panic(err)
39+
return err
4040
}
4141

4242
aliasPath, err := utils.GetOwnersAliasesFile(pwd)
4343
if err == nil && len(aliasPath) > 0 {
4444
_, err := utils.GetOwnerAliases(aliasPath)
4545
if err != nil {
46-
panic(fmt.Errorf("error parsing file: %s - %w", aliasPath, err))
46+
return err
4747
}
4848
}
4949

@@ -52,19 +52,19 @@ var validateCmd = &cobra.Command{
5252
if err == nil && len(sigsYamlPath) > 0 {
5353
context, err = utils.GetSigsYaml(sigsYamlPath)
5454
if err != nil {
55-
panic(fmt.Errorf("error parsing file: %s - %w", sigsYamlPath, err))
55+
return err
5656
}
5757
}
5858

5959
files, err := utils.GetOwnerFiles(pwd)
6060
if err != nil {
61-
panic(err)
61+
return err
6262
}
6363

6464
for _, path := range files {
6565
_, err := utils.GetOwnersInfo(path)
6666
if err != nil {
67-
panic(fmt.Errorf("error parsing file: %s - %w", path, err))
67+
return err
6868
}
6969
}
7070

@@ -79,6 +79,7 @@ var validateCmd = &cobra.Command{
7979
}
8080
//panic("please see errors above")
8181
}
82+
return nil
8283
},
8384
}
8485

0 commit comments

Comments
 (0)