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 ba6a297
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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"
.\dist\winit-conf.exe run -pwsh_profile_path "$PROFILE"
- name: Make sure correctly copied
run: |
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) < 1 {
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)
}
}
13 changes: 10 additions & 3 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,6 +41,13 @@ 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 {
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 ba6a297

Please sign in to comment.