Skip to content

Commit 24b0a0c

Browse files
committed
Add a new command doctor to check if some wrong configurations on gitea instance
1 parent 23a288f commit 24b0a0c

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

cmd/doctor.go

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
package cmd
66

77
import (
8-
"github.com/urfave/cli"
8+
"bufio"
9+
"fmt"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
13+
"strings"
914

1015
"code.gitea.io/gitea/modules/setting"
16+
"github.com/urfave/cli"
1117
)
1218

1319
// CmdDoctor represents the available doctor sub-command.
@@ -18,14 +24,95 @@ var CmdDoctor = cli.Command{
1824
Action: runDoctor,
1925
}
2026

27+
type check struct {
28+
title string
29+
f func(ctx *cli.Context) ([]string, error)
30+
}
31+
32+
var checklist = []check{
33+
{
34+
title: "Check if openssh authorized_keys file correct",
35+
f: runDoctorLocationMoved,
36+
},
37+
}
38+
2139
func runDoctor(ctx *cli.Context) error {
2240
if err := initDB(); err != nil {
2341
return err
2442
}
2543

26-
runDoctorLocationMoved(ctx)
44+
for i, check := range checklist {
45+
fmt.Println("[", i+1, "]", check.title)
46+
fmt.Println()
47+
if messages, err := check.f(ctx); err != nil {
48+
fmt.Println("Error:", err)
49+
} else if len(messages) > 0 {
50+
for _, message := range messages {
51+
fmt.Println("-", message)
52+
}
53+
} else {
54+
fmt.Println("OK.")
55+
}
56+
fmt.Println()
57+
}
58+
return nil
59+
}
60+
61+
func exePath() (string, error) {
62+
file, err := exec.LookPath(os.Args[0])
63+
if err != nil {
64+
return "", err
65+
}
66+
return filepath.Abs(file)
2767
}
2868

29-
func runDoctorLocationMoved(ctx *cliContext) {
30-
setting.RepoRootPath
69+
func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) {
70+
if setting.SSH.StartBuiltinServer {
71+
return nil, nil
72+
}
73+
74+
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
75+
f, err := os.Open(fPath)
76+
if err != nil {
77+
return nil, err
78+
}
79+
defer f.Close()
80+
81+
var firstline string
82+
scanner := bufio.NewScanner(f)
83+
for scanner.Scan() {
84+
firstline = scanner.Text()
85+
if !strings.HasPrefix(firstline, "#") {
86+
break
87+
}
88+
}
89+
90+
// command="/Volumes/data/Projects/gitea/gitea/gitea --config
91+
if len(firstline) > 0 {
92+
var start, end int
93+
for i, c := range firstline {
94+
if c == ' ' {
95+
end = i
96+
break
97+
} else if c == '"' {
98+
start = i + 1
99+
}
100+
}
101+
if start > 0 && end > 0 {
102+
p, err := exePath()
103+
if err != nil {
104+
return nil, err
105+
}
106+
p, err = filepath.Abs(p)
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
if firstline[start:end] != p {
112+
return []string{fmt.Sprintf("Wants %s but %s on %s", p, firstline[start:end], fPath)}, nil
113+
}
114+
}
115+
}
116+
117+
return nil, nil
31118
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ arguments - which can alternatively be run by running the subcommand web.`
6868
cmd.CmdMigrate,
6969
cmd.CmdKeys,
7070
cmd.CmdConvert,
71+
cmd.CmdDoctor,
7172
}
7273
// Now adjust these commands to add our global configuration options
7374

0 commit comments

Comments
 (0)