Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new command doctor to check if some wrong configurations on gitea instance #9095

Merged
merged 5 commits into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a new command doctor to check if some wrong configurations on git…
…ea instance
  • Loading branch information
lunny committed Jan 11, 2020
commit 24b0a0cfab3a446144607c4382b0730e37e41001
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",
lunny marked this conversation as resolved.
Show resolved Hide resolved
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 {
lunny marked this conversation as resolved.
Show resolved Hide resolved
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