How to Use the cobra Library for CLI Configuration

Cli
Use spf13/cobra to define CLI commands and spf13/viper to handle configuration files and flags for Go applications.

Use the spf13/cobra library to define commands and spf13/viper to manage configuration files, environment variables, and flags for your CLI.

package main

import (
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

var rootCmd = &cobra.Command{
	Use: "myapp",
	Run: func(cmd *cobra.Command, args []string) {
		viper.BindPFlags(cmd.Flags())
		fmt.Println(viper.GetString("config"))
	},
}

func main() {
	viper.SetConfigFile("config.yaml")
	viper.ReadInConfig()
	rootCmd.Execute()
}