Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed Apr 14, 2020
1 parent ef2fdff commit 064ebad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
29 changes: 2 additions & 27 deletions cmd/pdu/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package main

import (
"fmt"

"github.com/pdupub/go-pdu/console"
"github.com/spf13/cobra"
)
Expand All @@ -28,40 +26,17 @@ var consoleCmd = &cobra.Command{
Use: "console",
Short: "Console of pdu",
RunE: func(_ *cobra.Command, args []string) error {
var content string
consoleInfoDisplay()

cls, err := console.NewConsole()
if err != nil {
return err
}
for {
fmt.Print("> ")
scanLine(&content)
if content == "quit" || content == "q" {
cls.Close()
break
} else if content == "show" {
cls.ExeShowCmd()
} else {
fmt.Println(content)
}
}
cls.Run()

return nil
},
}

func consoleInfoDisplay() {
fmt.Print(`
##############################################################
# #
# Welcome to PDU console ;-) #
# #
##############################################################
`)
}

func init() {
rootCmd.AddCommand(consoleCmd)
}
40 changes: 40 additions & 0 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package console

import (
"bufio"
"fmt"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -26,6 +28,7 @@ import (
type Console struct {
targetURL string
showCmd *cobra.Command
history []string
}

// NewConsole used to build a new console
Expand All @@ -40,6 +43,35 @@ func (c *Console) Close() {

}

// Run the console
func (c *Console) Run() {
c.welcome()
for {
fmt.Print("> ")
content := c.scanLine()
if content == "quit" || content == "q" {
c.Close()
break
} else if content == "show" {
c.ExeShowCmd()
} else {
fmt.Println(content)
}
}
}

// initInfoDisplay
func (c Console) welcome() {
fmt.Print(`
##############################################################
# #
# Welcome to PDU console ;-) #
# #
##############################################################
`)
}

// SetTargetURL set the url of remote url
func (c *Console) SetTargetURL(url string) {
c.targetURL = url
Expand All @@ -64,3 +96,11 @@ func (c *Console) ExeShowCmd() error {
}
return nil
}

func (c Console) scanLine() string {
var input string
reader := bufio.NewReader(os.Stdin)
data, _, _ := reader.ReadLine()
input = string(data)
return input
}

0 comments on commit 064ebad

Please sign in to comment.