Skip to content

Commit a4c1235

Browse files
committed
use regex match authorized_keys on doctor
1 parent eb462b1 commit a4c1235

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

cmd/doctor.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415

1516
"code.gitea.io/gitea/modules/setting"
@@ -32,14 +33,18 @@ type check struct {
3233

3334
var checklist = []check{
3435
{
35-
title: "Check if openssh authorized_keys file correct",
36+
title: "Check if openssh authorized_keys file id correct",
3637
f: runDoctorLocationMoved,
3738
},
3839
}
3940

4041
func runDoctor(ctx *cli.Context) error {
41-
if err := initDB(); err != nil {
42-
return err
42+
err := initDB()
43+
fmt.Println("Using app.ini at ", setting.CustomConf)
44+
if err != nil {
45+
fmt.Println(err)
46+
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
47+
return nil
4348
}
4449

4550
for i, check := range checklist {
@@ -90,28 +95,28 @@ func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) {
9095

9196
// command="/Volumes/data/Projects/gitea/gitea/gitea --config
9297
if len(firstline) > 0 {
93-
var start, end int
94-
for i, c := range firstline {
95-
if c == ' ' {
96-
end = i
97-
break
98-
} else if c == '"' {
99-
start = i + 1
100-
}
98+
exp := regexp.MustCompile(`^[ \t]*(?:command=")([^ ]+) --config='([^']+)' serv key-([^"]+)",(?:[^ ]+) ssh-rsa ([^ ]+) ([^ ]+)[ \t]*$`)
99+
100+
// command="/home/user/gitea --config='/home/user/etc/app.ini' serv key-999",option-1,option-2,option-n ssh-rsa public-key-value key-name
101+
res := exp.FindAllStringSubmatch(firstline, -1)
102+
103+
giteaPath := res[1] // => /home/user/gitea
104+
iniPath := res[2] // => /home/user/etc/app.ini
105+
106+
p, err := exePath()
107+
if err != nil {
108+
return nil, err
109+
}
110+
p, err = filepath.Abs(p)
111+
if err != nil {
112+
return nil, err
101113
}
102-
if start > 0 && end > 0 {
103-
p, err := exePath()
104-
if err != nil {
105-
return nil, err
106-
}
107-
p, err = filepath.Abs(p)
108-
if err != nil {
109-
return nil, err
110-
}
111114

112-
if firstline[start:end] != p {
113-
return []string{fmt.Sprintf("Wants %s but %s on %s", p, firstline[start:end], fPath)}, nil
114-
}
115+
if len(giteaPath) > 0 && giteaPath[0] != p {
116+
return []string{fmt.Sprintf("Gitea exe path wants %s but %s on %s", p, giteaPath[0], fPath)}, nil
117+
}
118+
if len(iniPath) > 0 && iniPath[0] != setting.CustomConf {
119+
return []string{fmt.Sprintf("Gitea config path wants %s but %s on %s", setting.CustomConf, iniPath[0], fPath)}, nil
115120
}
116121
}
117122

0 commit comments

Comments
 (0)