package cli

import (
	"os"
	"path/filepath"
	"strconv"

	"github.com/spf13/cobra"
)

func flag(cmd *cobra.Command, f string) string {
	s := cmd.Flag(f).DefValue
	if cmd.Flag(f).Changed {
		s = cmd.Flag(f).Value.String()
	}

	return s
}

func GetCliStringFlag(cmd *cobra.Command, f string) string {
	return flag(cmd, f)
}

func GetCliIntFlag(cmd *cobra.Command, f string) int {
	v, err := strconv.Atoi(flag(cmd, f))
	if err != nil {
		return 0
	}

	return v
}

func BinName() string {
	return filepath.Base(os.Args[0])
}

package cli

import (
	"os"
	"path/filepath"
	"strconv"

	"github.com/spf13/cobra"
)

func flag(cmd *cobra.Command, f string) string {
	s := cmd.Flag(f).DefValue
	if cmd.Flag(f).Changed {
		s = cmd.Flag(f).Value.String()
	}

	return s
}

func GetCliStringFlag(cmd *cobra.Command, f string) string {
	return flag(cmd, f)
}

func GetCliIntFlag(cmd *cobra.Command, f string) int {
	v, err := strconv.Atoi(flag(cmd, f))
	if err != nil {
		return 0
	}

	return v
}

func BinName() string {
	name, err := os.Executable()
	if err != nil {
		return filepath.Base(os.Args[0])
	}

	link, err := filepath.EvalSymlinks(name)
	if err != nil {
		return filepath.Base(name)
	}

	return filepath.Base(link)
}

Fix bin name when using symlink.
Fix bin name when using symlink.
