File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments