cli/cmd/pastee.go

67 lines
1.5 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"
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
2021-03-27 04:07:32 +00:00
viper.SetConfigName(".pastee")
viper.SetConfigType("yaml")
viper.AddConfigPath(home)
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()
}