-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style(dump): Simplify filename generation logic
- Loading branch information
Showing
3 changed files
with
5 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,25 @@ | ||
package dump | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
const DateFormat = "2006-01-02_150405" | ||
|
||
var FilenameTemplate = fmt.Sprintf("{{ .Namespace }}_{{ .Date.Format %#v }}{{ .Ext }}", DateFormat) | ||
|
||
type Filename struct { | ||
Namespace string | ||
Ext string | ||
Date time.Time | ||
} | ||
|
||
func (vars Filename) Generate() (string, error) { | ||
return generate(vars, FilenameTemplate) | ||
} | ||
|
||
func generate(vars Filename, tmpl string) (string, error) { | ||
t, err := template.New("filename").Parse(tmpl) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var buf strings.Builder | ||
err = t.Execute(&buf, vars) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return buf.String(), nil | ||
func (vars Filename) Generate() string { | ||
return vars.Namespace + "_" + vars.Date.Format(DateFormat) + vars.Ext | ||
} | ||
|
||
func HelpFilename() string { | ||
filename, _ := Filename{ | ||
return Filename{ | ||
Namespace: "clevyr", | ||
Ext: ".sql.gz", | ||
Date: time.Date(2022, 1, 9, 9, 41, 0, 0, time.UTC), | ||
}.Generate() | ||
return filename | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters