diff --git a/.github/workflows/ci-windows.yml b/.github/workflows/ci-windows.yml index 162f117e..257731aa 100644 --- a/.github/workflows/ci-windows.yml +++ b/.github/workflows/ci-windows.yml @@ -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" diff --git a/cmd/winit-conf/main.go b/cmd/winit-conf/main.go index 6581136a..926a0d86 100644 --- a/cmd/winit-conf/main.go +++ b/cmd/winit-conf/main.go @@ -3,6 +3,7 @@ package main import ( "embed" "flag" + "fmt" "log" "os" "path" @@ -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) + } } diff --git a/cmd/winit-reg/main.go b/cmd/winit-reg/main.go index 0f19c15e..cfd524fa 100644 --- a/cmd/winit-reg/main.go +++ b/cmd/winit-reg/main.go @@ -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 ` } @@ -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) diff --git a/config/windows/README.md b/config/windows/README.md index 623d2590..acececf7 100644 --- a/config/windows/README.md +++ b/config/windows/README.md @@ -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 ```