Skip to content

Commit

Permalink
Add a new command doctor to check if some wrong configurations on git…
Browse files Browse the repository at this point in the history
…ea instance
  • Loading branch information
lunny committed Jan 11, 2020
1 parent 23a288f commit 24b0a0c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
95 changes: 91 additions & 4 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
package cmd

import (
"github.com/urfave/cli"
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"code.gitea.io/gitea/modules/setting"
"github.com/urfave/cli"
)

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

type check struct {
title string
f func(ctx *cli.Context) ([]string, error)
}

var checklist = []check{
{
title: "Check if openssh authorized_keys file correct",
f: runDoctorLocationMoved,
},
}

func runDoctor(ctx *cli.Context) error {
if err := initDB(); err != nil {
return err
}

runDoctorLocationMoved(ctx)
for i, check := range checklist {
fmt.Println("[", i+1, "]", check.title)
fmt.Println()
if messages, err := check.f(ctx); err != nil {
fmt.Println("Error:", err)
} else if len(messages) > 0 {
for _, message := range messages {
fmt.Println("-", message)
}
} else {
fmt.Println("OK.")
}
fmt.Println()
}
return nil
}

func exePath() (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
return filepath.Abs(file)
}

func runDoctorLocationMoved(ctx *cliContext) {
setting.RepoRootPath
func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) {
if setting.SSH.StartBuiltinServer {
return nil, nil
}

fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
f, err := os.Open(fPath)
if err != nil {
return nil, err
}
defer f.Close()

var firstline string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
firstline = scanner.Text()
if !strings.HasPrefix(firstline, "#") {
break
}
}

// command="/Volumes/data/Projects/gitea/gitea/gitea --config
if len(firstline) > 0 {
var start, end int
for i, c := range firstline {
if c == ' ' {
end = i
break
} else if c == '"' {
start = i + 1
}
}
if start > 0 && end > 0 {
p, err := exePath()
if err != nil {
return nil, err
}
p, err = filepath.Abs(p)
if err != nil {
return nil, err
}

if firstline[start:end] != p {
return []string{fmt.Sprintf("Wants %s but %s on %s", p, firstline[start:end], fPath)}, nil
}
}
}

return nil, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ arguments - which can alternatively be run by running the subcommand web.`
cmd.CmdMigrate,
cmd.CmdKeys,
cmd.CmdConvert,
cmd.CmdDoctor,
}
// Now adjust these commands to add our global configuration options

Expand Down

0 comments on commit 24b0a0c

Please sign in to comment.