-
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.
- Loading branch information
1 parent
45eb3a8
commit e7ebfe9
Showing
5 changed files
with
79 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package globals | ||
|
||
var Version = "1.0.2" |
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 +1,33 @@ | ||
package installer | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"runtime" | ||
) | ||
|
||
func copyBinary(src string, dst string) error{ | ||
err := os.MkdirAll(dst, 0777) | ||
if err!= nil && err != os.ErrExist { | ||
return err | ||
} | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer in.Close() | ||
var added = "" | ||
if runtime.GOOS == "windows" { | ||
added+=".exe" | ||
} | ||
out, err := os.Create(dst+"grif"+added) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
_, err = io.Copy(out, in) | ||
if err != nil { | ||
return err | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
//+build linux | ||
//+build freebsd | ||
//+build darwin | ||
|
||
package installer | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fatih/color" | ||
"github.com/grifpkg/cli/globals" | ||
"github.com/kardianos/osext" | ||
"github.com/segmentio/ksuid" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"os/user" | ||
"path" | ||
) | ||
|
||
func isRoot() (bool, error) { | ||
currentUser, err := user.Current() | ||
if err != nil { | ||
return false, err | ||
} | ||
return currentUser.Username == "root", nil | ||
} | ||
|
||
func Install(){ | ||
path, _ := osext.ExecutableFolder() | ||
root, _:= isRoot() | ||
if !root { | ||
fmt.Fprintf(color.Output, "%s %s\n", color.HiYellowString("!"), color.RedString("You must execute this command as root in order to be able to copy the binary to /usr/local/grifpkg/bin")) | ||
return | ||
} | ||
file, _ := osext.Executable() | ||
fmt.Println(path+file) | ||
if(!strings.HasPrefix(file, "/usr/local/grifpkg/bin/")){ | ||
_ := os.MkdirAll("/usr/local/grifpkg/bin/", 0700) | ||
_ := os.Rename(path+file,"/usr/local/grifpkg/bin/grif") | ||
randomId := ksuid.New().String() | ||
installPath := "/usr/local/etc/grifpkg/bin/" | ||
_ = copyBinary(file, installPath+randomId+"/") | ||
|
||
// remove all folders except new installation and current installation | ||
files, err := ioutil.ReadDir(installPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, f := range files { | ||
if f.Name()!="." && path.Base(f.Name()) != path.Base(file) && path.Base(f.Name()) != randomId { | ||
_ = os.RemoveAll(installPath+f.Name()) | ||
} | ||
} | ||
exec.Command("export","PATH=/usr/local/grifpkg/bin/:$PATH") | ||
|
||
// symlink | ||
_ = os.Remove("/usr/local/bin/grif") | ||
exec.Command("ln", "-s", installPath+randomId+"/grif", "/usr/local/bin/grif").Run() | ||
exec.Command("chmod", "+x", "/usr/local/bin/grif").Run() | ||
|
||
// | ||
fmt.Fprintf(color.Output, "%s grif %s has been installed\n", color.HiGreenString("i"), color.CyanString(globals.Version)) | ||
} |
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