Skip to content

Commit cf32448

Browse files
committed
feat: Add 1pctl internationalization
1 parent 54279f4 commit cf32448

File tree

26 files changed

+1078
-1189
lines changed

26 files changed

+1078
-1189
lines changed

core/cmd/server/cmd/listen-ip.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,59 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/1Panel-dev/1Panel/core/constant"
6+
"github.com/1Panel-dev/1Panel/core/i18n"
77
"github.com/spf13/cobra"
88
)
99

1010
func init() {
11+
listenCmd.SetHelpFunc(func(c *cobra.Command, s []string) {
12+
i18n.UseI18nForCmd(language)
13+
loadListenIPHelper()
14+
})
15+
1116
RootCmd.AddCommand(listenCmd)
1217
listenCmd.AddCommand(listenIpv4Cmd)
1318
listenCmd.AddCommand(listenIpv6Cmd)
1419
}
1520

1621
var listenCmd = &cobra.Command{
17-
Use: "listen-ip",
18-
Short: "切换监听 IP",
22+
Use: "listen-ip",
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
i18n.UseI18nForCmd(language)
25+
loadListenIPHelper()
26+
return nil
27+
},
1928
}
2029

2130
var listenIpv4Cmd = &cobra.Command{
22-
Use: "ipv4",
23-
Short: "监听 IPv4",
31+
Use: "ipv4",
2432
RunE: func(cmd *cobra.Command, args []string) error {
33+
i18n.UseI18nForCmd(language)
2534
return updateBindInfo("ipv4")
2635
},
2736
}
2837
var listenIpv6Cmd = &cobra.Command{
29-
Use: "ipv6",
30-
Short: "监听 IPv6",
38+
Use: "ipv6",
3139
RunE: func(cmd *cobra.Command, args []string) error {
40+
i18n.UseI18nForCmd(language)
3241
return updateBindInfo("ipv6")
3342
},
3443
}
3544

3645
func updateBindInfo(protocol string) error {
3746
if !isRoot() {
38-
fmt.Println("请使用 sudo 1pctl listen-ip ipv6 或者切换到 root 用户")
47+
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl listen-ip ipv6"}))
3948
return nil
4049
}
4150
db, err := loadDBConn()
4251
if err != nil {
4352
return err
4453
}
45-
ipv6 := constant.StatusDisable
54+
ipv6 := "disable"
4655
tcp := "tcp4"
4756
address := "0.0.0.0"
4857
if protocol == "ipv6" {
49-
ipv6 = constant.StatusEnable
58+
ipv6 = "enable"
5059
tcp = "tcp6"
5160
address = "::"
5261
}
@@ -56,6 +65,15 @@ func updateBindInfo(protocol string) error {
5665
if err := setSettingByKey(db, "BindAddress", address); err != nil {
5766
return err
5867
}
59-
fmt.Printf("切换成功!已切换至监听 %s [%s]", tcp, address)
68+
fmt.Println(i18n.GetMsgWithMapForCmd("ListenChangeSuccessful", map[string]interface{}{"value": fmt.Sprintf(" %s [%s]", tcp, address)}))
6069
return nil
6170
}
71+
72+
func loadListenIPHelper() {
73+
fmt.Println(i18n.GetMsgByKeyForCmd("UpdateCommands"))
74+
fmt.Println("\nUsage:\n 1panel listen-ip [command]\n\nAvailable Commands:")
75+
fmt.Println("\n ipv4 " + i18n.GetMsgByKeyForCmd("ListenIPv4"))
76+
fmt.Println(" ipv6 " + i18n.GetMsgByKeyForCmd("ListenIPv6"))
77+
fmt.Println("\nFlags:\n -h, --help help for listen-ip")
78+
fmt.Println("\nUse \"1panel listen-ip [command] --help\" for more information about a command.")
79+
}

core/cmd/server/cmd/reset.go

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/1Panel-dev/1Panel/core/constant"
6+
"github.com/1Panel-dev/1Panel/core/i18n"
77
"github.com/spf13/cobra"
88
)
99

