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 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(),
	)

	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}
