-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from kachick/go-on-windows
Rewrite PowerShell with Go for better Windows scripting
- Loading branch information
Showing
19 changed files
with
250 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux || darwin | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux || darwin | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build windows | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"golang.org/x/sys/windows/registry" | ||
) | ||
|
||
// # https://github.com/kachick/times_kachick/issues/214 | ||
func main() { | ||
key, err := registry.OpenKey(registry.CURRENT_USER, `Control Panel\Sound`, registry.SET_VALUE) | ||
if err != nil { | ||
log.Fatalf("Failed to open registry key: %+v", err) | ||
} | ||
defer key.Close() | ||
|
||
err = key.SetStringValue("Beep", "no") | ||
if err != nil { | ||
log.Fatalf("Failed to update registry: %+v", err) | ||
} | ||
|
||
log.Println("Completed to disable beeps, you need to restart Windows to activate settings") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux || darwin | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//go:build windows | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"golang.org/x/sys/windows/registry" | ||
) | ||
|
||
func main() { | ||
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Classes\CLSID`, registry.CREATE_SUB_KEY) | ||
if err != nil { | ||
log.Fatalf("Failed to open registry key: %+v", err) | ||
} | ||
defer key.Close() | ||
|
||
newKey, isExists, err := registry.CreateKey(key, `{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32`, registry.SET_VALUE) | ||
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 | ||
} | ||
|
||
err = newKey.SetStringValue("", "") | ||
if err != nil { | ||
log.Fatalf("Failed to set empty default value, may need to fallback manually: %+v", err) | ||
} | ||
|
||
log.Println("Completed to enable classic style of context menu, you need to restart all explorer.exe processes to activate settings") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux || darwin | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux || darwin | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.