Skip to content

Commit db8cf3f

Browse files
committed
panic() is not the best way to show error in entrypoints
1 parent 57c4dde commit db8cf3f

4 files changed

Lines changed: 28 additions & 40 deletions

File tree

cmd/ubackup/encryption.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ func decryptEntry() *cobra.Command {
7575
Short: "Decrypts an encrypted backup file (from stdin) with your private key",
7676
Args: cobra.ExactArgs(1),
7777
Run: func(cmd *cobra.Command, args []string) {
78-
if err := decryptAndDecompress(args[0], os.Stdin, os.Stdout); err != nil {
79-
panic(err)
80-
}
78+
exitIfError(decryptAndDecompress(args[0], os.Stdin, os.Stdout))
8179
},
8280
}
8381
}
@@ -88,9 +86,7 @@ func decryptionKeyGenerateEntry() *cobra.Command {
8886
Short: "Generate RSA private key for backup decryption",
8987
Args: cobra.NoArgs,
9088
Run: func(cmd *cobra.Command, args []string) {
91-
if err := decryptionKeyGenerate(os.Stdout); err != nil {
92-
panic(err)
93-
}
89+
exitIfError(decryptionKeyGenerate(os.Stdout))
9490
},
9591
}
9692
}
@@ -101,9 +97,7 @@ func decryptionKeyToEncryptionKeyEntry() *cobra.Command {
10197
Short: "Prints encryption key (= public key) of decryption key (= private key)",
10298
Args: cobra.NoArgs,
10399
Run: func(cmd *cobra.Command, args []string) {
104-
if err := decryptionKeyToEncryptionKey(os.Stdin, os.Stdout); err != nil {
105-
panic(err)
106-
}
100+
exitIfError(decryptionKeyToEncryptionKey(os.Stdin, os.Stdout))
107101
},
108102
}
109103
}

cmd/ubackup/main.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ func main() {
3030
Run: func(cmd *cobra.Command, args []string) {
3131
rootLogger := logex.StandardLogger()
3232

33-
if err := runBackup(
33+
exitIfError(runBackup(
3434
ossignal.InterruptOrTerminateBackgroundCtx(logex.Prefix("main", rootLogger)),
3535
rootLogger,
36-
); err != nil {
37-
panic(err)
38-
}
36+
))
3937
},
4038
})
4139

@@ -47,10 +45,7 @@ func main() {
4745
app.AddCommand(decryptionKeyGenerateEntry())
4846
app.AddCommand(decryptionKeyToEncryptionKeyEntry())
4947

50-
if err := app.Execute(); err != nil {
51-
fmt.Fprintln(os.Stderr, err)
52-
os.Exit(1)
53-
}
48+
exitIfError(app.Execute())
5449
}
5550

5651
func manualEntry() *cobra.Command {
@@ -84,11 +79,12 @@ func manualEntry() *cobra.Command {
8479
Run: func(cmd *cobra.Command, args []string) {
8580
rootLogger := logex.StandardLogger()
8681

87-
ctx := ossignal.InterruptOrTerminateBackgroundCtx(logex.Prefix("main", rootLogger))
88-
89-
if err := manual(ctx, args[0], args[1], os.Stdin, rootLogger); err != nil {
90-
panic(err)
91-
}
82+
exitIfError(manual(
83+
ossignal.InterruptOrTerminateBackgroundCtx(rootLogger),
84+
args[0],
85+
args[1],
86+
os.Stdin,
87+
rootLogger))
9288
},
9389
}
9490
}
@@ -112,9 +108,7 @@ func configValidateEntry() *cobra.Command {
112108
Short: "Validates your config file (from stdin)",
113109
Args: cobra.NoArgs,
114110
Run: func(cmd *cobra.Command, args []string) {
115-
if err := jsonfile.Unmarshal(os.Stdin, &ubconfig.Config{}, true); err != nil {
116-
panic(err)
117-
}
111+
exitIfError(jsonfile.Unmarshal(os.Stdin, &ubconfig.Config{}, true))
118112
},
119113
}
120114
}
@@ -128,9 +122,7 @@ func configExampleEntry() *cobra.Command {
128122
Short: "Shows you an example config file",
129123
Args: cobra.NoArgs,
130124
Run: func(cmd *cobra.Command, args []string) {
131-
if err := jsonfile.Marshal(os.Stdout, ubconfig.DefaultConfig(pubkeyFilePath, kitchenSink)); err != nil {
132-
panic(err)
133-
}
125+
exitIfError(jsonfile.Marshal(os.Stdout, ubconfig.DefaultConfig(pubkeyFilePath, kitchenSink)))
134126
},
135127
}
136128

@@ -139,3 +131,9 @@ func configExampleEntry() *cobra.Command {
139131

140132
return cmd
141133
}
134+
135+
func exitIfError(err error) {
136+
if err != nil {
137+
fmt.Fprintln(os.Stderr, err)
138+
}
139+
}

cmd/ubackup/scheduler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ func schedulerEntry() *cobra.Command {
7979
return nil
8080
}
8181

82-
if err := runScheduler(ctx, backupTime, logex.Prefix("scheduler", rootLogger)); err != nil {
83-
panic(err)
84-
}
82+
exitIfError(runScheduler(
83+
ctx,
84+
backupTime,
85+
logex.Prefix("scheduler", rootLogger)))
8586
},
8687
})
8788

@@ -95,9 +96,8 @@ func schedulerEntry() *cobra.Command {
9596
"µbackup",
9697
systemdinstaller.Args("scheduler", "run"),
9798
systemdinstaller.Docs("https://function61.com/"))
98-
if err := systemdinstaller.Install(service); err != nil {
99-
panic(err)
100-
}
99+
100+
exitIfError(systemdinstaller.Install(service))
101101

102102
fmt.Println(systemdinstaller.GetHints(service))
103103
},

cmd/ubackup/storage.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ func storageEntry() *cobra.Command {
6565
Short: "List backups from storage for a service",
6666
Args: cobra.ExactArgs(1),
6767
Run: func(cmd *cobra.Command, args []string) {
68-
if err := ls(args[0]); err != nil {
69-
panic(err)
70-
}
68+
exitIfError(ls(args[0]))
7169
},
7270
})
7371

@@ -76,9 +74,7 @@ func storageEntry() *cobra.Command {
7674
Short: "Get backup from storage",
7775
Args: cobra.ExactArgs(1),
7876
Run: func(cmd *cobra.Command, args []string) {
79-
if err := get(args[0]); err != nil {
80-
panic(err)
81-
}
77+
exitIfError(get(args[0]))
8278
},
8379
})
8480

0 commit comments

Comments
 (0)