62 lines
1.3 KiB
Go
62 lines
1.3 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")
|
|
}
|
|
|
|
fmt.Println("init client")
|
|
defer fmt.Println("init client done")
|
|
return internal.InitClient(config.C)
|
|
},
|
|
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Println("client exit")
|
|
return internal.Close()
|
|
},
|
|
}
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "C", "config.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)
|
|
}
|
|
}
|