Initial commit
This commit is contained in:
91
cmd/login.go
Normal file
91
cmd/login.go
Normal file
@ -0,0 +1,91 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/term"
|
||||
"os"
|
||||
pastee "paste.ee/go"
|
||||
)
|
||||
|
||||
var loginCmd = &cobra.Command{
|
||||
Use: "login",
|
||||
Short: "Login with an account and store a user access token",
|
||||
Long: `Logs into the Paste.ee API and stores a user access token to associate pastes to your account.`,
|
||||
Run: loginCommandHandler,
|
||||
}
|
||||
|
||||
func init() {
|
||||
loginCmd.Flags().StringP("username", "u", "", "Username")
|
||||
loginCmd.Flags().StringP("password", "p", "", "Password")
|
||||
}
|
||||
|
||||
// loginCommandHandler handles the login command from cobra
|
||||
func loginCommandHandler(cmd *cobra.Command, args []string) {
|
||||
client := pastee.New(viper.GetString("apiKey"))
|
||||
|
||||
username, err := cmd.Flags().GetString("username")
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
if username == "" || err != nil {
|
||||
for {
|
||||
fmt.Print("Username: ")
|
||||
username, err = reader.ReadString('\n')
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
} else if username == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
password, err := cmd.Flags().GetString("password")
|
||||
|
||||
if password == "" || err != nil {
|
||||
for {
|
||||
fmt.Print("Password: ")
|
||||
passwordBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
|
||||
if err != nil {
|
||||
password, err = reader.ReadString('\n')
|
||||
|
||||
if password != "" {
|
||||
break
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
password = string(passwordBytes)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
res, err := client.Authenticate(username, password)
|
||||
|
||||
if err != nil {
|
||||
cmd.PrintErrln("Unable to authenticate:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !res.Success {
|
||||
cmd.PrintErrln("Invalid username or password")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
viper.Set("apiKey", res.Key)
|
||||
err = viper.WriteConfig()
|
||||
|
||||
if err != nil {
|
||||
cmd.PrintErrln("Unable to write config:", err)
|
||||
}
|
||||
|
||||
cmd.Println("Success!")
|
||||
}
|
72
cmd/pastee.go
Normal file
72
cmd/pastee.go
Normal file
@ -0,0 +1,72 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
Key = ""
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
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,
|
||||
}
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
)
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().BoolP("encrypt", "e", true, "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("author", rootCmd.PersistentFlags().Lookup("author"))
|
||||
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
||||
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
||||
viper.SetDefault("license", "apache")
|
||||
viper.SetDefault("apiKey", Key)
|
||||
|
||||
rootCmd.AddCommand(loginCmd)
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
home, err := homedir.Dir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
viper.SetConfigName(".pastee")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(home)
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
|
||||
} else {
|
||||
fmt.Println("Error: Configuration file was unable to be read -", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
viper.AutomaticEnv()
|
||||
}
|
64
cmd/upload.go
Normal file
64
cmd/upload.go
Normal file
@ -0,0 +1,64 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
"paste.ee/go"
|
||||
"path"
|
||||
)
|
||||
|
||||
// uploadCommandHandler handles the root command from Cobra
|
||||
// The program takes two inputs, an arg list (files), or stdin.
|
||||
// If args is empty, stdin will be used.
|
||||
func uploadCommandHandler(cmd *cobra.Command, args []string) {
|
||||
client := pastee.New(viper.GetString("apiKey"))
|
||||
|
||||
paste := &pastee.Paste{}
|
||||
|
||||
if len(args) > 0 && args[0] != "-" {
|
||||
paste.Sections = make([]*pastee.Section, int(math.Min(float64(len(args)), 10)))
|
||||
|
||||
// File list
|
||||
for i, filePath := range args {
|
||||
if i >= 10 {
|
||||
break
|
||||
}
|
||||
|
||||
bytes, err := ioutil.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
cmd.PrintErrln("Unable to read file:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
paste.Sections[i] = &pastee.Section{
|
||||
Name: path.Base(filePath),
|
||||
Contents: string(bytes),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Stdin
|
||||
stdinContents, err := ioutil.ReadAll(os.Stdin)
|
||||
|
||||
if err != nil {
|
||||
cmd.PrintErrln("Error reading from stdin:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
paste.Sections = []*pastee.Section{
|
||||
{Contents: string(stdinContents)},
|
||||
}
|
||||
}
|
||||
|
||||
res, err := client.Submit(paste)
|
||||
|
||||
if err != nil {
|
||||
cmd.PrintErrln("Unable to upload paste:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cmd.Println(res.Link)
|
||||
}
|
Reference in New Issue
Block a user