utils.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package conf
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "github.com/cockroachdb/errors"
  6. "gogs.io/gogs/internal/osutil"
  7. "gogs.io/gogs/internal/process"
  8. )
  9. // cleanUpOpenSSHVersion cleans up the raw output of "ssh -V" and returns a clean version string.
  10. func cleanUpOpenSSHVersion(raw string) string {
  11. v := strings.TrimRight(strings.Fields(raw)[0], ",1234567890")
  12. v = strings.TrimSuffix(strings.TrimPrefix(v, "OpenSSH_"), "p")
  13. return v
  14. }
  15. // openSSHVersion returns string representation of OpenSSH version via command "ssh -V".
  16. func openSSHVersion() (string, error) {
  17. // NOTE: Somehow the version is printed to stderr.
  18. _, stderr, err := process.Exec("conf.openSSHVersion", "ssh", "-V")
  19. if err != nil {
  20. return "", errors.Wrap(err, stderr)
  21. }
  22. return cleanUpOpenSSHVersion(stderr), nil
  23. }
  24. // ensureAbs prepends the WorkDir to the given path if it is not an absolute path.
  25. func ensureAbs(path string) string {
  26. if filepath.IsAbs(path) {
  27. return path
  28. }
  29. return filepath.Join(WorkDir(), path)
  30. }
  31. // CheckRunUser returns false if configured run user does not match actual user that
  32. // runs the app. The first return value is the actual user name. This check is ignored
  33. // under Windows since SSH remote login is not the main method to login on Windows.
  34. func CheckRunUser(runUser string) (string, bool) {
  35. if IsWindowsRuntime() {
  36. return "", true
  37. }
  38. currentUser := osutil.CurrentUsername()
  39. return currentUser, runUser == currentUser
  40. }