Add touch command - fixes #1594

This commit is contained in:
Jakub Tasiemski 2017-11-20 12:16:05 +01:00 committed by Nick Craig-Wood
parent d8b3bf014d
commit 3cf7c61aa0
3 changed files with 133 additions and 0 deletions

View File

@ -43,6 +43,7 @@ import (
_ "github.com/ncw/rclone/cmd/sha1sum" _ "github.com/ncw/rclone/cmd/sha1sum"
_ "github.com/ncw/rclone/cmd/size" _ "github.com/ncw/rclone/cmd/size"
_ "github.com/ncw/rclone/cmd/sync" _ "github.com/ncw/rclone/cmd/sync"
_ "github.com/ncw/rclone/cmd/touch"
_ "github.com/ncw/rclone/cmd/tree" _ "github.com/ncw/rclone/cmd/tree"
_ "github.com/ncw/rclone/cmd/version" _ "github.com/ncw/rclone/cmd/version"
) )

71
cmd/touch/touch.go Normal file
View File

@ -0,0 +1,71 @@
package touch
import (
"bytes"
"time"
"github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
notCreateNewFile bool
timeAsArgument string
)
const defaultLayout string = "060102"
const layoutDateWithTime = "2006-01-02T15:04:05"
func init() {
cmd.Root.AddCommand(commandDefintion)
flags := commandDefintion.Flags()
flags.BoolVarP(&notCreateNewFile, "no-create", "C", false, "Do not create the file if it does not exist.")
flags.StringVarP(&timeAsArgument, "timestamp", "t", "", "Change the modification times to the specified time instead of the current time of day. The argument is of the form 'YYMMDD' (ex. 17.10.30) or 'YYYY-MM-DDTHH:MM:SS' (ex. 2006-01-02T15:04:05)")
}
var commandDefintion = &cobra.Command{
Use: "touch remote:path",
Short: `Create new file or change file modification time.`,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args)
fsrc, srcFileName := cmd.NewFsDstFile(args)
cmd.Run(true, false, command, func() error {
return Touch(fsrc, srcFileName)
})
},
}
//Touch create new file or change file modification time.
func Touch(fsrc fs.Fs, srcFileName string) error {
timeAtr := time.Now()
if timeAsArgument != "" {
layout := defaultLayout
if len(timeAsArgument) == len(layoutDateWithTime) {
layout = layoutDateWithTime
}
timeAtrFromFlags, err := time.Parse(layout, timeAsArgument)
if err != nil {
return errors.Wrap(err, "failed to parse date/time argument")
}
timeAtr = timeAtrFromFlags
}
file, err := fsrc.NewObject(srcFileName)
if err != nil {
if !notCreateNewFile {
var buffer []byte
src := fs.NewStaticObjectInfo(srcFileName, timeAtr, int64(len(buffer)), true, nil, fsrc)
_, err = fsrc.Put(bytes.NewBuffer(buffer), src)
if err != nil {
return err
}
}
return nil
}
err = file.SetModTime(timeAtr)
if err != nil {
return errors.Wrap(err, "touch: couldn't set mod time")
}
return nil
}

61
cmd/touch/touch_test.go Normal file
View File

@ -0,0 +1,61 @@
package touch
import (
"testing"
"time"
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/ncw/rclone/local"
)
func TestTouch(t *testing.T) {
fstest.Initialise()
f, err := fs.NewFs("testfiles")
err = Touch(f, "newFile")
require.NoError(t, err)
file, errFile := f.NewObject("newFile")
require.NoError(t, errFile)
err = file.Remove()
require.NoError(t, err)
notCreateNewFile = true
err = Touch(f, "fileWithCflag")
require.NoError(t, err)
file, errFile = f.NewObject("fileWithCflag")
require.Error(t, errFile)
notCreateNewFile = false
timeAsArgument = "060102"
err = Touch(f, "oldFile")
require.NoError(t, err)
file, err = f.NewObject("oldFile")
require.NoError(t, err)
curretTime := time.Now()
require.NoError(t, err)
print(file.ModTime().Year() < curretTime.Year())
assert.Equal(t, true, file.ModTime().Year() < curretTime.Year())
err = file.Remove()
require.NoError(t, err)
timeAsArgument = "2006-01-02T15:04:05"
err = Touch(f, "oldFile")
require.NoError(t, err)
file, err = f.NewObject("oldFile")
require.NoError(t, err)
assert.Equal(t, true, file.ModTime().Year() < curretTime.Year())
timeAsArgument = ""
err = Touch(f, "oldFile")
require.NoError(t, err)
file, err = f.NewObject("oldFile")
require.NoError(t, err)
timeBetween2007YearAndCurrent, errTime := time.Parse("060102", "121212")
require.NoError(t, errTime)
assert.Equal(t, true, file.ModTime().Year() > timeBetween2007YearAndCurrent.Year())
err = file.Remove()
require.NoError(t, err)
}