1010
func init() {
11+
resetCmd.SetHelpFunc(func(c *cobra.Command, s []string) {
12+
i18n.UseI18nForCmd(language)
13+
loadResetHelper()
14+
})
15+
1116
RootCmd.AddCommand(resetCmd)
1217
resetCmd.AddCommand(resetMFACmd)
1318
resetCmd.AddCommand(resetSSLCmd)
@@ -17,48 +22,52 @@ func init() {
1722
}
1823

1924
var resetCmd = &cobra.Command{
20-
Use: "reset",
21-
Short: "重置系统信息",
25+
Use: "reset",
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
i18n.UseI18nForCmd(language)
28+
loadResetHelper()
29+
return nil
30+
},
2231
}
2332

2433
var resetMFACmd = &cobra.Command{
25-
Use: "mfa",
26-
Short: "取消 1Panel 两步验证",
34+
Use: "mfa",
2735
RunE: func(cmd *cobra.Command, args []string) error {
36+
i18n.UseI18nForCmd(language)
2837
if !isRoot() {
29-
fmt.Println("请使用 sudo 1pctl reset mfa 或者切换到 root 用户")
38+
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset mfa"}))
3039
return nil
3140
}
3241
db, err := loadDBConn()
3342
if err != nil {
3443
return err
3544
}
3645

37-
return setSettingByKey(db, "MFAStatus", constant.StatusDisable)
46+
return setSettingByKey(db, "MFAStatus", "disable")
3847
},
3948
}
4049
var resetSSLCmd = &cobra.Command{
41-
Use: "https",
42-
Short: "取消 1Panel https 方式登录",
50+
Use: "https",
4351
RunE: func(cmd *cobra.Command, args []string) error {
52+
i18n.UseI18nForCmd(language)
4453
if !isRoot() {
45-
fmt.Println("请使用 sudo 1pctl reset https 或者切换到 root 用户")
54+
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset https"}))
4655
return nil
4756
}
4857
db, err := loadDBConn()
4958
if err != nil {
5059
return err
5160
}
5261

53-
return setSettingByKey(db, "SSL", constant.StatusDisable)
62+
return setSettingByKey(db, "SSL", "disable")
5463
},
5564
}
5665
var resetEntranceCmd = &cobra.Command{
57-
Use: "entrance",
58-
Short: "取消 1Panel 安全入口",
66+
Use: "entrance",
5967
RunE: func(cmd *cobra.Command, args []string) error {
68+
i18n.UseI18nForCmd(language)
6069
if !isRoot() {
61-
fmt.Println("请使用 sudo 1pctl reset entrance 或者切换到 root 用户")
70+
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset entrance"}))
6271
return nil
6372
}
6473
db, err := loadDBConn()
@@ -70,11 +79,11 @@ var resetEntranceCmd = &cobra.Command{
7079
},
7180
}
7281
var resetBindIpsCmd = &cobra.Command{
73-
Use: "ips",
74-
Short: "取消 1Panel 授权 IP 限制",
82+
Use: "ips",
7583
RunE: func(cmd *cobra.Command, args []string) error {
84+
i18n.UseI18nForCmd(language)
7685
if !isRoot() {
77-
fmt.Println("请使用 sudo 1pctl reset ips 或者切换到 root 用户")
86+
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset ips"}))
7887
return nil
7988
}
8089
db, err := loadDBConn()
@@ -86,11 +95,11 @@ var resetBindIpsCmd = &cobra.Command{
8695
},
8796
}
8897
var resetDomainCmd = &cobra.Command{
89-
Use: "domain",
90-
Short: "取消 1Panel 访问域名绑定",
98+
Use: "domain",
9199
RunE: func(cmd *cobra.Command, args []string) error {
100+
i18n.UseI18nForCmd(language)
92101
if !isRoot() {
93-
fmt.Println("请使用 sudo 1pctl reset domain 或者切换到 root 用户")
102+
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl reset domain"}))
94103
return nil
95104
}
96105
db, err := loadDBConn()
@@ -101,3 +110,15 @@ var resetDomainCmd = &cobra.Command{
101110
return setSettingByKey(db, "BindDomain", "")
102111
},
103112
}
113+
114+
func loadResetHelper() {
115+
fmt.Println(i18n.GetMsgByKeyForCmd("ResetCommands"))
116+
fmt.Println("\nUsage:\n 1panel reset [command]\n\nAvailable Commands:")
117+
fmt.Println("\n domain " + i18n.GetMsgByKeyForCmd("ResetDomain"))
118+
fmt.Println(" entrance " + i18n.GetMsgByKeyForCmd("ResetEntrance"))
119+
fmt.Println(" https " + i18n.GetMsgByKeyForCmd("ResetHttps"))
120+
fmt.Println(" ips " + i18n.GetMsgByKeyForCmd("ResetIPs"))
121+
fmt.Println(" mfa " + i18n.GetMsgByKeyForCmd("ResetMFA"))
122+
fmt.Println("\nFlags:\n -h, --help help for reset")
123+
fmt.Println("\nUse \"1panel reset [command] --help\" for more information about a command.")
124+
}

core/cmd/server/cmd/restore.go

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import (
66
"path"
77
"sort"
88
"strings"
9+
"time"
910

11+
"github.com/1Panel-dev/1Panel/core/global"
12+
"github.com/1Panel-dev/1Panel/core/i18n"
1013
cmdUtils "github.com/1Panel-dev/1Panel/core/utils/cmd"
1114
"github.com/1Panel-dev/1Panel/core/utils/files"
15+
"github.com/pkg/errors"
1216

1317
"github.com/spf13/cobra"
1418
)
@@ -18,11 +22,11 @@ func init() {
1822
}
1923

2024
var restoreCmd = &cobra.Command{
21-
Use: "restore",
22-
Short: "回滚 1Panel 服务及数据",
25+
Use: "restore",
2326
RunE: func(cmd *cobra.Command, args []string) error {
27+
i18n.UseI18nForCmd(language)
2428
if !isRoot() {
25-
fmt.Println("请使用 sudo 1pctl restore 或者切换到 root 用户")
29+
fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl restore"}))
2630
return nil
2731
}
2832
stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/local/bin/1pctl | cut -d'=' -f2")
@@ -36,40 +40,56 @@ var restoreCmd = &cobra.Command{
3640
if err != nil {
3741
return err
3842
}
39-
if tmpPath == "暂无可回滚文件" {
40-
fmt.Println("暂无可回滚文件")
43+
if tmpPath == "no such file" {
44+
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreNoSuchFile"))
4145
return nil
4246
}
4347
tmpPath = path.Join(upgradeDir, tmpPath, "original")
44-
fmt.Printf("(0/4) 开始从 %s 目录回滚 1Panel 服务及数据... \n", tmpPath)
48+
fmt.Println(i18n.GetMsgWithMapForCmd("RestoreStep1", map[string]interface{}{"name": tmpPath}))
4549

46-
if err := files.CopyItem(false, true, path.Join(tmpPath, "1panel*"), "/usr/local/bin"); err != nil {
50+
if err := files.CopyFile(path.Join(tmpPath, "1panel"), "/usr/local/bin", true); err != nil {
4751
return err
4852
}
49-
fmt.Println("(1/4) 1panel 二进制回滚成功")
50-
if err := files.CopyItem(false, true, path.Join(tmpPath, "1pctl"), "/usr/local/bin"); err != nil {
53+
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep2"))
54+
if err := files.CopyFile(path.Join(tmpPath, "1pctl"), "/usr/local/bin", true); err != nil {
5155
return err
5256
}
53-
fmt.Println("(2/4) 1panel 脚本回滚成功")
54-
if err := files.CopyItem(false, true, path.Join(tmpPath, "1panel*.service"), "/etc/systemd/system"); err != nil {
57+
_, _ = cmdUtils.Execf("cp -r %s /usr/local/bin", path.Join(tmpPath, "lang"))
58+
geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo")
59+
_, _ = cmdUtils.Execf("mkdir %s && cp %s %s/", geoPath, path.Join(tmpPath, "GeoIP.mmdb"), geoPath)
60+
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep3"))
61+
if err := files.CopyFile(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system", true); err != nil {
5562
return err
5663
}
57-
fmt.Println("(3/4) 1panel 服务回滚成功")
58-
if _, err := os.Stat(path.Join(tmpPath, "core.db")); err == nil {
59-
if err := files.CopyItem(true, true, path.Join(tmpPath, "db"), path.Join(baseDir, "1panel")); err != nil {
64+
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep4"))
65+
checkPointOfWal()
66+
if _, err := os.Stat(path.Join(tmpPath, "1Panel.db")); err == nil {
67+
if err := files.CopyFile(path.Join(tmpPath, "1Panel.db"), path.Join(baseDir, "1panel/db"), true); err != nil {
6068
return err
6169
}
6270
}
63-
fmt.Printf("(4/4) 1panel 数据回滚成功 \n\n")
64-
65-
fmt.Println("回滚成功!正在重启服务,请稍候...")
71+
if _, err := os.Stat(path.Join(tmpPath, "db.tar.gz")); err == nil {
72+
if err := handleUnTar(path.Join(tmpPath, "db.tar.gz"), path.Join(baseDir, "1panel")); err != nil {
73+
return err
74+
}
75+
}
76+
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep5"))
77+
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreSuccessful"))
6678
return nil
6779
},
6880
}
6981

82+
func checkPointOfWal() {
83+
db, err := loadDBConn()
84+
if err != nil {
85+
return
86+
}
87+
_ = db.Exec("PRAGMA wal_checkpoint(TRUNCATE);").Error
88+
}
89+
7090
func loadRestorePath(upgradeDir string) (string, error) {
7191
if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) {
72-
return "暂无可回滚文件", nil
92+
return "no such file", nil
7393
}
7494
files, err := os.ReadDir(upgradeDir)
7595
if err != nil {
@@ -82,10 +102,25 @@ func loadRestorePath(upgradeDir string) (string, error) {
82102
}
83103
}
84104
if len(folders) == 0 {
85-
return "暂无可回滚文件", nil
105+
return "no such file", nil
86106
}
87107
sort.Slice(folders, func(i, j int) bool {
88108
return folders[i] > folders[j]
89109
})
90110
return folders[0], nil
91111
}
112+
113+
func handleUnTar(sourceFile, targetDir string) error {
114+
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
115+
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {
116+
return err
117+
}
118+
}
119+
120+
commands := fmt.Sprintf("tar zxvfC %s %s", sourceFile, targetDir)
121+
stdout, err := cmdUtils.ExecWithTimeOut(commands, 20*time.Second)
122+
if err != nil {
123+
return errors.New(stdout)
124+
}
125+
return nil
126+
}

core/cmd/server/cmd/root.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"os/user"
6-
"path"
76
"strings"
87
"time"
98

@@ -14,11 +13,14 @@ import (
1413
"gorm.io/gorm"
1514
)
1615

17-
func init() {}
16+
var language string
17+
18+
func init() {
19+
RootCmd.PersistentFlags().StringVarP(&language, "language", "l", "en", "Set the language")
20+
}
1821

1922
var RootCmd = &cobra.Command{
20-
Use: "1panel",
21-
Short: "1Panel ,一款现代化的 Linux 面板",
23+
Use: "1panel",
2224
RunE: func(cmd *cobra.Command, args []string) error {
2325
server.Start()
2426
return nil
@@ -29,9 +31,9 @@ type setting struct {
2931
ID uint `gorm:"primarykey;AUTO_INCREMENT" json:"id"`
3032
CreatedAt time.Time `json:"createdAt"`
3133
UpdatedAt time.Time `json:"updatedAt"`
32-
Key string `json:"key" gorm:"not null;"`
33-
Value string `json:"value"`
34-
About string `json:"about"`
34+
Key string `json:"key" gorm:"type:varchar(256);not null;"`
35+
Value string `json:"value" gorm:"type:varchar(256)"`
36+
About string `json:"about" gorm:"type:longText"`
3537
}
3638

3739
func loadDBConn() (*gorm.DB, error) {
@@ -43,7 +45,11 @@ func loadDBConn() (*gorm.DB, error) {
4345
if len(baseDir) == 0 {
4446
return nil, fmt.Errorf("error `BASE_DIR` find in /usr/local/bin/1pctl \n")
4547
}
46-
db, err := gorm.Open(sqlite.Open(path.Join(baseDir, "/1panel/db/core.db")), &gorm.Config{})
48+
if strings.HasSuffix(baseDir, "/") {
49+
baseDir = baseDir[:strings.LastIndex(baseDir, "/")]
50+
}
51+
52+
db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{})
4753
if err != nil {
4854
return nil, fmt.Errorf("init my db conn failed, err: %v \n", err)
4955
}

0 commit comments

Comments
 (0)