Skip to content

Commit 63773f8

Browse files
Merge pull request #13 from RaghavRoy145/return-errors
Return errors using RunE
2 parents 023dd3e + f8204d5 commit 63773f8

File tree

7 files changed

+40
-32
lines changed

7 files changed

+40
-32
lines changed

cmd/audit.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ var auditCmd = &cobra.Command{
6060
Args: cobra.MinimumNArgs(1),
6161
Short: "ensure OWNERS, OWNERS_ALIASES and sigs.yaml have the correct data structure",
6262
Long: ``,
63-
Run: func(cmd *cobra.Command, args []string) {
63+
RunE: func(cmd *cobra.Command, args []string) error {
6464
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
6565
pwd, err := os.Getwd()
6666
if err != nil {
67-
panic(err)
67+
return err
6868
}
6969

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

7676
sigsYamlPath, err := utils.GetSigsYamlFile(pwd)
7777
if err != nil {
78-
panic(fmt.Errorf("ERROR: unable to find sigs.yaml file: %w", err))
78+
return err
7979
}
8080
context, err := utils.GetSigsYaml(sigsYamlPath)
8181
if err != nil {
82-
panic(fmt.Errorf("ERROR: parsing file: %s - %w", sigsYamlPath, err))
82+
return err
8383
}
8484

8585
if auditSpecifiedGroups(pwd, context, args) {
8686
auditGithubIDs(context)
8787
auditLocalOwnersFiles(context, args)
8888
}
8989
fmt.Printf("Done.\n")
90+
return nil
9091
},
9192
}
9293

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ var exportCmd = &cobra.Command{
3535
Use: "export",
3636
Short: "export contents of OWNERS and OWNERS_ALIASES as parsable csv file",
3737
Long: ``,
38-
Run: func(cmd *cobra.Command, args []string) {
38+
RunE: func(cmd *cobra.Command, args []string) error {
3939
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
4040
pwd, err := os.Getwd()
4141
if err != nil {
42-
panic(err)
42+
return err
4343
}
4444
err = exportOwnersAndAliases(pwd)
4545
if err != nil {
46-
panic(err)
46+
return err
4747
}
48+
return nil
4849
},
4950
}
5051

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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ var pruneCmd = &cobra.Command{
5555
Use: "prune",
5656
Short: "Remove stale github ids from OWNERS and OWNERS_ALIASES",
5757
Long: ``,
58-
Run: func(cmd *cobra.Command, args []string) {
58+
RunE: func(cmd *cobra.Command, args []string) error {
5959
fmt.Printf("Running script : %s\n", time.Now().Format("01-02-2006 15:04:05"))
6060
pwd, err := os.Getwd()
6161
if err != nil {
62-
panic(err)
62+
return err
6363
}
6464

6565
userIDs, repoAliases, files, err := getOwnersAndAliases(pwd)
6666
if err != nil {
67-
panic(err)
67+
return err
6868
}
6969
for _, file := range files {
7070
fmt.Printf("Processed %s\n", file)
@@ -78,7 +78,7 @@ var pruneCmd = &cobra.Command{
7878
if !skipDS {
7979
contribs, err := utils.GetContributionsForAYear(repositoryDS, periodDS)
8080
if err != nil {
81-
panic(err)
81+
return err
8282
}
8383
if len(contribs) == 0 {
8484
panic("unable to find any contributions in repository : " + repositoryDS)
@@ -154,11 +154,12 @@ var pruneCmd = &cobra.Command{
154154
if !dryRun {
155155
err = fixupOwnersFiles(files, missingIDs, lowPRComments)
156156
if err != nil {
157-
panic(err)
157+
return err
158158
}
159159
} else {
160160
fmt.Printf("--dryrun is set to true, will skip updating OWNERS and OWNER_ALIASES")
161161
}
162+
return nil
162163
},
163164
}
164165

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)