6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-30 18:51:03 +00:00
prologic-saltyim/cmd/salty-chat/root.go
2022-03-23 12:39:31 +00:00

126 lines
3.1 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
sync "github.com/sasha-s/go-deadlock"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.mills.io/saltyim"
)
var configFile string
var rootCmd = &cobra.Command{
Use: "salty-chat",
Version: saltyim.FullVersion(),
Short: "Salty IM Command-line client",
Long: `salty.im is an open specification for a new Saltpack based e2e
encrypted messaging protocol and platform for secure communications with
a focus on privacy, security and being self-hosted.
Getting Started:
$ salty-chat make-user nick@domain
# follow the instructions
See https://salty.im for more details.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// set logging level
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
// Disable deadlock detection in production mode
sync.Opts.Disable = true
}
},
}
// Execute adds all child commands to the root command
// and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(
&configFile, "config", "c", "",
"config file (default is $HOME/.config/salty/config.yml)",
)
rootCmd.PersistentFlags().StringP(
"user", "u", "",
"User name/profile form config to use",
)
rootCmd.PersistentFlags().BoolP(
"debug", "d", false,
"Enable debug logging",
)
rootCmd.PersistentFlags().StringP(
"identity", "i", saltyim.DefaultIdentity(),
"Use the identity file at PATH",
)
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
viper.SetDefault("debug", false)
viper.BindPFlag("user", rootCmd.PersistentFlags().Lookup("user"))
viper.SetDefault("user", "")
viper.BindPFlag("identity", rootCmd.PersistentFlags().Lookup("identity"))
viper.SetDefault("identity", saltyim.DefaultIdentity())
viper.BindPFlag("pre-hook", rootCmd.PersistentFlags().Lookup("pre-hook"))
viper.SetDefault("pre-hook", "")
viper.BindPFlag("pre-hook", rootCmd.PersistentFlags().Lookup("pre-hook"))
viper.SetDefault("post-hook", "")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if configFile != "" {
// Use config file from the flag.
viper.SetConfigFile(configFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.AddConfigPath(filepath.Join(home, ".config", "salty"))
viper.AddConfigPath(filepath.Join("$XDG_CONFIG_HOME", "salty"))
viper.SetConfigName("config")
viper.SetConfigType("yml")
}
// from the environment
viper.SetEnvPrefix("SALTY")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Debugf("Using config file: %s", viper.ConfigFileUsed())
}
}