Skip to content

Commit

Permalink
fix #4: improve shutdown function
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Apr 23, 2021
1 parent 9e522f2 commit 2734949
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/golang/mock v1.5.0
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
go.octolab.org v0.11.5
go.octolab.org v0.12.0
go.octolab.org/toolkit/cli v0.6.2
go.octolab.org/toolkit/config v0.0.4
)
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/brianvoe/gofakeit/v6 v6.0.4/go.mod h1:palrJUk4Fyw38zIFB/uBZqsgzW5VsNllhHKKwAebzew=
github.com/brianvoe/gofakeit/v6 v6.4.1/go.mod h1:palrJUk4Fyw38zIFB/uBZqsgzW5VsNllhHKKwAebzew=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down Expand Up @@ -194,8 +194,8 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.octolab.org v0.9.0/go.mod h1:nGTsvNE5SzxSriL/1u0xl5B+ttjIn9RzIbAhzaKE+Sk=
go.octolab.org v0.11.5 h1:zN+1dOTFpCmyJq5y0JhLy2bLu3TFEbILavjBx2wuI6A=
go.octolab.org v0.11.5/go.mod h1:Hl97oqUCvkbdBpFhsU3zUOl3WSkdRUqYab9zwbHCLJY=
go.octolab.org v0.12.0 h1:NZrvpJvcREBheb3m0ZXvlQbDSHyA6gA352iT37k2+20=
go.octolab.org v0.12.0/go.mod h1:TEYR7HOfImJq8AsoCcl9JBK+80t4QBY4EilGxMtscNE=
go.octolab.org/toolkit/cli v0.6.2 h1:oRUvE1T1wAuglqcggKg/F8djTTsRvTwr2eFeJvL6ExE=
go.octolab.org/toolkit/cli v0.6.2/go.mod h1:iEL6yN6YT3/Sbi19Tl38mn4agRr2yUyiNvbN29iqb/U=
go.octolab.org/toolkit/config v0.0.4 h1:Q/MnSuRf4ttHCuAaq7PqpD8xQhEtmfK8mO9NX1IxvlA=
Expand Down
19 changes: 19 additions & 0 deletions internal/cmd/demo/panic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package demo

import "github.com/spf13/cobra"

// Panic returns a demo cobra.Command to raise a panic.
//
// $ go run main.go panic
//
func Panic() *cobra.Command {
command := cobra.Command{
Use: "panic",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
panic("unexpected panic")
},
}

return &command
}
12 changes: 10 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cmd

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"

"tool/internal/cmd/demo"
)

// New returns the new root command.
func New() *cobra.Command {
Expand All @@ -14,7 +18,11 @@ func New() *cobra.Command {
SilenceErrors: false,
SilenceUsage: true,
}

/* configure instance */
command.AddCommand( /* related commands */ )
command.AddCommand(
demo.Panic(),
)

return &command
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func main() {
}

func shutdown(err error) {
if recovered, is := errors.Unwrap(err).(errors.Recovered); is {
var recovered errors.Recovered
if errors.As(err, &recovered) {
unsafe.DoSilent(fmt.Fprintf(stderr, "recovered: %+v\n", recovered.Cause()))
unsafe.DoSilent(fmt.Fprintln(stderr, "---"))
unsafe.DoSilent(fmt.Fprintf(stderr, "%+v\n", err))
Expand Down

0 comments on commit 2734949

Please sign in to comment.