mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 05:37:05 +02:00
allow datetime string as --timestamp parameter
This commit is contained in:
parent
c6be6c558f
commit
be1cfdac9e
4 changed files with 25 additions and 13 deletions
|
@ -16,8 +16,8 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
|
|||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -155,13 +155,29 @@ func (c *TransferCmd[E, I, R]) Transfer(exp backends.ExportProcessor[R], imp bac
|
|||
}
|
||||
|
||||
func (c *TransferCmd[E, I, R]) timestamp() (time.Time, error) {
|
||||
flagValue, err := c.cmd.Flags().GetInt64("timestamp")
|
||||
if err == nil && flagValue > math.MinInt64 {
|
||||
return time.Unix(flagValue, 0), nil
|
||||
} else {
|
||||
flagValue, err := c.cmd.Flags().GetString("timestamp")
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
// No timestamp given, read from database
|
||||
if flagValue == "" {
|
||||
timestamp, err := c.db.GetImportTimestamp(c.sourceName, c.targetName, c.entity)
|
||||
return timestamp, err
|
||||
}
|
||||
|
||||
// Try using given value as a Unix timestamp
|
||||
if timestamp, err := strconv.ParseInt(flagValue, 10, 64); err == nil {
|
||||
return time.Unix(timestamp, 0), nil
|
||||
}
|
||||
|
||||
// Try to parse datetime string
|
||||
for _, format := range []string{time.DateTime, time.RFC3339} {
|
||||
if t, err := time.Parse(format, flagValue); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, errors.New(i18n.Tr("invalid timestamp string \"%v\"", flagValue))
|
||||
}
|
||||
|
||||
func (c *TransferCmd[E, I, R]) updateTimestamp(result models.ImportResult, oldTimestamp time.Time) error {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue