Resolve key issue
This commit is contained in:
parent
e2da267c47
commit
68b03407e7
16
.drone.yml
16
.drone.yml
@ -10,14 +10,14 @@ steps:
|
|||||||
path: /build
|
path: /build
|
||||||
commands:
|
commands:
|
||||||
- mkdir -p /build
|
- mkdir -p /build
|
||||||
- GOOS=linux GOARCH=386 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_linux_i386
|
- GOOS=linux GOARCH=386 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_linux_i386
|
||||||
- GOOS=linux GOARCH=amd64 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_linux_amd64
|
- GOOS=linux GOARCH=amd64 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_linux_amd64
|
||||||
- GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_linux_armv7
|
- GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_linux_armv7
|
||||||
- GOOS=linux GOARCH=arm64 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_linux_arm64
|
- GOOS=linux GOARCH=arm64 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_linux_arm64
|
||||||
- GOOS=windows GOARCH=386 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_windows_i386.exe
|
- GOOS=windows GOARCH=386 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_windows_i386.exe
|
||||||
- GOOS=windows GOARCH=amd64 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_windows_amd64.exe
|
- GOOS=windows GOARCH=amd64 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_windows_amd64.exe
|
||||||
- GOOS=darwin GOARCH=arm64 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_macos_arm64
|
- GOOS=darwin GOARCH=arm64 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_macos_arm64
|
||||||
- GOOS=darwin GOARCH=amd64 go build -ldflags="-X 'cmd.Key=$API_KEY'" -o /build/pastee_macos_amd64
|
- GOOS=darwin GOARCH=amd64 go build -ldflags="-X 'paste.ee/cli/cmd.Key=$API_KEY'" -o /build/pastee_macos_amd64
|
||||||
environment:
|
environment:
|
||||||
API_KEY:
|
API_KEY:
|
||||||
from_secret: api_key
|
from_secret: api_key
|
||||||
|
@ -1,72 +1,66 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"os"
|
"os"
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
Key = ""
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "pastee",
|
Use: "pastee",
|
||||||
Short: "A quick and easy to use Command-Line Paste Interface",
|
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!`,
|
Long: `Fast, ad-free, secure, and feature filled! Submit pastes to Paste.ee from any operating system, anywhere!`,
|
||||||
Args: cobra.ArbitraryArgs,
|
Args: cobra.ArbitraryArgs,
|
||||||
Run: uploadCommandHandler,
|
Run: uploadCommandHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfgFile string
|
cfgFile string
|
||||||
|
Key = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
rootCmd.PersistentFlags().BoolP("encrypt", "e", true, "Encrypt uploaded contents")
|
rootCmd.PersistentFlags().BoolP("encrypt", "e", false, "Encrypt uploaded contents")
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
|
||||||
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
|
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
|
||||||
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
|
viper.BindPFlag("encrypt", rootCmd.PersistentFlags().Lookup("encrypt"))
|
||||||
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
viper.SetDefault("apiKey", Key)
|
||||||
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
|
||||||
viper.SetDefault("license", "apache")
|
|
||||||
viper.SetDefault("apiKey", Key)
|
|
||||||
|
|
||||||
rootCmd.AddCommand(loginCmd)
|
rootCmd.AddCommand(loginCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
if cfgFile != "" {
|
if cfgFile != "" {
|
||||||
// Use config file from the flag.
|
// Use config file from the flag.
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
} else {
|
} else {
|
||||||
home, err := homedir.Dir()
|
home, err := homedir.Dir()
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
|
|
||||||
viper.SetConfigName(".pastee")
|
viper.SetConfigName(".pastee")
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
viper.AddConfigPath(home)
|
viper.AddConfigPath(home)
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Error: Configuration file was unable to be read -", err)
|
fmt.Println("Error: Configuration file was unable to be read -", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
}
|
}
|
||||||
|
101
cmd/upload.go
101
cmd/upload.go
@ -1,64 +1,77 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"errors"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/cobra"
|
||||||
"io/ioutil"
|
"github.com/spf13/viper"
|
||||||
"math"
|
"io/ioutil"
|
||||||
"os"
|
"math"
|
||||||
"paste.ee/go"
|
"os"
|
||||||
"path"
|
"paste.ee/go"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
// uploadCommandHandler handles the root command from Cobra
|
// uploadCommandHandler handles the root command from Cobra
|
||||||
// The program takes two inputs, an arg list (files), or stdin.
|
// The program takes two inputs, an arg list (files), or stdin.
|
||||||
// If args is empty, stdin will be used.
|
// If args is empty, stdin will be used.
|
||||||
func uploadCommandHandler(cmd *cobra.Command, args []string) {
|
func uploadCommandHandler(cmd *cobra.Command, args []string) {
|
||||||
client := pastee.New(viper.GetString("apiKey"))
|
client := pastee.New(viper.GetString("apiKey"))
|
||||||
|
|
||||||
paste := &pastee.Paste{}
|
paste := &pastee.Paste{
|
||||||
|
Encrypted: viper.GetBool("encrypt"),
|
||||||
|
}
|
||||||
|
|
||||||
if len(args) > 0 && args[0] != "-" {
|
if len(args) > 0 && args[0] != "-" {
|
||||||
paste.Sections = make([]*pastee.Section, int(math.Min(float64(len(args)), 10)))
|
paste.Sections = make([]*pastee.Section, int(math.Min(float64(len(args)), 10)))
|
||||||
|
|
||||||
// File list
|
// File list
|
||||||
for i, filePath := range args {
|
for i, filePath := range args {
|
||||||
if i >= 10 {
|
if i >= 10 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes, err := ioutil.ReadFile(filePath)
|
bytes, err := ioutil.ReadFile(filePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.PrintErrln("Unable to read file:", err)
|
cmd.PrintErrln("Unable to read file:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
paste.Sections[i] = &pastee.Section{
|
paste.Sections[i] = &pastee.Section{
|
||||||
Name: path.Base(filePath),
|
Name: path.Base(filePath),
|
||||||
Contents: string(bytes),
|
Contents: string(bytes),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Stdin
|
// Stdin
|
||||||
stdinContents, err := ioutil.ReadAll(os.Stdin)
|
stdinContents, err := ioutil.ReadAll(os.Stdin)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.PrintErrln("Error reading from stdin:", err)
|
cmd.PrintErrln("Error reading from stdin:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
paste.Sections = []*pastee.Section{
|
paste.Sections = []*pastee.Section{
|
||||||
{Contents: string(stdinContents)},
|
{Contents: string(stdinContents)},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := client.Submit(paste)
|
res, err := client.Submit(paste)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil || res.Link == "" {
|
||||||
cmd.PrintErrln("Unable to upload paste:", err)
|
if err == nil {
|
||||||
os.Exit(1)
|
err = errors.New("unknown error")
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Println(res.Link)
|
cmd.PrintErrln("Unable to upload paste:", err)
|
||||||
}
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
link := res.Link
|
||||||
|
|
||||||
|
if res.Key != "" {
|
||||||
|
link += "#" + res.Key
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Println(link)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user