Skip to content

Commit

Permalink
change the way to detect os info; fixed null pointer error on --edit-…
Browse files Browse the repository at this point in the history
…config;
  • Loading branch information
Karmenzind committed Jun 12, 2024
1 parent 500bafb commit aa8298b
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
在终端中执行:

```bash
sh -c "$(curl -fsSL https://raw.githubusercontent.com/Karmenzind/kd/master/scripts/install.sh)"
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Karmenzind/kd/master/scripts/install.sh)"
```

> 如果raw.githubusercontent.com被屏蔽,改用`git clone https://github.com/Karmenzind/kd && bash kd/install.sh`
Expand Down
14 changes: 12 additions & 2 deletions cmd/kd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func flagRestart(*cli.Context, bool) error {

func flagUpdate(ctx *cli.Context, _ bool) (err error) {
var ver string
if runtime.GOOS == "linux" && pkg.GetLinuxDistro() == "arch" {
if runtime.GOOS == "linux" && run.Info.GetOSInfo().Distro == "arch" {
d.EchoFine("您在使用ArchLinux,推荐直接通过AUR安装/升级(例如`yay -S kd`),更便于维护")
}
force := ctx.Bool("force")
Expand Down Expand Up @@ -160,6 +160,16 @@ func flagEditConfig(*cli.Context, bool) error {
break
}
}
for _, k := range []string{"nano", "vi", "vim"} {
d.EchoRun("未找到EDITOR或VISUAL环境变量,尝试启动编辑器%s", k)
if pkg.CommandExists(k) {
cmd = exec.Command(k, p)
break
}
}
if cmd == nil {
return fmt.Errorf("未找到nano或vim,请安装至少一种,或者指定环境变量EDITOR/VISUAL")
}
case "windows":
cmd = exec.Command("notepad", p)
case "darwin":
Expand Down Expand Up @@ -190,7 +200,7 @@ func checkAndNoticeUpdate() {
if ltag := update.GetCachedLatestTag(); ltag != "" {
if update.CompareVersions(ltag, VERSION) == 1 {
prompt := fmt.Sprintf("发现新版本%s,请执行`kd --update`更新", ltag)
if pkg.GetLinuxDistro() == "arch" {
if run.Info.GetOSInfo().Distro == "arch" {
prompt += "。ArchLinux推荐通过AUR安装/升级"
}
d.EchoWeakNotice(prompt)
Expand Down
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"strings"

"github.com/Karmenzind/kd/internal/run"
"github.com/Karmenzind/kd/pkg/str"
"github.com/jinzhu/configor"
)
Expand All @@ -18,7 +19,7 @@ var CONFIG_PATH string
type LoggerConfig struct {
Enable bool `default:"true" toml:"enable"`
Path string `toml:"path"`
Level string `default:"warn" toml:"level"`
Level string `default:"info" toml:"level"`
Stderr bool `default:"false" toml:"stderr"`
RedirectToStream bool `default:"false" toml:"redirect_to_stream"`
}
Expand Down Expand Up @@ -99,6 +100,10 @@ func parseConfig() (err error) {
switch runtime.GOOS {
case "darwin": //MacOS
Cfg.Paging = false
case "linux":
if run.Info.GetOSInfo().IsDebianBased {
Cfg.Paging = false
}
}
}
return err
Expand Down
9 changes: 7 additions & 2 deletions internal/daemon/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func cronEnsureDaemonJsonFile() {
needUpdate = true
} else {
ri := run.Info
run.Info.SetOSInfo()
if !(ri.StartTime == di.StartTime &&
ri.PID == di.PID &&
ri.Port == di.Port &&
Expand Down Expand Up @@ -129,7 +130,7 @@ func cronUpdateDataZip() {
fmt.Println(":)")
}
}
zap.S().Debugf("Need to dl: %s", need2Dl)
zap.S().Debugf("Need to dl: %v", need2Dl)
var err error
// if need to download
if need2Dl {
Expand All @@ -142,7 +143,11 @@ func cronUpdateDataZip() {
}
err = decompressDBZip(tempDBPath, zipPath)
if err != nil {
zap.S().Warnf("Failed: %s", err)
zap.S().Warnf("Failed: %s. Current invalid file will be removed.", err)
errDel := os.Remove(zipPath)
if errDel != nil {
zap.S().Warnf("Failed to remove file: %s", err)
}
continue
}
zap.S().Infof("Decompressed DB zip file -> %s", tempDBPath)
Expand Down
17 changes: 17 additions & 0 deletions internal/model/others.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type RunInfo struct {
ExePath string
Version string

OS *pkg.OSInfo

isServer bool
termHeight int
termWidth int
Expand All @@ -31,6 +33,21 @@ func (r *RunInfo) SetPort(v string) {
r.Port = v
}

func (r *RunInfo) SetOSInfo() {
var err error
r.OS, err = pkg.GetOSInfo()
if err != nil {
zap.S().Warn("Failed to fetch os info: %s", err)
}
}

func (r *RunInfo) GetOSInfo() *pkg.OSInfo {
if r.OS == nil {
r.SetOSInfo()
}
return r.OS
}

func (r *RunInfo) GetTermSize() (int, int, error) {
if r.termHeight > 0 && r.termWidth > 0 {
return r.termWidth, r.termHeight, nil
Expand Down
3 changes: 3 additions & 0 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func StartServer() (err error) {
return err
}
run.Info.SetPort(port)
run.Info.SetOSInfo()

go run.Info.SaveToFile(filepath.Join(run.CACHE_RUN_PATH, "daemon.json"))

d.EchoOkay("Listening on host: %s, port: %s\n", host, port)
zap.S().Info("Started kd server")

Expand Down
1 change: 0 additions & 1 deletion internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal

import "github.com/Karmenzind/kd/internal/model"

var IS_SERVER = false

func buildResult(q string, ilt bool) *model.Result {
return &model.Result{
Expand Down
39 changes: 39 additions & 0 deletions pkg/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"strings"
)

type OSInfo struct {
Distro string
IsDebianBased bool
}

func GetLinuxDistro() string {
file, err := os.Open("/etc/os-release")
if err != nil {
Expand All @@ -22,3 +27,37 @@ func GetLinuxDistro() string {
}
return ""
}

func GetOSInfo() (*OSInfo, error) {
var ret = &OSInfo{}
// Open the os-release file
file, err := os.Open("/etc/os-release")
if err != nil {
return ret, err
}
defer file.Close()

// Read the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()

// Check for ID or ID_LIKE that indicates Debian
if strings.HasPrefix(line, "ID=") || strings.HasPrefix(line, "ID_LIKE=") {
if strings.HasPrefix(line, "ID=") {
splited := strings.SplitN(line, "=", 2)
ret.Distro = splited[1]
}

if strings.Contains(line, "debian") {
ret.IsDebianBased = true
}
}
}

if err := scanner.Err(); err != nil {
return ret, err
}

return ret, nil
}
2 changes: 2 additions & 0 deletions plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- gitea镜像

## short-term
- edit时候文件不存在,自动创建
- 重启改为signal
- 多source直接嵌套进列表
- 记录pid/port,先检查这两个
Expand Down Expand Up @@ -37,6 +38,7 @@
# BUG

- `--status` shows port and pid of dead daemon
- disable pager on Debian/MacOS

## Risk
- 实际文件名 不改的时候的process_name,增加同时校验kd和当前文件名
Expand Down
10 changes: 5 additions & 5 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ do_build() {
local cgo=1 cc=

if [[ $targetfile == "" ]] && [[ $os != "" ]] && [[ $arch != "" ]]; then
echo
echo
echo "≫ Building for $os $arch..."

local filename=kd_${os}_${arch}
Expand All @@ -43,10 +43,10 @@ do_build() {
;;
esac

set -x
set -x
GOOS=$os GOARCH=$arch CGO_ENABLED=$cgo CC=$cc go build ${buildopts} -o ${targetfile} -ldflags="-s -w" -tags urfave_cli_no_docs cmd/kd.go
local ret=$?
set +x
local ret=$?
set +x

if (($ret == 0)); then
echo " [✔] Finished -> ${targetfile}"
Expand Down Expand Up @@ -76,7 +76,7 @@ case $1 in
do_build '' '' /usr/bin/kd
exit
;;
-a)
-a | --all)
build_all
;;
*)
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ if (($? != 0)); then
exit 1
fi

INST_PATH=/usr/local/bin/kd
INST_PATH=/usr/bin/kd

echo "[✔] 已经下载完成,文件临时保存位置:${TEMP_PATH}"
if [[ $(whoami) == "root" ]]; then
Expand Down

0 comments on commit aa8298b

Please sign in to comment.