Skip to content

Commit

Permalink
binary file downloader implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gurkansahinn committed Jul 4, 2022
1 parent a9e954e commit d09dfe9
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

altv-cli
.idea
.DS_Store

# Dependency directories (remove the comment below to include it)
# vendor/
21 changes: 21 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"altv-cli/view"
"github.com/spf13/cobra"
)

var initCommand = &cobra.Command{
Use: "init",
Short: "alt:V CLI Init",
Long: `Downloads alt:V binary files quickly`,
Run: execute,
}

func execute(cmd *cobra.Command, args []string) {
view.BranchSelectorView()
}

func init() {
RootCmd.AddCommand(initCommand)
}
25 changes: 25 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var RootCmd = &cobra.Command{
Use: "altv-cli",
Short: "alt:V CLI Tool",
Long: "alt:V CLI Tool",
}

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize()
}
25 changes: 25 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module altv-cli

go 1.17

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/charmbracelet/bubbles v0.12.0 // indirect
github.com/charmbracelet/bubbletea v0.22.0 // indirect
github.com/charmbracelet/lipgloss v0.5.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.1 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
)
55 changes: 55 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/charmbracelet/bubbles v0.12.0 h1:fxb9U9yI60Hek3tcPmMTFya5NhvPrqpkpyMaNngFh7A=
github.com/charmbracelet/bubbles v0.12.0/go.mod h1:bbeTiXwPww4M031aGi8UK2HT9RDWoiNibae+1yCMtcc=
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/bubbletea v0.22.0 h1:E1BTNSE3iIrq0G0X6TjGAmrQ32cGCbFDPcIuImikrUc=
github.com/charmbracelet/bubbletea v0.22.0/go.mod h1:aoVIwlNlr5wbCB26KhxfrqAn0bMp4YpJcoOelbxApjs=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/cancelreader v0.2.0/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/cancelreader v0.2.1 h1:Xzd1B4U5bWQOuSKuN398MyynIGTNT89dxzpEDsalXZs=
github.com/muesli/cancelreader v0.2.1/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "altv-cli/cmd"

func main() {
cmd.Execute()
}
27 changes: 27 additions & 0 deletions pkg/binary/binaryDownloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pkg

import (
"altv-cli/util"
"log"
"os"
)

func DownloadBinaryFiles(branch string) {
directories := [2]string{"data", "modules"}

for _, name := range directories {
exists := util.ExistsDirectory(name)

if !exists {
err := os.Mkdir(name, 0755)
if err != nil {
log.Fatal(err)
}
}
}

for _, binaryFile := range GetBinaryFiles(branch) {
util.DownloadFile(binaryFile.RootPath, binaryFile.CdnUrl)
}
os.Exit(1)
}
81 changes: 81 additions & 0 deletions pkg/binary/getBinaryFiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package pkg

import (
"fmt"
"runtime"
)

type BinaryFile struct {
RootPath string `json:"rootPath"`
CdnUrl string `json:"cdnUrl"`
}

func GetBinaryFiles(branch string) []BinaryFile {
var linuxBinaryFiles = []BinaryFile{
{
RootPath: "data/vehmodels.bin",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/data/%s/data/vehmodels.bin", branch),
},
{
RootPath: "data/vehmods.bin",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/data/%s/data/vehmods.bin", branch),
},
{
RootPath: "data/clothes.bin",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/data/%s/data/clothes.bin", branch),
},
{
RootPath: "modules/libjs-module.so",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/js-module/%s/x64_linux/modules/js-module/libjs-module.so", branch),
},
{
RootPath: "libnode.so.102",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/js-module/${%s}/x64_linux/modules/js-module/libnode.so.102", branch),
},
{
RootPath: "start.sh",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/others/start.sh", branch),
},
{
RootPath: "altv-server",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/server/${branch}/x64_linux/altv-server", branch),
},
}

var windowsBinaryFiles = []BinaryFile{
{
RootPath: "data/vehmodels.bin",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/data/%s/data/vehmodels.bin", branch),
},
{
RootPath: "data/vehmods.bin",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/data/%s/data/vehmods.bin", branch),
},
{
RootPath: "data/clothes.bin",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/data/%s/data/clothes.bin", branch),
},
{
RootPath: "modules/js-module.dll",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/js-module/%s/x64_win32/modules/js-module/js-module.dll", branch),
},
{
RootPath: "libnode.dll",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/js-module/${%s}/x64_win32/modules/js-module/libnode.dll", branch),
},
{
RootPath: "altv-server.exe",
CdnUrl: fmt.Sprintf("https://cdn.altv.mp/server/${branch}/x64_win32/altv-server.exe", branch),
},
}

os := runtime.GOOS
switch os {
case "windows":
return windowsBinaryFiles
case "darwin", "linux":
return linuxBinaryFiles
default:
return linuxBinaryFiles
}
}
33 changes: 33 additions & 0 deletions util/downloadFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package util

import (
"fmt"
"io"
"net/http"
"os"
)

func DownloadFile(filepath string, url string) (err error) {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()

resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}

_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
}
12 changes: 12 additions & 0 deletions util/existsDirectory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package util

import "os"

func ExistsDirectory(name string) bool {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false
}

return true
}
81 changes: 81 additions & 0 deletions view/branchSelector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package view

import (
pkg "altv-cli/pkg/binary"
"fmt"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"os"
)

var docStyle = lipgloss.NewStyle().Margin(1, 2)

type item struct {
title, branch, desc string
}

func (i item) Title() string { return i.title }
func (i item) Description() string { return i.desc }
func (i item) FilterValue() string { return i.title }

type listModel struct {
list list.Model
loading bool
}

func (m listModel) Init() tea.Cmd {
return nil
}

func executeSelect(selecetedItem item) {
go SpinnerView()
pkg.DownloadBinaryFiles(selecetedItem.branch)
}

func (m listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter", " ":
executeSelect(m.list.SelectedItem().(item))

case "ctrl+c":
return m, tea.Quit
}

case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}

var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}

func (m listModel) View() string {
if m.loading {
return docStyle.Render("Downloading...")
}

return docStyle.Render(m.list.View())
}

func BranchSelectorView() {
items := []list.Item{
item{title: "Release", branch: "release", desc: "Download alt:V Release version"},
item{title: "Release Candidate", branch: "rc", desc: "Download alt:V RC version"},
item{title: "Development", branch: "dev", desc: "Download alt:V Development version"},
}

m := listModel{list: list.New(items, list.NewDefaultDelegate(), 0, 0)}
m.list.Title = "Download Binary Files"

p := tea.NewProgram(m, tea.WithAltScreen())

if err := p.Start(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
Loading

0 comments on commit d09dfe9

Please sign in to comment.