5
5
package cmd
6
6
7
7
import (
8
- "github.com/urfave/cli"
8
+ "bufio"
9
+ "fmt"
10
+ "os"
11
+ "os/exec"
12
+ "path/filepath"
13
+ "strings"
9
14
10
15
"code.gitea.io/gitea/modules/setting"
16
+ "github.com/urfave/cli"
11
17
)
12
18
13
19
// CmdDoctor represents the available doctor sub-command.
@@ -18,14 +24,95 @@ var CmdDoctor = cli.Command{
18
24
Action : runDoctor ,
19
25
}
20
26
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
+
21
39
func runDoctor (ctx * cli.Context ) error {
22
40
if err := initDB (); err != nil {
23
41
return err
24
42
}
25
43
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 )
27
67
}
28
68
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
31
118
}
0 commit comments