Skip to content

Commit

Permalink
Add subcommand to winit-conf to run without side-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Mar 7, 2024
1 parent d358607 commit d71339e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ jobs:
# This logics can be finished even if tools are not installed
- name: Put config files around terminals
run: |
.\dist\winit-conf.exe -pwsh_profile_path "$PROFILE"
Write-Host "$PROFILE"
.\dist\winit-conf.exe run --pwsh_profile_path="$PROFILE"
- name: Make sure correctly copied
run: |
Test-Path "$PROFILE"
Get-Content "$PROFILE"
73 changes: 59 additions & 14 deletions cmd/winit-conf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"embed"
"flag"
"fmt"
"log"
"os"
"path"
Expand Down Expand Up @@ -103,27 +104,71 @@ func (p provisioner) Copy() error {
return nil
}

func usage() string {
return `Usage: winit-conf [SUB] [OPTIONS]
Windows initialization to apply my settings for some apps
$ winit-conf.exe list
$ winit-conf.exe generate
$ winit-conf.exe run -pwsh_profile_path "$PROFILE"
`
}

func main() {
pwshProfilePathFlag := flag.String("pwsh_profile_path", "", "Specify PowerShell profile path")
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
pwshProfilePathFlag := runCmd.String("pwsh_profile_path", "", "Specify PowerShell profile path")
flag.Usage = func() {
// https://github.com/golang/go/issues/57059#issuecomment-1336036866
fmt.Printf("%s", usage()+"\n\n")
flag.PrintDefaults()
}
flag.Parse()
if *pwshProfilePathFlag == "" {

if len(os.Args) < 2 {
flag.Usage()
log.Fatalf("called with wrong arguments")

os.Exit(1)
}
// $PROFILE is an "Automatic Variables", not ENV
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.4
pwshProfilePath := filepath.Clean(*pwshProfilePathFlag)

for _, p := range provisioners(pwshProfilePath) {
log.Printf("%s => %s\n", p.EmbedPath(), p.DstPath())
err := p.Copy()
if err != nil {
log.Fatalf("Failed to copy file: %+v %+v", p, err)

switch os.Args[1] {
case "list":
for _, p := range provisioners("dummy_path") {
fmt.Println(p.EmbedPath())
}
case "generate":
for _, p := range provisioners("dummy_path") {
body, err := config.WindowsAssets.ReadFile(p.EmbedPath())
if err != nil {
fmt.Println(p.EmbedPath())
fmt.Println("--------------------------------------------------")
fmt.Println(string(body))
}
}
case "run":
if *pwshProfilePathFlag == "" {
flag.Usage()
log.Fatalf("called with wrong arguments")
}
// $PROFILE is an "Automatic Variables", not ENV
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.4
pwshProfilePath := filepath.Clean(*pwshProfilePathFlag)

for _, p := range provisioners(pwshProfilePath) {
log.Printf("%s => %s\n", p.EmbedPath(), p.DstPath())
err := p.Copy()
if err != nil {
log.Fatalf("Failed to copy file: %+v %+v", p, err)
}
}
}

log.Printf(`Completed, you need to restart terminals
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)
default:
flag.Usage()

os.Exit(1)
}
}
15 changes: 11 additions & 4 deletions cmd/winit-reg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func usage() string {
Windows initialization to modify default settings
$ winit-reg list
$ winit-reg run --action disable_beeps
$ winit-reg run --all
$ winit-reg.exe list
$ winit-reg.exe run --action disable_beeps
$ winit-reg.exe run --all
`
}

Expand All @@ -41,9 +41,16 @@ func main() {
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
actionFlag := runCmd.String("action", "", "which action you want to do")
allFlag := runCmd.Bool("all", false, "do ALL if you trust me")

flag.Usage = func() {
// https://github.com/golang/go/issues/57059#issuecomment-1336036866
fmt.Printf("%s", usage()+"\n\n")
flag.PrintDefaults()
}

flag.Parse()

if len(os.Args) < 1 {
if len(os.Args) < 2 {
flag.Usage()

os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion config/windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Basically following codes will be done in PowerShell
1. Download the windows helper binaries from [GitHub releases](https://github.com/kachick/dotfiles/releases) or uploaded artifacts in [each workflow](https://github.com/kachick/dotfiles/actions/workflows/release.yml) summary
1. New session of pwsh
```powershell
./winit-conf.exe -pwsh_profile_path "$PROFILE"
./winit-conf.exe run -pwsh_profile_path "$PROFILE"
./winit-reg.exe list
./winit-reg.exe run --all
```
Expand Down

0 comments on commit d71339e

Please sign in to comment.