apollo-backend/internal/cmd/root.go
2021-07-15 20:09:38 -04:00

64 lines
1.3 KiB
Go

package cmd
import (
"context"
"os"
"runtime/pprof"
"github.com/DataDog/datadog-go/statsd"
"github.com/adjust/rmq/v4"
"github.com/go-redis/redis/v8"
_ "github.com/heroku/x/hmetrics/onload"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type Command struct {
ctx context.Context
logger *logrus.Logger
statsd *statsd.Client
redis *redis.Client
jobs *rmq.Connection
db *pgxpool.Pool
}
func Execute(ctx context.Context) int {
_ = godotenv.Load()
profile := false
rootCmd := &cobra.Command{
Use: "apollo",
Short: "Apollo is an amazing Reddit client for iOS. This isn't it, but it helps.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !profile {
return nil
}
f, perr := os.Create("cpu.pprof")
if perr != nil {
return perr
}
pprof.StartCPUProfile(f)
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if profile {
pprof.StopCPUProfile()
}
},
}
rootCmd.PersistentFlags().BoolVarP(&profile, "profile", "p", false, "record CPU pprof")
rootCmd.AddCommand(APICmd(ctx))
rootCmd.AddCommand(SchedulerCmd(ctx))
rootCmd.AddCommand(WorkerCmd(ctx))
if err := rootCmd.Execute(); err != nil {
return 1
}
return 0
}