Added some comments

This commit is contained in:
Leons Aleksandrovs
2025-08-10 21:50:56 +03:00
parent 04a5a27cfe
commit 0d3fe297af

View File

@@ -11,11 +11,13 @@ import (
) )
func main() { func main() {
// Check for arguments
if len(os.Args) < 2 { if len(os.Args) < 2 {
fmt.Println("Usage: runner <file-or-directory>") fmt.Println("Usage: runner <file-or-directory>")
os.Exit(1) os.Exit(1)
} }
// Select first argument, and check if it's a file or directory
targetPath := os.Args[1] targetPath := os.Args[1]
info, err := os.Stat(targetPath) info, err := os.Stat(targetPath)
@@ -53,12 +55,14 @@ func main() {
} }
func runCommandsFromFile(filename string, currentDir *string) error { func runCommandsFromFile(filename string, currentDir *string) error {
// Try to open the file
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
return fmt.Errorf("Error opening file: %w", err) return fmt.Errorf("Error opening file: %w", err)
} }
defer f.Close() defer f.Close()
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
for scanner.Scan() { for scanner.Scan() {
cmdLine := strings.TrimSpace(scanner.Text()) cmdLine := strings.TrimSpace(scanner.Text())
@@ -86,6 +90,7 @@ func runCommandsFromFile(filename string, currentDir *string) error {
continue continue
} }
// Execute the command
fmt.Println(">>> Executing:", cmdLine) fmt.Println(">>> Executing:", cmdLine)
cmd := exec.Command("bash", "-c", cmdLine) cmd := exec.Command("bash", "-c", cmdLine)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout