Repository: codenotary/immudb
File: cmd/immuadmin/commands/stats.go (Go)

Code before:

func (cl *commandline) status(cmd *cobra.Command) {
	ccmd := &cobra.Command{
		Use:     "status",
		Short:   "Show heartbeat status",
		Aliases: []string{"p"},
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := context.Background()
			if err := cl.immuClient.HealthCheck(ctx); err != nil {
				c.QuitWithUserError(err)
			}
			fmt.Println("OK - server is reachable and responding to queries")
			return nil
		},
		Args: cobra.NoArgs,
	}
	cmd.AddCommand(ccmd)
}

func (cl *commandline) stats(cmd *cobra.Command) {
	ccmd := &cobra.Command{
		Use:     "stats",
		Short:   fmt.Sprintf("Show statistics as text or visually with the '-v' option. Run 'immuadmin stats -h' for details."),
		Aliases: []string{"s"},
		PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
			if cl.immuClient, err = client.NewImmuClient(options()); err != nil {
				c.QuitToStdErr(err)
			}
			return
		},
		PersistentPostRun: func(cmd *cobra.Command, args []string) {
			if err := cl.immuClient.Disconnect(); err != nil {
				c.QuitToStdErr(err)
			}
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			raw, err := cmd.Flags().GetBool("raw")
			if err != nil {
				c.QuitToStdErr(err)
			}
			options := cl.immuClient.GetOptions()
			if raw {
				if err := stats.ShowMetricsRaw(options.Address); err != nil {
					c.QuitToStdErr(err)
				}
				return nil
			}
			text, err := cmd.Flags().GetBool("text")
			if err != nil {
				c.QuitToStdErr(err)
			}

Review comment (refactor):
@mmeloni Since almost all sub-commands have the same implementation for pre- and post- run functions, i suppose their implementation could be extracted in a single place and referenced in all sub-commands(?)

Code after:
	ccmd := &cobra.Command{
		Use:     "status",
		Short:   "Show heartbeat status",
		Aliases: []string{"p"},
		PersistentPreRunE: cl.connect,
		PersistentPostRun: cl.disconnect,
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := context.Background()
			if err := cl.immuClient.HealthCheck(ctx); err != nil {
				c.QuitWithUserError(err)
			}
			fmt.Println("OK - server is reachable and responding to queries")
			return nil
		},
		Args: cobra.NoArgs,
	}
	cmd.AddCommand(ccmd)
}

func (cl *commandline) stats(cmd *cobra.Command) {
	ccmd := &cobra.Command{
		Use:     "stats",
		Short:   fmt.Sprintf("Show statistics as text or visually with the '-v' option. Run 'immuadmin stats -h' for details."),
		Aliases: []string{"s"},
		PersistentPreRunE: cl.connect,
		PersistentPostRun: cl.disconnect,
		RunE: func(cmd *cobra.Command, args []string) error {
			raw, err := cmd.Flags().GetBool("raw")
			if err != nil {
				c.QuitToStdErr(err)
			}
			options := cl.immuClient.GetOptions()
			if raw {
				if err := stats.ShowMetricsRaw(options.Address); err != nil {
					c.QuitToStdErr(err)
				}
				return nil
			}
			text, err := cmd.Flags().GetBool("text")
			if err != nil {
				c.QuitToStdErr(err)
			}
			if text {
				if err := stats.ShowMetricsAsText(options.Address); err != nil {
					c.QuitToStdErr(err)
				}
				return nil
			}
			if err := stats.ShowMetricsVisually(options.Address); err != nil {
				c.QuitToStdErr(err)
			}