import.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2016 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 cmd
  5. import (
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "time"
  10. "github.com/Unknwon/com"
  11. "github.com/urfave/cli"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. var (
  15. CmdImport = 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/conf/app.ini", "Custom configuration file path"),
  32. },
  33. }
  34. )
  35. func runImportLocale(c *cli.Context) error {
  36. if !c.IsSet("source") {
  37. return fmt.Errorf("Source directory is not specified")
  38. } else if !c.IsSet("target") {
  39. return fmt.Errorf("Target directory is not specified")
  40. }
  41. if !com.IsDir(c.String("source")) {
  42. return fmt.Errorf("Source directory does not exist or is not a directory")
  43. } else if !com.IsDir(c.String("target")) {
  44. return fmt.Errorf("Target directory does not exist or is not a directory")
  45. }
  46. if c.IsSet("config") {
  47. setting.CustomConf = c.String("config")
  48. }
  49. setting.NewContext()
  50. now := time.Now()
  51. // Cut out en-US.
  52. for _, lang := range setting.Langs[1:] {
  53. name := fmt.Sprintf("locale_%s.ini", lang)
  54. source := filepath.Join(c.String("source"), name)
  55. target := filepath.Join(c.String("target"), name)
  56. if !com.IsFile(source) {
  57. continue
  58. }
  59. if err := com.Copy(source, target); err != nil {
  60. return fmt.Errorf("Copy file: %v", err)
  61. }
  62. // Modification time of files from Crowdin often ahead of current,
  63. // so we need to set back to current.
  64. os.Chtimes(target, now, now)
  65. }
  66. fmt.Println("Locale files has been successfully imported!")
  67. return nil
  68. }