mirror of
https://github.com/zu1k/nali.git
synced 2025-02-02 10:22:41 +08:00
refactor: project structure
Signed-off-by: zu1k <i@lgf.im>
This commit is contained in:
parent
d1b584c3e7
commit
6bc863cc69
2
.github/workflows/go.yml
vendored
2
.github/workflows/go.yml
vendored
@ -13,7 +13,7 @@ jobs:
|
|||||||
stable: 'false'
|
stable: 'false'
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Cache go module
|
- name: Cache go module
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v2
|
||||||
|
26
cmd/root.go
26
cmd/root.go
@ -1,12 +1,19 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/zu1k/nali/pkg/entity"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/zu1k/nali/internal/app"
|
|
||||||
"github.com/zu1k/nali/internal/constant"
|
"github.com/zu1k/nali/internal/constant"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
@ -55,7 +62,22 @@ Find document on: https://github.com/zu1k/nali
|
|||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
gbk, _ := cmd.Flags().GetBool("gbk")
|
gbk, _ := cmd.Flags().GetBool("gbk")
|
||||||
app.Root(args, gbk)
|
|
||||||
|
if len(args) == 0 {
|
||||||
|
stdin := bufio.NewScanner(os.Stdin)
|
||||||
|
for stdin.Scan() {
|
||||||
|
line := stdin.Text()
|
||||||
|
if gbk {
|
||||||
|
line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line)
|
||||||
|
}
|
||||||
|
if line == "quit" || line == "exit" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).ColorString())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString())
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,9 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"github.com/zu1k/nali/internal/db"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/zu1k/nali/internal/constant"
|
|
||||||
"github.com/zu1k/nali/pkg/cdn"
|
|
||||||
"github.com/zu1k/nali/pkg/qqwry"
|
|
||||||
"github.com/zu1k/nali/pkg/zxipv6wry"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// updateCmd represents the update command
|
// updateCmd represents the update command
|
||||||
@ -17,32 +12,8 @@ var updateCmd = &cobra.Command{
|
|||||||
Short: "update chunzhen ip database",
|
Short: "update chunzhen ip database",
|
||||||
Long: `update chunzhen ip database`,
|
Long: `update chunzhen ip database`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Chunzhen ipv4
|
db.UpdateAllDB()
|
||||||
filePath := filepath.Join(constant.HomePath, "qqwry.dat")
|
|
||||||
log.Println("正在下载最新 纯真 IPv4数据库...")
|
|
||||||
_, err := qqwry.Download(filePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("下载失败", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ZX ipv6
|
|
||||||
filePath = filepath.Join(constant.HomePath, "ipv6wry.database")
|
|
||||||
log.Println("正在下载最新 ZX IPv6数据库...")
|
|
||||||
_, err = zxipv6wry.Download(filePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("下载失败", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// cdn
|
|
||||||
filePath = filepath.Join(constant.HomePath, "cdn.json")
|
|
||||||
log.Println("正在下载最新 CDN服务提供商数据库...")
|
|
||||||
_, err = cdn.Download(filePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("下载失败", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/zu1k/nali/internal/entity"
|
|
||||||
"golang.org/x/text/encoding/simplifiedchinese"
|
|
||||||
"golang.org/x/text/transform"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Root(args []string, needTransform bool) {
|
|
||||||
if len(args) == 0 {
|
|
||||||
stdin := bufio.NewScanner(os.Stdin)
|
|
||||||
for stdin.Scan() {
|
|
||||||
line := stdin.Text()
|
|
||||||
if needTransform {
|
|
||||||
line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line)
|
|
||||||
}
|
|
||||||
if line == "quit" || line == "exit" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).ColorString())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString())
|
|
||||||
}
|
|
||||||
}
|
|
@ -104,18 +104,27 @@ func GetIPDBbyName(name string) (dbif.DB, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update() {
|
func UpdateAllDB() {
|
||||||
|
log.Println("正在下载最新 纯真 IPv4数据库...")
|
||||||
_, err := qqwry.Download(QQWryPath)
|
_, err := qqwry.Download(QQWryPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Database QQWry download failed:", err)
|
log.Fatalln("数据库 QQWry 下载失败:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("正在下载最新 ZX IPv6数据库...")
|
||||||
_, err = zxipv6wry.Download(ZXIPv6WryPath)
|
_, err = zxipv6wry.Download(ZXIPv6WryPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Database ZXIPv6Wry download failed:", err)
|
log.Fatalln("数据库 ZXIPv6Wry 下载失败:", err)
|
||||||
}
|
}
|
||||||
|
_, err = ip2region.Download(Ip2RegionPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("数据库 Ip2Region 下载失败:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("正在下载最新 CDN服务提供商数据库...")
|
||||||
_, err = cdn.Download(CDNPath)
|
_, err = cdn.Download(CDNPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Database CDN download failed:", err)
|
log.Fatalln("数据库 CDN 下载失败:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,9 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"github.com/zu1k/nali/pkg/re"
|
||||||
|
|
||||||
"github.com/zu1k/nali/internal/db"
|
"github.com/zu1k/nali/internal/db"
|
||||||
"github.com/zu1k/nali/internal/re"
|
|
||||||
"github.com/zu1k/nali/pkg/dbif"
|
"github.com/zu1k/nali/pkg/dbif"
|
||||||
)
|
)
|
||||||
|
|
@ -21,7 +21,7 @@ func Download(filePath ...string) (data []byte, err error) {
|
|||||||
|
|
||||||
if len(filePath) == 1 {
|
if len(filePath) == 1 {
|
||||||
if err := common.SaveFile(filePath[0], data); err == nil {
|
if err := common.SaveFile(filePath[0], data); err == nil {
|
||||||
log.Println("已将最新的 ip2region 保存到本地:", filePath[0])
|
log.Println("已将最新的 ip2region 保存到本地:", filePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -20,7 +20,7 @@ func Download(filePath ...string) (data []byte, err error) {
|
|||||||
|
|
||||||
if len(filePath) == 1 {
|
if len(filePath) == 1 {
|
||||||
if err := common.SaveFile(filePath[0], data); err == nil {
|
if err := common.SaveFile(filePath[0], data); err == nil {
|
||||||
log.Println("已将最新的 ZX IPv6数据库 保存到本地:", filePath[0])
|
log.Println("已将最新的 ZX IPv6数据库 保存到本地:", filePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user