Files
tg_exporter/main.go
2024-09-04 13:50:11 +08:00

59 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"exporter/config"
"exporter/internal"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var cfgFile string
func main() {
rootCmd := &cobra.Command{
Use: "exporter",
Short: "Yet Another Telegram Channel Exporter",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("load configs")
if err := config.Load(cfgFile); err != nil {
return errors.Wrap(err, "load config")
}
fmt.Println("init db")
if err := internal.InitDB(config.C.DSN); err != nil {
return errors.Wrap(err, "init db")
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("client exit")
return internal.Close()
},
}
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "C", "/root/.exporter.yml", "config file")
rootCmd.AddCommand(
internal.LoginCmd(),
internal.ExportCmd(),
internal.PublishCmd(),
internal.WatchCmd(),
)
// rootCmd.SilenceErrors = true
rootCmd.SilenceUsage = true
rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
cmd.Println(err)
cmd.Println(cmd.UsageString())
return err
})
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}