diff --git a/.golangci.yml b/.golangci.yml index d9c8fe01..397a18b4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,6 +35,10 @@ issues: exclude-dirs: - example exclude-rules: + - path: cmd/* + linters: + - gochecknoglobals + - gochecknoinits - path: (.+)_test.go linters: - dupl \ No newline at end of file diff --git a/cmd/check.go b/cmd/check.go index ea3b95e2..4acddc3b 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -1,4 +1,3 @@ -//nolint:gochecknoglobals package cmd import ( @@ -53,7 +52,6 @@ var checkCmd = &cobra.Command{ }, } -//nolint:gochecknoinits func init() { rootCmd.AddCommand(checkCmd) diff --git a/cmd/rest.go b/cmd/rest.go new file mode 100644 index 00000000..58f156c2 --- /dev/null +++ b/cmd/rest.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "context" + "errors" + "net/http" + + "github.com/rs/zerolog" + "github.com/spf13/cobra" + + "github.com/bavix/gripmock/internal/deps" +) + +// restCmd represents the rest command. +var restCmd = &cobra.Command{ + Use: "rest", + Short: "Start only the rest service", + RunE: func(cmd *cobra.Command, _ []string) error { + builder := deps.NewBuilder(deps.WithDefaultConfig()) + ctx, cancel := builder.SignalNotify(cmd.Context()) + defer cancel() + + ctx = builder.Logger(ctx) + + return restServe(ctx, builder) + }, +} + +func restServe(ctx context.Context, builder *deps.Builder) error { + srv, err := builder.RestServe(ctx, stubFlag) + if err != nil { + return err + } + + ch := make(chan error) + defer close(ch) + + go func() { + zerolog.Ctx(ctx). + Info(). + Str("addr", builder.Config().HTTPAddr). + Msg("Serving stub-manager") + + ch <- srv.ListenAndServe() + }() + + select { + case err = <-ch: + if errors.Is(err, http.ErrServerClosed) { + return nil + } + + return err + case <-ctx.Done(): + return ctx.Err() + } +} + +func init() { + rootCmd.AddCommand(restCmd) +} diff --git a/cmd/root.go b/cmd/root.go index 787de69c..ba0dc613 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,10 +1,7 @@ -//nolint:gochecknoglobals package cmd import ( "context" - "errors" - "net/http" "os" "github.com/rs/zerolog" @@ -45,37 +42,6 @@ var rootCmd = &cobra.Command{ }, } -func restServe(ctx context.Context, builder *deps.Builder) error { - srv, err := builder.RestServe(ctx, stubFlag) - if err != nil { - return err - } - - ch := make(chan error) - defer close(ch) - - go func() { - zerolog.Ctx(ctx). - Info(). - Str("addr", builder.Config().HTTPAddr). - Msg("Serving stub-manager") - - ch <- srv.ListenAndServe() - }() - - select { - case err = <-ch: - if errors.Is(err, http.ErrServerClosed) { - return nil - } - - return err - case <-ctx.Done(): - return ctx.Err() - } -} - -//nolint:gochecknoinits func init() { rootCmd.Flags().StringVarP( &outputFlag,