Skip to content

Commit

Permalink
Adding default command
Browse files Browse the repository at this point in the history
Adding config file option
Adding config.yaml.sample
  • Loading branch information
LuD1161 committed May 11, 2022
1 parent 52f1bb3 commit 4d2518e
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/
set-env-vars.sh
upi-recon-cli
upi-recon-cli
config.yaml
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GIT_COMMIT=$(shell git rev-parse --short=10 HEAD)

.PHONY: build-and-execute
build-and-execute:
chmod +x ./set-env-vars.sh && . ./set-env-vars.sh && go build -ldflags "-X main.GitCommit=${GIT_COMMIT}" -o ${APP} main.go && chmod +x ./${APP} && ./${APP}
go build -ldflags "-X main.GitCommit=${GIT_COMMIT}" -o ${APP} main.go && chmod +x ./${APP} && ./${APP}

.PHONY: build
build:
Expand Down
8 changes: 5 additions & 3 deletions cmd/checkUpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// checkUpiCmd represents the checkUpi command
Expand All @@ -25,8 +26,9 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
api_key := viper.Get("RAZORPAY_LIVE_API_KEY").(string)
if check_is_a_number(args[0]) {
checkUpi(args[0])
checkUpi(args[0], api_key)
}
},
}
Expand All @@ -46,7 +48,7 @@ func init() {

}

func checkUpi(number string) {
func checkUpi(number string, api_key string) {
maxGoroutines := 1000
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
Expand All @@ -71,7 +73,7 @@ func checkUpi(number string) {
vpasChan := make(chan string, maxGoroutines)
resultsChan := make(chan VPAResponse)
for i := 0; i < maxGoroutines; i++ {
go MakeRequest(vpasChan, resultsChan)
go MakeRequest(vpasChan, resultsChan, api_key)
}

go func() {
Expand Down
61 changes: 46 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ Copyright © 2022 Aseem Shrey
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "upi-recon-cli",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
var (
cfgFile string
userLicense string
rootCmd = &cobra.Command{
Use: "upi-recon-cli",
Args: cobra.ArbitraryArgs, // https://github.com/spf13/cobra/issues/42
Short: "Check UPI ids corresponding to a mobile number",
Long: `Check virtual payment address corresponding to a mobile number.
Get the user's name as well.`,
Run: func(cmd *cobra.Command, args []string) {
api_key := viper.Get("RAZORPAY_LIVE_API_KEY").(string)
if check_is_a_number(args[0]) {
checkUpi(args[0], api_key)
}
},
}
)

// 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.
Expand All @@ -39,9 +45,34 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.upi-recon-cli.yaml)")
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().Int32P("threads", "t", 1000, "No of threads")
}

func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".cobra")
}

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
4 changes: 2 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/rs/zerolog/log"
)

func MakeRequest(vpasChan <-chan string, resultsChan chan<- VPAResponse) {
func MakeRequest(vpasChan <-chan string, resultsChan chan<- VPAResponse, api_key string) {
client := http.Client{Timeout: TIMEOUT * time.Second}
url := fmt.Sprintf("https://api.razorpay.com/v1/payments/validate/account?key_id=%s", os.Getenv("RAZORPAY_LIVE_API_KEY"))
url := fmt.Sprintf("https://api.razorpay.com/v1/payments/validate/account?key_id=%s", api_key)

for vpa := range vpasChan {
result := VPAResponse{
Expand Down
1 change: 1 addition & 0 deletions config.yaml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RAZORPAY_LIVE_API_KEY: <RAZORPAY_LIVE_KEY_HERE>
19 changes: 18 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@ module github.com/LuD1161/upi-recon-cli

go 1.18

require github.com/rs/zerolog v1.26.1

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/rs/zerolog v1.26.1
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.11.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

0 comments on commit 4d2518e

Please sign in to comment.