import.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package cmd
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "time"
  9. "github.com/cockroachdb/errors"
  10. "github.com/unknwon/com"
  11. "github.com/urfave/cli"
  12. "gogs.io/gogs/internal/conf"
  13. )
  14. var (
  15. Import = cli.Command{
  16. Name: "import",
  17. Usage: "Import portable data as local Gogs data",
  18. Description: `Allow user import data from other Gogs installations to local instance
  19. without manually hacking the data files`,
  20. Subcommands: []cli.Command{
  21. subcmdImportLocale,
  22. },
  23. }
  24. subcmdImportLocale = cli.Command{
  25. Name: "locale",
  26. Usage: "Import locale files to local repository",
  27. Action: runImportLocale,
  28. Flags: []cli.Flag{
  29. stringFlag("source", "", "Source directory that stores new locale files"),
  30. stringFlag("target", "", "Target directory that stores old locale files"),
  31. stringFlag("config, c", "", "Custom configuration file path"),
  32. },
  33. }
  34. )
  35. func runImportLocale(c *cli.Context) error {
  36. if !c.IsSet("source") {
  37. return errors.New("source directory is not specified")
  38. } else if !c.IsSet("target") {
  39. return errors.New("target directory is not specified")
  40. }
  41. if !com.IsDir(c.String("source")) {
  42. return errors.Newf("source directory %q does not exist or is not a directory", c.String("source"))
  43. } else if !com.IsDir(c.String("target")) {
  44. return errors.Newf("target directory %q does not exist or is not a directory", c.String("target"))
  45. }
  46. err := conf.Init(c.String("config"))
  47. if err != nil {
  48. return errors.Wrap(err, "init configuration")
  49. }
  50. now := time.Now()
  51. var line []byte
  52. badChars := []byte(`="`)
  53. escapedQuotes := []byte(`\"`)
  54. regularQuotes := []byte(`"`)
  55. // Cut out en-US.
  56. for _, lang := range conf.I18n.Langs[1:] {
  57. name := fmt.Sprintf("locale_%s.ini", lang)
  58. source := filepath.Join(c.String("source"), name)
  59. target := filepath.Join(c.String("target"), name)
  60. if !com.IsFile(source) {
  61. continue
  62. }
  63. // Crowdin surrounds double quotes for strings contain quotes inside,
  64. // this breaks INI parser, we need to fix that.
  65. sr, err := os.Open(source)
  66. if err != nil {
  67. return errors.Newf("open: %v", err)
  68. }
  69. tw, err := os.Create(target)
  70. if err != nil {
  71. return errors.Newf("create: %v", err)
  72. }
  73. scanner := bufio.NewScanner(sr)
  74. for scanner.Scan() {
  75. line = scanner.Bytes()
  76. idx := bytes.Index(line, badChars)
  77. if idx > -1 && line[len(line)-1] == '"' {
  78. // We still want the "=" sign
  79. line = append(line[:idx+1], line[idx+2:len(line)-1]...)
  80. line = bytes.ReplaceAll(line, escapedQuotes, regularQuotes)
  81. }
  82. _, _ = tw.Write(line)
  83. _, _ = tw.WriteString("\n")
  84. }
  85. _ = sr.Close()
  86. _ = tw.Close()
  87. // Modification time of files from Crowdin often ahead of current,
  88. // so we need to set back to current.
  89. _ = os.Chtimes(target, now, now)
  90. }
  91. fmt.Println("Locale files has been successfully imported!")
  92. return nil
  93. }