Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
"time"

"code.gitea.io/gitea/models"
Expand All @@ -35,6 +36,11 @@ It can be used for backup and capture Gitea server image to send to maintainer`,
Value: "custom/conf/app.ini",
Usage: "Custom configuration file path",
},
cli.StringFlag{
Name: "file, f",
Value: "gitea-dump-%d.zip",
Usage: "Name of the dump file which will be created.",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Show process details",
Expand Down Expand Up @@ -85,7 +91,7 @@ func runDump(ctx *cli.Context) error {

dbDump := path.Join(tmpWorkDir, "gitea-db.sql")

fileName := fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix())
fileName := getDumpFileName(ctx)
log.Printf("Packing dump files...")
z, err := zip.Create(fileName)
if err != nil {
Expand Down Expand Up @@ -164,6 +170,16 @@ func runDump(ctx *cli.Context) error {
return nil
}

func getDumpFileName(ctx *cli.Context) string {
fmtString := ctx.String("file")

if strings.Count(fmtString, "%d") > 0 {
fmtString = fmt.Sprintf(fmtString, time.Now().Unix())
Copy link
Member

@jolheiser jolheiser Apr 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In reference to my other comment, personally I think this is unnecessary.

If a user wants to override the file name, they should be able to provide the exact name they want, without relying on the code transforming a %d. This code would only transform a single %d as well, and if they had any other formatting symbols before/after, they would also end up with a bad filename.

I will not block this PR, however I don't personally approve of this "magic".
(To clarify, I do like this feature. Overriding the filename would be great. I just don't particularly care for this part.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the "dupe" comment notification, I forget that comments in the file section don't show up in conversation unless it's marked as a review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see your point and generally agree. but if you don't submit a custom file name with the --file argument, it'll use the default and the default contains %d. so, this check is necessary.
we could change the default to not include %d but that may break existing setups?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or change the default value to

Value: fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some pseudo code could be:

filename := fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix())
if ctx.String("file") {
    filename = ctx.String("file")
}

and don't do fmt.Sprintf on what is passed in via CLI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Value: fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix())

damn. that's brilliant. i'll do that. thank you, sir!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or what @techknowlogick said. Whichever seems cleaner in the code. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented @jolheiser's approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉
Thanks again for the PR!

}

return fmtString
}

// zipAddDirectoryExclude zips absPath to specified zipPath inside z excluding excludeAbsPath
func zipAddDirectoryExclude(zip *zip.ZipArchive, zipPath, absPath string, excludeAbsPath string) error {
absPath, err := filepath.Abs(absPath)
Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/usage/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ in the current directory.

- Options:
- `--config path`, `-c path`: Gitea configuration file path. Optional. (default: custom/conf/app.ini).
- `--file name`, `-f name`: Name of the dump file with will be created. Optional. (default: gitea-dump-[timestamp].zip).
- `--tempdir path`, `-t path`: Path to the temporary directory used. Optional. (default: /tmp).
- `--skip-repository`, `-R`: Skip the repository dumping. Optional.
- `--database`, `-d`: Specify the database SQL syntax. Optional.
Expand Down