utils.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package conf
  5. import (
  6. "path/filepath"
  7. "strings"
  8. "github.com/pkg/errors"
  9. "gogs.io/gogs/internal/process"
  10. )
  11. // openSSHVersion returns string representation of OpenSSH version via command "ssh -V".
  12. func openSSHVersion() (string, error) {
  13. // NOTE: Somehow the version is printed to stderr.
  14. _, stderr, err := process.Exec("conf.openSSHVersion", "ssh", "-V")
  15. if err != nil {
  16. return "", errors.Wrap(err, stderr)
  17. }
  18. // Trim unused information, see https://github.com/gogs/gogs/issues/4507#issuecomment-305150441.
  19. v := strings.TrimRight(strings.Fields(stderr)[0], ",1234567890")
  20. v = strings.TrimSuffix(strings.TrimPrefix(v, "OpenSSH_"), "p")
  21. return v, nil
  22. }
  23. // ensureAbs prepends the WorkDir to the given path if it is not an absolute path.
  24. func ensureAbs(path string) string {
  25. if filepath.IsAbs(path) {
  26. return path
  27. }
  28. return filepath.Join(WorkDir(), path)
  29. }