Skip to content

Commit c862df5

Browse files
committed
feat: update config paths on windows, fix task scheduler job not working
1 parent b6603dd commit c862df5

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

docs/.vitepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const currentYear = currentDate.getFullYear();
77
export default defineConfig({
88
title: "Updatectl",
99
description: "A CLI tool for automating project updates",
10+
cleanUrls: true,
1011
base: "/",
1112
head: [
1213
["link", { rel: "icon", href: "/icon.ico" }],

install.bat

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
@echo off
2-
REM updatectl Windows Installer
2+
REM updatectl User Installer
33

44
echo Installing updatectl...
55

6+
REM Combine user and system PATH for this session
7+
for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v Path 2^>nul') do set "USER_PATH=%%B"
8+
for /f "tokens=2*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path 2^>nul') do set "SYS_PATH=%%B"
9+
set "PATH=%USER_PATH%;%SYS_PATH%"
10+
611
REM Check if Go is installed
712
go version >nul 2>&1
813
if %errorlevel% neq 0 (
9-
echo Error: Go is not installed. Please install Go first.
14+
echo Error: Go is not installed or not in PATH.
15+
echo Please install Go or restart your terminal after installing it.
1016
pause
1117
exit /b 1
1218
)
1319

1420
REM Build the binary
1521
echo Building updatectl...
1622
go build -o updatectl.exe main.go
23+
if %errorlevel% neq 0 (
24+
echo Build failed.
25+
pause
26+
exit /b 1
27+
)
1728

18-
REM Create install directory
19-
if not exist "C:\Program Files\updatectl" mkdir "C:\Program Files\updatectl"
29+
REM Install location (user home)
30+
set "INSTALL_DIR=%USERPROFILE%\updatectl"
31+
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"
2032

2133
REM Copy binary
22-
copy updatectl.exe "C:\Program Files\updatectl\"
34+
copy /Y updatectl.exe "%INSTALL_DIR%\" >nul
35+
36+
REM Add to user PATH if not already there
37+
setlocal enabledelayedexpansion
38+
for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v Path 2^>nul') do (
39+
set "CUR_PATH=%%B"
40+
)
41+
42+
echo !CUR_PATH! | find /I "%INSTALL_DIR%" >nul
43+
if %errorlevel% neq 0 (
44+
echo Adding "%INSTALL_DIR%" to your user PATH...
45+
setx PATH "!CUR_PATH!;%INSTALL_DIR%" >nul
46+
) else (
47+
echo Path already includes "%INSTALL_DIR%"
48+
)
2349

24-
echo updatectl installed successfully to C:\Program Files\updatectl\
25-
echo Make sure C:\Program Files\updatectl is in your PATH.
26-
echo Run 'updatectl init' to set up the daemon.
50+
endlocal
2751

28-
pause
52+
echo.
53+
echo updatectl installed successfully to "%INSTALL_DIR%"
54+
echo.
55+
echo You can now run "updatectl" from any new command prompt.
56+
echo Run "updatectl init" to set up the daemon.
57+
pause

main.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var initCmd = &cobra.Command{
6262
var configDir, configPath string
6363

6464
if runtime.GOOS == "windows" {
65-
configDir = filepath.Join(os.Getenv("ProgramData"), "updatectl")
65+
configDir = filepath.Join(os.Getenv("USERPROFILE"), "updatectl")
6666
} else {
6767
configDir = "/etc/updatectl"
6868
}
@@ -86,9 +86,45 @@ projects:
8686
}
8787

8888
if runtime.GOOS == "windows" {
89-
taskCmd := `schtasks /Create /TN "updatectl" /TR "updatectl watch" /SC ONSTART /RL HIGHEST /F`
90-
exec.Command("cmd", "/C", taskCmd).Run()
89+
taskName := "updatectl"
90+
configDir := filepath.Join(os.Getenv("USERPROFILE"), "updatectl")
91+
92+
batScript := fmt.Sprintf(`@echo off
93+
start "" /b "%s" watch
94+
`, filepath.Join(configDir, "updatectl.exe"))
95+
batScriptPath := filepath.Join(configDir, "run_updatectl.bat")
96+
97+
err := os.WriteFile(batScriptPath, []byte(batScript), 0644)
98+
if err != nil {
99+
fmt.Println("Failed to write batch wrapper script:", err)
100+
return
101+
}
102+
103+
taskRun := batScriptPath
104+
105+
cmd := exec.Command(
106+
"schtasks",
107+
"/Create",
108+
"/TN", taskName,
109+
"/TR", taskRun,
110+
"/SC", "ONSTART",
111+
"/RL", "HIGHEST",
112+
"/F",
113+
)
114+
output, err := cmd.CombinedOutput()
115+
if err != nil {
116+
fmt.Printf("Failed to create scheduled task: %v\nOutput: %s\n", err, output)
117+
return
118+
}
91119
fmt.Println("Created Windows Task Scheduler job for updatectl.")
120+
121+
runCmd := exec.Command("schtasks", "/Run", "/TN", taskName)
122+
runOutput, runErr := runCmd.CombinedOutput()
123+
if runErr != nil {
124+
fmt.Printf("Failed to run scheduled task immediately: %v\nOutput: %s\n", runErr, runOutput)
125+
} else {
126+
fmt.Println("Scheduled task started immediately.")
127+
}
92128
} else {
93129
fmt.Print("Enter the user for the systemd service (default: root): ")
94130
scanner := bufio.NewScanner(os.Stdin)
@@ -168,7 +204,7 @@ var buildCmd = &cobra.Command{
168204
func loadConfig() Config {
169205
var configPath string
170206
if runtime.GOOS == "windows" {
171-
configPath = filepath.Join(os.Getenv("ProgramData"), "updatectl", "updatectl.yaml")
207+
configPath = filepath.Join(os.Getenv("USERPROFILE"), "updatectl", "updatectl.yaml")
172208
} else {
173209
configPath = "/etc/updatectl/updatectl.yaml"
174210
}

0 commit comments

Comments
 (0)