cli/cmd/pastee.go

69 lines
1.6 KiB
Go
Raw Normal View History

2021-03-27 03:47:13 +00:00
package cmd
import (
2021-03-27 04:07:32 +00:00
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"path"
2021-03-27 03:47:13 +00:00
)
var rootCmd = &cobra.Command{
2021-03-27 04:07:32 +00:00
Use: "pastee",
Short: "A quick and easy to use Command-Line Paste Interface",
Long: `Fast, ad-free, secure, and feature filled! Submit pastes to Paste.ee from any operating system, anywhere!`,
Args: cobra.ArbitraryArgs,
Run: uploadCommandHandler,
2021-03-27 03:47:13 +00:00
}
var (
2021-03-27 04:07:32 +00:00
cfgFile string
Key = ""
2021-03-27 03:47:13 +00:00
)
func Execute() {
2021-03-27 04:07:32 +00:00
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
2021-03-27 03:47:13 +00:00
}
func init() {
2021-03-27 04:07:32 +00:00
cobra.OnInitialize(initConfig)
2021-03-27 03:47:13 +00:00
2021-03-27 04:07:32 +00:00
rootCmd.PersistentFlags().BoolP("encrypt", "e", false, "Encrypt uploaded contents")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
viper.BindPFlag("encrypt", rootCmd.PersistentFlags().Lookup("encrypt"))
viper.SetDefault("apiKey", Key)
2021-03-27 03:47:13 +00:00
2021-03-27 04:07:32 +00:00
rootCmd.AddCommand(loginCmd)
2021-03-27 03:47:13 +00:00
}
func initConfig() {
2021-03-27 04:07:32 +00:00
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
cobra.CheckErr(err)
2021-03-27 03:47:13 +00:00
viper.SetConfigName("pastee")
2021-03-27 04:07:32 +00:00
viper.SetConfigType("yaml")
viper.AddConfigPath(path.Join(home, ".config", "pastee"))
viper.AddConfigPath("/etc")
2021-03-27 03:47:13 +00:00
2021-03-27 04:07:32 +00:00
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
2021-03-27 03:47:13 +00:00
2021-03-27 04:07:32 +00:00
} else {
fmt.Println("Error: Configuration file was unable to be read -", err)
os.Exit(1)
}
}
}
2021-03-27 03:47:13 +00:00
2021-03-27 04:07:32 +00:00
viper.AutomaticEnv()
}