From 40837755116812b403d0d98c77fd9c64d604ac11 Mon Sep 17 00:00:00 2001 From: Kirill Scherba Date: Wed, 13 Sep 2023 13:06:31 +0300 Subject: [PATCH] Move branch module to the menu package --- cmd/teonet/commands.go | 12 +++++++++--- cmd/teonet/main.go | 4 ++-- cmd/teonet/{ => menu}/batch.go | 26 +++++++++++++------------- cmd/teonet/teocli.go | 4 ++-- teonet.go | 2 +- 5 files changed, 27 insertions(+), 21 deletions(-) rename cmd/teonet/{ => menu}/batch.go (71%) diff --git a/cmd/teonet/commands.go b/cmd/teonet/commands.go index e042ae3..c07f1cd 100644 --- a/cmd/teonet/commands.go +++ b/cmd/teonet/commands.go @@ -15,7 +15,7 @@ import ( "github.com/teonet-go/teonet/cmd/teonet/menu" ) -// Command name +// Commands name const ( cmdConnectTo = "connectto" cmdSendTo = "sendto" @@ -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 @@ -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 @@ -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 diff --git a/cmd/teonet/main.go b/cmd/teonet/main.go index b8838ba..25aa395 100644 --- a/cmd/teonet/main.go +++ b/cmd/teonet/main.go @@ -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( diff --git a/cmd/teonet/batch.go b/cmd/teonet/menu/batch.go similarity index 71% rename from cmd/teonet/batch.go rename to cmd/teonet/menu/batch.go index 949bfd9..96ec6ff 100644 --- a/cmd/teonet/batch.go +++ b/cmd/teonet/menu/batch.go @@ -1,4 +1,4 @@ -package main +package menu import ( "bufio" @@ -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 } @@ -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) } @@ -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 } @@ -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 diff --git a/cmd/teonet/teocli.go b/cmd/teonet/teocli.go index a930b62..03cefc0 100644 --- a/cmd/teonet/teocli.go +++ b/cmd/teonet/teocli.go @@ -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() @@ -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 diff --git a/teonet.go b/teonet.go index c728521..6b1adcd 100644 --- a/teonet.go +++ b/teonet.go @@ -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()))