Skip to content

Commit

Permalink
Move branch module to the menu package
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-scherba committed Sep 13, 2023
1 parent 9631daa commit 4083775
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
12 changes: 9 additions & 3 deletions cmd/teonet/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/teonet-go/teonet/cmd/teonet/menu"
)

// Command name
// Commands name
const (
cmdConnectTo = "connectto"
cmdSendTo = "sendto"
Expand All @@ -25,6 +25,12 @@ const (
cmdHelp = "help"
)

// Name of batch files
const (
aliasBatchFile = "alias.conf"
connectBatchFile = "connectto.conf"
)

var ErrWrongNumArguments = errors.New("wrong number of arguments")

// addCommands add commands
Expand Down Expand Up @@ -79,7 +85,7 @@ func (c CmdAlias) Exec(line string) (err error) {
// Check -save flag
case save:
aliases := c.alias.list()
c.batch.Save(aliasBatchFile, cmdAlias, aliases)
c.batch.Save(appShort, aliasBatchFile, cmdAlias, aliases)
return

// Check length of arguments
Expand Down Expand Up @@ -150,7 +156,7 @@ func (c CmdConnectTo) Exec(line string) (err error) {
connecttos = append(connecttos, alias)
}
}
c.batch.Save(connectBatchFile, cmdConnectTo, connecttos)
c.batch.Save(appShort, connectBatchFile, cmdConnectTo, connecttos)
return

// Check length of arguments
Expand Down
4 changes: 2 additions & 2 deletions cmd/teonet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func main() {
}

// Run batch files
cli.batch.run(aliasBatchFile)
cli.batch.run(connectBatchFile)
cli.batch.Run(appShort, aliasBatchFile)
cli.batch.Run(appShort, connectBatchFile)

// Run Teonet CLI commands menu
fmt.Print(
Expand Down
26 changes: 13 additions & 13 deletions cmd/teonet/batch.go → cmd/teonet/menu/batch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package menu

import (
"bufio"
Expand All @@ -8,20 +8,17 @@ import (
"strings"

"github.com/teonet-go/teonet"
"github.com/teonet-go/teonet/cmd/teonet/menu"
)

const (
aliasBatchFile = "alias.conf"
connectBatchFile = "connectto.conf"
)
type Batch struct{ menu *Menu }

type Batch struct{ menu *menu.Menu }
// NewBatch creates new Batch object
func NewBatch(menu *Menu) *Batch { return &Batch{menu} }

// run aliases from config file
func (b *Batch) run(name string) (err error) {
// Run executes commands from batch config file
func (b *Batch) Run(appShort, name string) (err error) {
// Get file name
fname, err := b.file(name)
fname, err := b.file(appShort, name)
if err != nil {
return
}
Expand All @@ -42,6 +39,9 @@ func (b *Batch) run(name string) (err error) {
line = space.ReplaceAllString(line, " ")

fmt.Println(line) // Println will add back the final '\n'
if len(line) == 0 {
continue
}
if err = b.menu.ExecuteCommand(line); err != nil {
fmt.Println("error:", err)
}
Expand All @@ -53,9 +53,9 @@ func (b *Batch) run(name string) (err error) {
}

// Save batch to config file
func (b Batch) Save(name string, prefix string, batch []string) (err error) {
func (b Batch) Save(appShort, name string, prefix string, batch []string) (err error) {
// Get file name
fname, err := b.file(name)
fname, err := b.file(appShort, name)
if err != nil {
return
}
Expand All @@ -78,7 +78,7 @@ func (b Batch) Save(name string, prefix string, batch []string) (err error) {
}

// file return full file name with config dir folder
func (b Batch) file(name string) (f string, err error) {
func (b Batch) file(appShort, name string) (f string, err error) {
f, err = os.UserConfigDir()
if err != nil {
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/teonet/teocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewTeocli(teo *teonet.Teonet, appShort string) (cli *Teocli, err error) {
return
}
cli.menu.Add(cli.commands...)
cli.batch = &Batch{cli.menu}
cli.batch = menu.NewBatch(cli.menu)
cli.alias = newAlias()
cli.api = newAPI()

Expand All @@ -32,7 +32,7 @@ func NewTeocli(teo *teonet.Teonet, appShort string) (cli *Teocli, err error) {

type Teocli struct {
commands []menu.Item
batch *Batch
batch *menu.Batch
alias *Alias
menu *menu.Menu
api *API
Expand Down
2 changes: 1 addition & 1 deletion teonet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/teonet-go/tru/teolog"
)

const Version = "0.6.3"
const Version = "0.6.4"

// Reset random and use rnd instead
var rnd = rand.New(rand.NewSource(time.Now().Unix()))
Expand Down

0 comments on commit 4083775

Please sign in to comment.