Skip to content

Commit

Permalink
unix global installer
Browse files Browse the repository at this point in the history
  • Loading branch information
quiquelhappy committed Aug 13, 2021
1 parent 45eb3a8 commit e7ebfe9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/globals/globals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package globals

var Version = "1.0.2"
3 changes: 2 additions & 1 deletion src/grif.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/fatih/color"
"github.com/grifpkg/cli/client"
"github.com/grifpkg/cli/config"
"github.com/grifpkg/cli/globals"
"github.com/grifpkg/cli/installer"
"github.com/spf13/cobra"
"os"
Expand All @@ -18,7 +19,7 @@ var rootCMD = &cobra.Command{
Short: "grif is a plugin management system for bukkit-based projects",
Long: "grif is a plugin management system for bukkit-based projects that allows you to install, remove and update packages existing on spigotmc, the grif library, and various other configurable sources",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(color.Output, "%s grif-cli version %s\n", color.HiGreenString("i"), color.CyanString("1.0.2"))
fmt.Fprintf(color.Output, "%s grif-cli version %s\n", color.HiGreenString("i"), color.CyanString(globals.Version))
},
}

Expand Down
32 changes: 32 additions & 0 deletions src/installer/installer.go
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
}
52 changes: 41 additions & 11 deletions src/installer/unixInstaller.go
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))
}
25 changes: 1 addition & 24 deletions src/installer/windowsInstaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/kardianos/osext"
"github.com/segmentio/ksuid"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -26,7 +25,6 @@ func Install(){
fmt.Println(copyerr.Error())
}
// remove all folders except new installation and current installation

files, err := ioutil.ReadDir(installPath)
if err != nil {
log.Fatal(err)
Expand All @@ -40,28 +38,7 @@ func Install(){
createInstallScript(installPath, randomId)

exec.Command(installPath+"install.bat").Run()
}

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()
out, err := os.Create(dst+"grif.exe")
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return err
fmt.Fprintf(color.Output, "%s grif %s has been installed\n", color.HiGreenString("i"), color.CyanString(globals.Version))
}

func createInstallScript(installPath string, id string){
Expand Down

0 comments on commit e7ebfe9

Please sign in to comment.