1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 13:19:02 +08:00

move system related code to app

This commit is contained in:
zu1k 2020-08-10 13:01:43 +08:00
parent 23ba5a45a5
commit ac1ca3f661
4 changed files with 97 additions and 42 deletions

View File

@ -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)
},
}

View File

@ -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)
},
}

View File

@ -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)
}
}

View File

@ -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)
}
}