Skip to content

Commit cb2b888

Browse files
add: implemented the executable tool for prereqs
1 parent e1c1d67 commit cb2b888

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tools/executable.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
func Executable() (int, error) {
10+
args := os.Args[1:]
11+
if len(args) != 1 {
12+
fmt.Println("Make a file executable:")
13+
fmt.Println(" chmod +x in Unix, rename to .exe in Windows")
14+
fmt.Println("Usage: executable <file>")
15+
return 0, nil
16+
}
17+
18+
file := args[0]
19+
20+
// Get the current file permissions
21+
info, err := os.Stat(file)
22+
if err != nil {
23+
return 1, err
24+
}
25+
26+
// Add execute permissions for the owner
27+
if GetOS() == "windows" {
28+
if !strings.HasSuffix(strings.ToLower(file), ".exe") {
29+
fileexe := file + ".exe"
30+
err = os.Rename(file, fileexe)
31+
if err != nil {
32+
return 1, err
33+
}
34+
fmt.Printf("Successfully renamed %s to %s\n", file, fileexe)
35+
return 0, nil
36+
} else {
37+
fmt.Println("Nothing to do")
38+
return 0, nil
39+
}
40+
} else {
41+
err = os.Chmod(file, info.Mode()|0100)
42+
if err != nil {
43+
return 1, err
44+
}
45+
fmt.Printf("Successfully added execute permissions to %s\n", file)
46+
return 0, nil
47+
}
48+
}

0 commit comments

Comments
 (0)