package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "frostfs-locode-db",
	Short: "Command Line Tool to work with FrostFS' UN/LOCODE databases",
	Long:  "This tool can be used for generating or accessing FrostFS UN/LOCODE databases.",
}

func ExitOnErr(cmd *cobra.Command, errFmt string, err error) {
	if err == nil {
		return
	}

	if errFmt != "" {
		err = fmt.Errorf(errFmt, err)
	}

	const (
		_ = iota
		internal
		aclDenied
	)
	cmd.PrintErrln(err)
	if cmd.PersistentPostRun != nil {
		cmd.PersistentPostRun(cmd, nil)
	}
	os.Exit(internal)
}

func main() {
	initUtilLocodeGenerateCmd()
	initUtilLocodeInfoCmd()
	rootCmd.AddCommand(locodeGenerateCmd, locodeInfoCmd)
	err := rootCmd.Execute()
	if err != nil {
		ExitOnErr(rootCmd, "", err)
	}
}