Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cashu/cashu.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ func (e ErrorResponse) Error() string {

// CreateInvoice will generate a blank invoice
func CreateInvoice() lightning.Invoicer {
if lightning.Config.Lightning.Enabled {
return lnbits.NewInvoice()
}
return nil
return lnbits.NewInvoice()
}

type Mint struct {
Expand Down
33 changes: 6 additions & 27 deletions cmd/cashu/feni.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
package main

import (
"fmt"
"os"
"regexp"
"strings"

"github.com/c-bata/go-prompt"
"github.com/cashubtc/cashu-feni/cmd/cashu/feni"
)

var sendRegex = regexp.MustCompile("send [0-9]")
var advancedPrompt = &feni.CobraPrompt{
RootCmd: feni.RootCmd,
PersistFlagValues: true,
ShowHelpCommandAndFlags: false,
DisableCompletionCommand: true,
AddDefaultExitCommand: false,

DynamicSuggestionsFunc: feni.DynamicSuggestion(feni.RootCmd),
GoPromptOptions: []prompt.Option{
prompt.OptionTitle("cashu-feni"),
prompt.OptionPrefix(">(cashu-feni)> "),
prompt.OptionMaxSuggestion(10),
},
DynamicSuggestionsFunc: func(annotationValue string, document *prompt.Document) []prompt.Suggest {
if document.Text == "-w " || document.Text == "--wallet " {
fmt.Println(document.Text)
if suggestions := feni.GetWalletsDynamic(annotationValue); suggestions != nil {
return suggestions
}
} else if document.Text == "locks " || document.Text == "-l " {
if suggestions := feni.GetLocksDynamic(annotationValue); suggestions != nil {
return suggestions
}
} else if sendRegex.MatchString(document.Text) {
document.Text = fmt.Sprintf("%s %s", document.Text, "-m ")
if suggestions := feni.GetMintsDynamic(annotationValue); suggestions != nil {
return suggestions
}
}

return nil
},
OnErrorFunc: func(err error) {
if strings.Contains(err.Error(), "unknown command") {
feni.RootCmd.PrintErrln(err)
feni.RootCmd.Command().PrintErrln(err)
return
}

feni.RootCmd.PrintErr(err)
feni.RootCmd.Command().PrintErr(err)
os.Exit(1)
},
}

func main() {
feni.StartClientConfiguration()
advancedPrompt.RootCmd.PersistentFlags().StringVarP(&feni.WalletUsed, "wallet", "w", "wallet", "Name of your wallet")
advancedPrompt.RootCmd.PersistentFlags().StringVarP(&feni.Host, "host", "H", "", "Mint host address")

advancedPrompt.RootCmd.Command().PersistentFlags().StringVarP(&feni.WalletName, "wallet", "w", "wallet", "Name of your wallet")
advancedPrompt.RootCmd.Command().PersistentFlags().StringVarP(&advancedPrompt.RootCmd.Wallet().Config.MintServerHost, "host", "H", "", "Mint host address")
advancedPrompt.Run()
}
11 changes: 6 additions & 5 deletions cmd/cashu/feni/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package feni

import (
"fmt"
"github.com/cashubtc/cashu-feni/wallet"
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(balanceCommand)
RootCmd.Command().AddCommand(balanceCommand)
}

var balanceCommand = &cobra.Command{
Use: "balance",
Short: "Check your balance",
Long: ``,
PreRun: PreRunFeni,
Run: balance,
PreRun: RunCommandWithWallet(RootCmd, preRun),
Run: RunCommandWithWallet(RootCmd, balance),
}

func balance(cmd *cobra.Command, args []string) {
balances, err := Wallet.balancePerKeySet()
func balance(wallet *wallet.Wallet, params cobraParameter) {
balances, err := wallet.Balances()
if err != nil {
panic(err)
}
Expand Down
21 changes: 11 additions & 10 deletions cmd/cashu/feni/burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"github.com/cashubtc/cashu-feni/cashu"
"github.com/cashubtc/cashu-feni/wallet"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -12,35 +13,35 @@ var burnCommand = &cobra.Command{
Use: "burn",
Short: "Burn spent tokens",
Long: ``,
PreRun: PreRunFeni,
Run: burnCmd,
PreRun: RunCommandWithWallet(RootCmd, preRun),
Run: RunCommandWithWallet(RootCmd, burnCmd),
}
var all bool
var force bool

func init() {
burnCommand.PersistentFlags().BoolVarP(&all, "all", "a", false, "burn all spent tokens.")
burnCommand.PersistentFlags().BoolVarP(&force, "force", "f", false, "force check on all tokens.")
RootCmd.AddCommand(burnCommand)
RootCmd.Command().AddCommand(burnCommand)
}
func burnCmd(cmd *cobra.Command, args []string) {
func burnCmd(wallet *wallet.Wallet, params cobraParameter) {
var token string
if len(args) == 1 {
token = args[0]
if len(params.args) == 1 {
token = params.args[0]
}
if !(all || force || token != "") || (token != "" && all) {
cmd.Println("Error: enter a token or use --all to burn all pending tokens or --force to check all tokens.")
params.cmd.Println("Error: enter a token or use --all to burn all pending tokens or --force to check all tokens.")
return
}
proofs := make([]cashu.Proof, 0)
var err error
if all {
proofs, err = storage.GetReservedProofs()
proofs, err = wallet.Storage.GetReservedProofs()
if err != nil {
log.Fatal(err)
}
} else if force {
proofs = Wallet.proofs
// invalidate all wallet proofs
} else {
p, err := base64.URLEncoding.DecodeString(token)
if err != nil {
Expand All @@ -56,7 +57,7 @@ func burnCmd(cmd *cobra.Command, args []string) {
return
}

err = invalidate(proofs)
err = wallet.Invalidate(proofs)
if err != nil {
log.Fatal(err)
}
Expand Down
39 changes: 39 additions & 0 deletions cmd/cashu/feni/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package feni

import (
"github.com/cashubtc/cashu-feni/wallet"
"github.com/spf13/cobra"
)

type command func(wallet *wallet.Wallet, params cobraParameter)
type cobraParameter struct {
cmd *cobra.Command
args []string
}
type RootCommand struct {
cmd *cobra.Command
wallet *wallet.Wallet
}

func (r *RootCommand) SetCommand(cmd *cobra.Command) {
r.cmd = cmd
}
func (r *RootCommand) Command() *cobra.Command {
return r.cmd
}
func (r *RootCommand) Wallet() *wallet.Wallet {
return r.wallet
}
func RunCommandWithWallet(root *RootCommand, command command) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
cmd.ParseFlags(args)
command(root.wallet, cobraParameter{cmd: cmd, args: args})
}
}
func preRun(w *wallet.Wallet, params cobraParameter) {
opts := make([]wallet.Option, 0)
if WalletName != "" {
opts = append(opts, wallet.WithName(WalletName))
}
RootCmd.wallet = wallet.New(opts...)
}
Loading
Loading