From ac1ca3f661752d2cf2ba15fd6c17f32e63ae73fb Mon Sep 17 00:00:00 2001 From: zu1k Date: Mon, 10 Aug 2020 13:01:43 +0800 Subject: [PATCH] move system related code to app --- cmd/cdn.go | 18 +---------- cmd/root.go | 26 +-------------- internal/app/command_others.go | 39 +++++++++++++++++++++++ internal/app/command_windows.go | 56 +++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 42 deletions(-) create mode 100644 internal/app/command_others.go create mode 100644 internal/app/command_windows.go diff --git a/cmd/cdn.go b/cmd/cdn.go index 656e5cf..5b1895b 100644 --- a/cmd/cdn.go +++ b/cmd/cdn.go @@ -1,10 +1,6 @@ package cmd import ( - "bufio" - "fmt" - "os" - "github.com/zu1k/nali/internal/app" "github.com/spf13/cobra" @@ -21,19 +17,7 @@ var cdnCmd = &cobra.Command{ } app.InitCDNDB() - - if len(args) == 0 { - stdin := bufio.NewScanner(os.Stdin) - for stdin.Scan() { - line := stdin.Text() - if line == "quit" || line == "exit" { - return - } - fmt.Println(app.ReplaceCDNInString(line)) - } - } else { - app.ParseCDNs(args) - } + app.CDN(args) }, } diff --git a/cmd/root.go b/cmd/root.go index 5b0a41d..27092eb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,18 +1,12 @@ package cmd import ( - "bufio" - "fmt" "log" "os" - "runtime" - "syscall" "github.com/spf13/cobra" "github.com/zu1k/nali/internal/app" "github.com/zu1k/nali/internal/ipdb" - "golang.org/x/text/encoding/simplifiedchinese" - "golang.org/x/text/transform" ) var rootCmd = &cobra.Command{ @@ -61,25 +55,7 @@ Find document on: https://github.com/zu1k/nali Run: func(cmd *cobra.Command, args []string) { app.InitIPDB(ipdb.GetIPDBType()) app.InitCDNDB() - - if len(args) == 0 { - stdin := bufio.NewScanner(os.Stdin) - for stdin.Scan() { - line := stdin.Text() - if runtime.GOOS == "windows" { - ftype, _ := syscall.GetFileType(syscall.Handle(os.Stdin.Fd())) - if ftype == 3 { - line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line) - } - } - if line == "quit" || line == "exit" { - return - } - fmt.Printf("%s\n", app.ReplaceIPInString(app.ReplaceCDNInString(line))) - } - } else { - app.ParseIPs(args) - } + app.Root(args) }, } diff --git a/internal/app/command_others.go b/internal/app/command_others.go new file mode 100644 index 0000000..8348e64 --- /dev/null +++ b/internal/app/command_others.go @@ -0,0 +1,39 @@ +// +build !windows + +package app + +import ( + "bufio" + "fmt" + "os" +) + +func Root(args []string) { + if len(args) == 0 { + stdin := bufio.NewScanner(os.Stdin) + for stdin.Scan() { + line := stdin.Text() + if line == "quit" || line == "exit" { + return + } + fmt.Printf("%s\n", ReplaceIPInString(ReplaceCDNInString(line))) + } + } else { + ParseIPs(args) + } +} + +func CDN(args []string) { + if len(args) == 0 { + stdin := bufio.NewScanner(os.Stdin) + for stdin.Scan() { + line := stdin.Text() + if line == "quit" || line == "exit" { + return + } + fmt.Println(ReplaceCDNInString(line)) + } + } else { + ParseCDNs(args) + } +} diff --git a/internal/app/command_windows.go b/internal/app/command_windows.go new file mode 100644 index 0000000..5090d48 --- /dev/null +++ b/internal/app/command_windows.go @@ -0,0 +1,56 @@ +// +build windows + +package app + +import ( + "bufio" + "fmt" + "os" + "syscall" + + "golang.org/x/text/encoding/simplifiedchinese" + "golang.org/x/text/transform" +) + +var pipeline = false + +func init() { + ftype, _ := syscall.GetFileType(syscall.Handle(os.Stdin.Fd())) + pipeline = ftype == 3 +} + +func Root(args []string) { + if len(args) == 0 { + stdin := bufio.NewScanner(os.Stdin) + for stdin.Scan() { + line := stdin.Text() + if pipeline { + line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line) + } + if line == "quit" || line == "exit" { + return + } + fmt.Printf("%s\n", ReplaceIPInString(ReplaceCDNInString(line))) + } + } else { + ParseIPs(args) + } +} + +func CDN(args []string) { + if len(args) == 0 { + stdin := bufio.NewScanner(os.Stdin) + for stdin.Scan() { + line := stdin.Text() + if pipeline { + line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line) + } + if line == "quit" || line == "exit" { + return + } + fmt.Println(ReplaceCDNInString(line)) + } + } else { + ParseCDNs(args) + } +}