Skip to content

Commit

Permalink
Rewrite setting up windows terminals scripts with golang
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Aug 22, 2023
1 parent d10e1e5 commit baca0da
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 37 deletions.
1 change: 1 addition & 0 deletions cmd/enable_windows_verbose_context_menu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions cmd/setup_windows_terminals/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//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")
}

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("~/.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: "~/.config/starship.toml"},
{Src: filepath.Join(dotsPath, "home", ".config", "alacritty", "alacritty-common.yml"), Dst: "~/.config/alacritty/alacritty-common.yml"},
{Src: filepath.Join(dotsPath, "home", ".config", "alacritty", "alacritty-windows.yml"), Dst: "~/.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)
}
14 changes: 2 additions & 12 deletions cmd/setup_wsl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package main

import (
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"

"github.com/kachick/dotfiles"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -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)
}
Expand Down
27 changes: 27 additions & 0 deletions copy.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 11 additions & 7 deletions windows/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# 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" -pwshProfilePathFlag "$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?
Expand Down
6 changes: 2 additions & 4 deletions windows/config/winget-pkgs-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 0 additions & 14 deletions windows/scripts/bootstrap.ps1

This file was deleted.

0 comments on commit baca0da

Please sign in to comment.