github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/cmd/touch/touch.go (about)

     1  package touch
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"time"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/rclone/rclone/cmd"
    10  	"github.com/rclone/rclone/fs"
    11  	"github.com/rclone/rclone/fs/config/flags"
    12  	"github.com/rclone/rclone/fs/object"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var (
    17  	notCreateNewFile bool
    18  	timeAsArgument   string
    19  	localTime        bool
    20  )
    21  
    22  const defaultLayout string = "060102"
    23  const layoutDateWithTime = "2006-01-02T15:04:05"
    24  
    25  func init() {
    26  	cmd.Root.AddCommand(commandDefinition)
    27  	cmdFlags := commandDefinition.Flags()
    28  	flags.BoolVarP(cmdFlags, &notCreateNewFile, "no-create", "C", false, "Do not create the file if it does not exist.")
    29  	flags.StringVarP(cmdFlags, &timeAsArgument, "timestamp", "t", "", "Use specified time instead of the current time of day.")
    30  	flags.BoolVarP(cmdFlags, &localTime, "localtime", "", false, "Use localtime for timestamp, not UTC.")
    31  }
    32  
    33  var commandDefinition = &cobra.Command{
    34  	Use:   "touch remote:path",
    35  	Short: `Create new file or change file modification time.`,
    36  	Long: `
    37  Set the modification time on object(s) as specified by remote:path to
    38  have the current time.
    39  
    40  If remote:path does not exist then a zero sized object will be created
    41  unless the --no-create flag is provided.
    42  
    43  If --timestamp is used then it will set the modification time to that
    44  time instead of the current time. Times may be specified as one of:
    45  
    46  - 'YYMMDD' - eg. 17.10.30
    47  - 'YYYY-MM-DDTHH:MM:SS' - eg. 2006-01-02T15:04:05
    48  
    49  Note that --timestamp is in UTC if you want local time then add the
    50  --localtime flag.
    51  `,
    52  	Run: func(command *cobra.Command, args []string) {
    53  		cmd.CheckArgs(1, 1, command, args)
    54  		fsrc, srcFileName := cmd.NewFsDstFile(args)
    55  		cmd.Run(true, false, command, func() error {
    56  			return Touch(context.Background(), fsrc, srcFileName)
    57  		})
    58  	},
    59  }
    60  
    61  //Touch create new file or change file modification time.
    62  func Touch(ctx context.Context, fsrc fs.Fs, srcFileName string) (err error) {
    63  	timeAtr := time.Now()
    64  	if timeAsArgument != "" {
    65  		layout := defaultLayout
    66  		if len(timeAsArgument) == len(layoutDateWithTime) {
    67  			layout = layoutDateWithTime
    68  		}
    69  		var timeAtrFromFlags time.Time
    70  		if localTime {
    71  			timeAtrFromFlags, err = time.ParseInLocation(layout, timeAsArgument, time.Local)
    72  		} else {
    73  			timeAtrFromFlags, err = time.Parse(layout, timeAsArgument)
    74  		}
    75  		if err != nil {
    76  			return errors.Wrap(err, "failed to parse date/time argument")
    77  		}
    78  		timeAtr = timeAtrFromFlags
    79  	}
    80  	file, err := fsrc.NewObject(ctx, srcFileName)
    81  	if err != nil {
    82  		if !notCreateNewFile {
    83  			var buffer []byte
    84  			src := object.NewStaticObjectInfo(srcFileName, timeAtr, int64(len(buffer)), true, nil, fsrc)
    85  			_, err = fsrc.Put(ctx, bytes.NewBuffer(buffer), src)
    86  			if err != nil {
    87  				return err
    88  			}
    89  		}
    90  		return nil
    91  	}
    92  	err = file.SetModTime(ctx, timeAtr)
    93  	if err != nil {
    94  		return errors.Wrap(err, "touch: couldn't set mod time")
    95  	}
    96  	return nil
    97  }