2024-06-02  2024-09-14    347 字  1 分钟

原理为将配置文件集中存放于同一目录下,使用软链接来将配置文件链接到应用所需目录下。

if (is-admin) {
  open linkfiles.toml | transpose  file linkto | each {|e| mkdir -v $'($e.linkto | path dirname)'; mklink $e.linkto $'(pwd)\($e.file)'}
} else {
  print "Run it as admin."
}

脚本解析toml文件来获取文件路径与应链接到的路径。

toml文件格式大致为下:

'.\configs\alacritty.toml'='C:\Users\Initsnow\AppData\Roaming\alacritty\alacritty.toml'
'.\configs\starship.toml'='C:\Users\Initsnow\.config\starship.toml'

附删除链接的脚本:

open .\linkfiles.toml | transpose _ linkfile | each {|f| powershell rm $f.linkfile}

之后使用rclone来同步至网盘。

#!/usr/bin/env nu
let $ping_output = do -i { ping -n 1 8.8.4.4 } | complete

#Synchronize local files with remote path
def sync [path:path,remotepath:string] {
  open paths.toml | get remote | each {|e| rclone sync $path $'($e):($remotepath)'}
}

#Copy files in the remote path to the specified directory
def pull [remotepath:string,path:path] {
  open paths.toml | get remote | each {|e| rclone copy $'($e):($remotepath)' $path}
}

def main [action: string #sync or pull
 ] {
  if $action == "sync" {
    if $ping_output.exit_code == 0 {
      print "Internet connection is OK"
      open paths.toml | get path | transpose local remote | each {|e| sync $e.local $e.remote}
    } else {
      print "Unable to connect to the internet!"
      print $"Ping output:($ping_output.stdout)"
    }
  } else if $action == "pull" {
    if $ping_output.exit_code == 0 {
      print "Internet connection is OK"
      open paths.toml | get path | transpose local remote | each {|e| pull $e.remote $e.local}
    } else {
      print "Unable to connect to the internet!"
      print $"Ping output:($ping_output.stdout)"
    }
  } else {
    print "There is nothing to do..."
  }
}
remote=['onedrive','gdrive']

[path]
'C:\Users\Initsnow\Pictures'='Pictures'
'C:\Users\Initsnow\Documents\backups\config'='backups\config'
'C:\Users\Initsnow\Documents\backups\script'='backups\script'

使用nushell,放弃bash!