From 3c5a4beaf13cdb9d251100f6a0c4d6370ed42f1c Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Tue, 22 Aug 2023 17:39:55 +0900 Subject: [PATCH] Rewrite setting up windows terminals scripts with golang --- .../main.go | 1 + cmd/setup_windows_terminals/main.go | 65 +++++++++++++++++++ cmd/setup_wsl/main.go | 14 +--- copy.go | 27 ++++++++ windows/README.md | 24 +++---- windows/config/winget-pkgs-dev.json | 6 +- windows/scripts/bootstrap.ps1 | 14 ---- 7 files changed, 110 insertions(+), 41 deletions(-) create mode 100644 cmd/setup_windows_terminals/main.go create mode 100644 copy.go delete mode 100644 windows/scripts/bootstrap.ps1 diff --git a/cmd/enable_windows_verbose_context_menu/main.go b/cmd/enable_windows_verbose_context_menu/main.go index ceec37c9..5a7f44ff 100644 --- a/cmd/enable_windows_verbose_context_menu/main.go +++ b/cmd/enable_windows_verbose_context_menu/main.go @@ -19,6 +19,7 @@ func main() { if err != nil { log.Fatalf("Failed to update registry: %+v", err) } + defer newKey.Close() if isExists { log.Println("Skipped to create registry key, because it is already exists") return diff --git a/cmd/setup_windows_terminals/main.go b/cmd/setup_windows_terminals/main.go new file mode 100644 index 00000000..06f28275 --- /dev/null +++ b/cmd/setup_windows_terminals/main.go @@ -0,0 +1,65 @@ +//go:build windows + +package main + +import ( + "flag" + "log" + "os" + "path" + "path/filepath" + + "github.com/kachick/dotfiles" +) + +func main() { + dotsPathFlag := flag.String("dotfiles_path", "", "Specify dotfiles repository path in your local") + pwshProfilePathFlag := flag.String("pwsh_profile_path", "", "Specify PowerShell profile path") + flag.Parse() + dotsPath := filepath.Clean(*dotsPathFlag) + pwshProfilePath := filepath.Clean(*pwshProfilePathFlag) + + if dotsPath == "" || pwshProfilePath == "" || len(os.Args) < 2 { + flag.Usage() + log.Fatalf("called with wrong arguments") + } + + homePath, err := os.UserHomeDir() + if err != nil { + log.Fatalf("Failed to get home directory: %+v", err) + } + + appdataPath, ok := os.LookupEnv("APPDATA") + if !ok { + log.Fatalln("ENV APPDATA is not found") + } + + // As I understand it, unix like permission masks will work even in windows... + err = os.MkdirAll(filepath.Join(homePath, ".config", "alacritty"), 0750) + if err != nil { + log.Fatalf("Failed to create dotfiles directory: %+v", err) + } + err = os.MkdirAll(path.Join(appdataPath, "alacritty"), 0750) + if err != nil { + log.Fatalf("Failed to create path that will have alacritty.yml: %+v", err) + } + + copies := []dotfiles.Copy{ + {Src: filepath.Join(dotsPath, "home", ".config", "starship.toml"), Dst: filepath.Join(homePath, ".config", "starship.toml")}, + {Src: filepath.Join(dotsPath, "home", ".config", "alacritty", "alacritty-common.yml"), Dst: filepath.Join(homePath, ".config", "alacritty", "alacritty-common.yml")}, + {Src: filepath.Join(dotsPath, "home", ".config", "alacritty", "alacritty-windows.yml"), Dst: filepath.Join(homePath, ".config", "alacritty", "alacritty.yml")}, + {Src: filepath.Join(dotsPath, "windows", "config", "Profile.ps1"), Dst: pwshProfilePath}, + } + + for _, copy := range copies { + err := copy.Run() + if err != nil { + log.Fatalf("Failed to copy file: %+v %+v", copy, err) + } + } + + log.Printf(`Completed, you need to restart terminals + +If you faced slow execution of PowerShell after this script, exclude %s from Anti Virus as Microsoft Defender +`, pwshProfilePath) +} diff --git a/cmd/setup_wsl/main.go b/cmd/setup_wsl/main.go index 7cbf6705..4cfa87b3 100644 --- a/cmd/setup_wsl/main.go +++ b/cmd/setup_wsl/main.go @@ -4,12 +4,12 @@ package main import ( "fmt" - "io" "log" "os" "os/exec" "strings" + "github.com/kachick/dotfiles" "golang.org/x/sys/unix" ) @@ -74,17 +74,7 @@ func mustPersistDockerZshCompletions() { } // Can't make immutable symlink, so copy and make immutable here // https://unix.stackexchange.com/questions/586430/how-to-make-a-symlink-read-only-chattr-i-location-symlink - target, err := os.Create(completionLoadablePath) - if err != nil { - log.Panicf("%+v\n", err) - } - defer target.Close() - integration, err := os.Open("dependencies/docker/zsh-vendor-completions.zsh") - if err != nil { - log.Panicf("%+v\n", err) - } - defer integration.Close() - _, err = io.Copy(target, integration) + err = dotfiles.Copy{Src: "dependencies/docker/zsh-vendor-completions.zsh", Dst: completionLoadablePath}.Run() if err != nil { log.Panicf("%+v\n", err) } diff --git a/copy.go b/copy.go new file mode 100644 index 00000000..c8763934 --- /dev/null +++ b/copy.go @@ -0,0 +1,27 @@ +package dotfiles + +import ( + "io" + "os" +) + +type Copy struct { + Src string + Dst string +} + +func (c Copy) Run() error { + src, err := os.Open(c.Src) + if err != nil { + return err + } + defer src.Close() + dst, err := os.Create(c.Dst) + if err != nil { + return err + } + defer dst.Close() + + _, err = io.Copy(dst, src) + return err +} diff --git a/windows/README.md b/windows/README.md index af8395ad..913ab3b6 100644 --- a/windows/README.md +++ b/windows/README.md @@ -1,28 +1,30 @@ # FAQ -## Configuration steps after installation packages +## Installation +1. ```powershell + winget import --import-file "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\config\winget-pkgs-basic.json" + winget import --import-file "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\config\winget-pkgs-dev.json" + ``` +1. New session of pwsh + ```powershell + go run github.com/kachick/dotfiles/cmd/setup_windows_terminals -dotfiles_path "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles" -pwsh_profile_path "$PROFILE" + go run github.com/kachick/dotfiles/cmd/disable_windows_beeps + go run github.com/kachick/dotfiles/cmd/enable_windows_verbose_context_menu + ``` 1. Change Dropbox storage path from `C:\Users`, default path made problems in System Restore. \ See https://zmzlz.blogspot.com/2014/10/windows-dropbox.html for detail -1. On powershell - ```powershell - Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser - \\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\scripts\bootstrap.ps1 -DotfilesPath "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles" - ``` -1. Exclude the `$PROFILE\Profile.ps1` from Anti Virus detection as Microsoft Defender 1. Enable Bitlocker and backup the restore key ## How to run go scripts in this repo? -After installed golang with winget +After installed golang with winget, you can run github hosting files ```console Administrator in ~ psh -> go run github.com/kachick/dotfiles/cmd/disable_windows_beeps@0ed52e4341624d7216d0b97a9b9bbab3719a8377 +> go run github.com/kachick/dotfiles/cmd/disable_windows_beeps@39ac6dc 2023/08/22 15:34:18 Completed to disable beeps, you need to restart Windows to activate settings -> go run github.com/kachick/dotfiles/cmd/disable_windows_beeps@0ed52e4341624d7216d0b97a9b9bbab3719a8377 -2023/08/22 15:40:42 Skipped to create registry key, because it is already exists ``` Specifying with branch name with the @ref may use cache, then specify commit ref diff --git a/windows/config/winget-pkgs-dev.json b/windows/config/winget-pkgs-dev.json index 706bdcb8..51cf0158 100644 --- a/windows/config/winget-pkgs-dev.json +++ b/windows/config/winget-pkgs-dev.json @@ -11,16 +11,14 @@ "Version" : "13.0.0" }, { - "PackageIdentifier" : "Canonical.Ubuntu.2204", - "Version" : "2204.2.33.0" + "PackageIdentifier" : "Canonical.Ubuntu.2204" }, { "PackageIdentifier" : "Docker.DockerDesktop", "Version" : "4.22.0" }, { - "PackageIdentifier" : "Microsoft.WindowsTerminal", - "Version" : "1.17.11461.0" + "PackageIdentifier" : "Microsoft.WindowsTerminal" }, { "PackageIdentifier" : "GoLang.Go", diff --git a/windows/scripts/bootstrap.ps1 b/windows/scripts/bootstrap.ps1 deleted file mode 100644 index 00cd7352..00000000 --- a/windows/scripts/bootstrap.ps1 +++ /dev/null @@ -1,14 +0,0 @@ -Param( - [String]$DotfilesPath -) - -mkdir ~/.config -ErrorAction SilentlyContinue -mkdir ~/.config/alacritty -ErrorAction SilentlyContinue -mkdir "$($env:APPDATA)/alacritty" -ErrorAction SilentlyContinue - -Copy-Item "${DotfilesPath}\home\.config/starship.toml" -Destination ~/.config -Copy-Item "${DotfilesPath}\home\.config/alacritty/alacritty-common.yml" -Destination ~/.config/alacritty -Copy-Item "${DotfilesPath}\home\.config/alacritty/alacritty-windows.yml" -Destination "$($env:APPDATA)/alacritty/alacritty.yml" -Copy-Item "${DotfilesPath}\windows\config/Profile.ps1" -Destination "$PROFILE" - -Write-Output 'Completed, you need to restart terminals'