78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
cfgFile string
|
|
serverAddr string
|
|
tokenFile string
|
|
|
|
rootCmd = &cobra.Command{
|
|
Use: "mcias-client",
|
|
Short: "MCIAS Client - Command line client for the Metacircular Identity and Access System",
|
|
Long: `MCIAS Client is a command line tool for interacting with the MCIAS server.
|
|
It provides access to the MCIAS API endpoints for authentication and resource access.
|
|
|
|
It currently supports the following operations:
|
|
1. User password authentication
|
|
2. User token authentication
|
|
3. Database credential retrieval`,
|
|
}
|
|
)
|
|
|
|
func Execute() error {
|
|
return rootCmd.Execute()
|
|
}
|
|
|
|
// setupRootCommand initializes the root command and its flags
|
|
func setupRootCommand() {
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mcias-client.yaml)")
|
|
rootCmd.PersistentFlags().StringVar(&serverAddr, "server", "http://localhost:8080", "MCIAS server address")
|
|
rootCmd.PersistentFlags().StringVar(&tokenFile, "token-file", "", "File to store authentication token (default is $HOME/.mcias-token)")
|
|
|
|
if err := viper.BindPFlag("server", rootCmd.PersistentFlags().Lookup("server")); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error binding server flag: %v\n", err)
|
|
}
|
|
if err := viper.BindPFlag("token-file", rootCmd.PersistentFlags().Lookup("token-file")); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error binding token-file flag: %v\n", err)
|
|
}
|
|
}
|
|
|
|
func initConfig() {
|
|
if cfgFile != "" {
|
|
viper.SetConfigFile(cfgFile)
|
|
} else {
|
|
home, err := os.UserHomeDir()
|
|
cobra.CheckErr(err)
|
|
|
|
viper.AddConfigPath(home)
|
|
viper.SetConfigType("yaml")
|
|
viper.SetConfigName(".mcias-client")
|
|
}
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
|
}
|
|
|
|
// Set default token file if not specified
|
|
if viper.GetString("token-file") == "" {
|
|
home, err := os.UserHomeDir()
|
|
if err == nil {
|
|
viper.Set("token-file", fmt.Sprintf("%s/.mcias-token", home))
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
setupRootCommand()
|
|
} |