如何使用 rex 创建目录

如何使用 rex 创建目录

rex如果目录不存在,我想用它创建一个目录。我读到 但我不知道该怎么做。

如果我发送没有目录的文件,我会收到此错误:

ERROR - upload: /usr/local/path/file.ext is not writable

有什么提示吗?

答案1

我认为 rex 书中的文档有点旧了。

请查看此处https://www.rexify.org/docs/api/或者(截至今天)最新的文档在这里:
https://www.rexify.org/docs/api/1.4/rex/commands/file.pm.html#file-file_name-options-
(上面说的是 1.4 版本,而当前的 cpan 版本是 1.6,不过没关系)

因此,举一个例子来回答你的问题:

task "backuptask", group => "mygroup", sub { 
    #
    # 1.) define the Backup Dir (you could do it without this step)                                                                                                                        
    my $backupdir = "/tmp/backup";  
    #                                                                                                      
    # 2.) "ensure" that the file is a (existing) "directory"        
    file $backupdir, 
            ensure=> "directory", 
            owner => "myowner", 
            group => "mygroup", 
            mode => 700, 
            on_change => sub { say "File was changed";};

    #                                                                                                                                    
    # 3.) define Backup File                                                                                                                         
    my $currTimestamp = strftime('%y%m%d',localtime);                                                                                     
    my $backupfile = "$backupdir/somebackup$currTimestamp";
    #                                                                            
    # 4.) "ensure" the the file is "present" at the defined path
    file $backupfile, ensure=> "present";

    ...execute something here...

}  ;                                                                                               

相关内容