diff options
author | Unknwon <u@gogs.io> | 2019-10-24 01:51:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-24 01:51:46 -0700 |
commit | 01c8df01ec0608f1f25b2f1444adabb98fa5ee8a (patch) | |
tree | f8a7e5dd8d2a8c51e1ce2cabb9d33571a93314dd /internal/cmd/import.go | |
parent | 613139e7bef81d3573e7988a47eb6765f3de347a (diff) |
internal: move packages under this directory (#5836)
* Rename pkg -> internal
* Rename routes -> route
* Move route -> internal/route
* Rename models -> db
* Move db -> internal/db
* Fix route2 -> route
* Move cmd -> internal/cmd
* Bump version
Diffstat (limited to 'internal/cmd/import.go')
-rw-r--r-- | internal/cmd/import.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/internal/cmd/import.go b/internal/cmd/import.go new file mode 100644 index 00000000..4ee43b5b --- /dev/null +++ b/internal/cmd/import.go @@ -0,0 +1,113 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "bufio" + "bytes" + "fmt" + "os" + "path/filepath" + "time" + + "github.com/unknwon/com" + "github.com/urfave/cli" + + "gogs.io/gogs/internal/setting" +) + +var ( + Import = cli.Command{ + Name: "import", + Usage: "Import portable data as local Gogs data", + Description: `Allow user import data from other Gogs installations to local instance +without manually hacking the data files`, + Subcommands: []cli.Command{ + subcmdImportLocale, + }, + } + + subcmdImportLocale = cli.Command{ + Name: "locale", + Usage: "Import locale files to local repository", + Action: runImportLocale, + Flags: []cli.Flag{ + stringFlag("source", "", "Source directory that stores new locale files"), + stringFlag("target", "", "Target directory that stores old locale files"), + stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"), + }, + } +) + +func runImportLocale(c *cli.Context) error { + if !c.IsSet("source") { + return fmt.Errorf("Source directory is not specified") + } else if !c.IsSet("target") { + return fmt.Errorf("Target directory is not specified") + } + if !com.IsDir(c.String("source")) { + return fmt.Errorf("Source directory does not exist or is not a directory") + } else if !com.IsDir(c.String("target")) { + return fmt.Errorf("Target directory does not exist or is not a directory") + } + + if c.IsSet("config") { + setting.CustomConf = c.String("config") + } + + setting.NewContext() + + now := time.Now() + + line := make([]byte, 0, 100) + badChars := []byte(`="`) + escapedQuotes := []byte(`\"`) + regularQuotes := []byte(`"`) + // Cut out en-US. + for _, lang := range setting.Langs[1:] { + name := fmt.Sprintf("locale_%s.ini", lang) + source := filepath.Join(c.String("source"), name) + target := filepath.Join(c.String("target"), name) + if !com.IsFile(source) { + continue + } + + // Crowdin surrounds double quotes for strings contain quotes inside, + // this breaks INI parser, we need to fix that. + sr, err := os.Open(source) + if err != nil { + return fmt.Errorf("Open: %v", err) + } + + tw, err := os.Create(target) + if err != nil { + if err != nil { + return fmt.Errorf("Open: %v", err) + } + } + + scanner := bufio.NewScanner(sr) + for scanner.Scan() { + line = scanner.Bytes() + idx := bytes.Index(line, badChars) + if idx > -1 && line[len(line)-1] == '"' { + // We still want the "=" sign + line = append(line[:idx+1], line[idx+2:len(line)-1]...) + line = bytes.Replace(line, escapedQuotes, regularQuotes, -1) + } + tw.Write(line) + tw.WriteString("\n") + } + sr.Close() + tw.Close() + + // Modification time of files from Crowdin often ahead of current, + // so we need to set back to current. + os.Chtimes(target, now, now) + } + + fmt.Println("Locale files has been successfully imported!") + return nil +} |