From 62635076c3d1a65bf7fce06783bea901e805ac76 Mon Sep 17 00:00:00 2001 From: HikariKnight <2557889+HikariKnight@users.noreply.github.com> Date: Wed, 5 Apr 2023 17:53:27 +0200 Subject: [PATCH] start work on go rewrite for stable release --- .goreleaser.yaml | 29 ---- cmd/main.go | 13 +- go.mod | 19 ++- go.sum | 63 ++++++++ .../ls_iommu_downloader.go | 143 ++++++++++++++++++ internal/tuimode/tuimode.go | 28 ++++ internal/untar/untar.go | 77 ++++++++++ main.go | 1 - pkg/params/params.go | 7 +- 9 files changed, 344 insertions(+), 36 deletions(-) delete mode 100644 .goreleaser.yaml create mode 100644 internal/ls_iommu_downloader/ls_iommu_downloader.go create mode 100644 internal/tuimode/tuimode.go create mode 100644 internal/untar/untar.go delete mode 120000 main.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml deleted file mode 100644 index f2d4dbc..0000000 --- a/.goreleaser.yaml +++ /dev/null @@ -1,29 +0,0 @@ -before: - hooks: - - go mod tidy -builds: - - env: - - CGO_ENABLED=0 - goos: - - linux - hooks: - post: - - upx "{{ .Path }}" -archives: - - replacements: - linux: Linux - amd64: x86_64 -checksum: - name_template: 'checksums.txt' -snapshot: - name_template: "{{ incpatch .Version }}-next" -changelog: - sort: asc - filters: - exclude: - - '^docs:' - - '^test:' - -# modelines, feel free to remove those if you don't want/use them: -# yaml-language-server: $schema=https://goreleaser.com/static/schema.json -# vim: set ts=2 sw=2 tw=0 fo=cnqoj diff --git a/cmd/main.go b/cmd/main.go index 89490cd..c0a135a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,7 +1,16 @@ package main -import "fmt" +import ( + "github.com/HikariKnight/quickpassthrough/internal/tuimode" + "github.com/HikariKnight/quickpassthrough/pkg/params" +) func main() { - fmt.Println("test") + // Get all our arguments in 1 neat struct + pArg := params.NewParams() + + if !pArg.Flag["gui"] { + //downloader.GetLsIOMMU() + tuimode.App() + } } diff --git a/go.mod b/go.mod index 800350d..1d8c267 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,21 @@ module github.com/HikariKnight/quickpassthrough go 1.20 -require github.com/akamensky/argparse v1.4.0 +require ( + code.rocketnine.space/tslocum/cview v1.5.8 + github.com/HikariKnight/ls-iommu v0.0.0-20230405132048-f543a620b6b6 + github.com/akamensky/argparse v1.4.0 + github.com/cavaliergopher/grab/v3 v3.0.1 +) + +require ( + code.rocketnine.space/tslocum/cbind v0.1.5 // indirect + github.com/gdamore/encoding v1.0.0 // indirect + github.com/gdamore/tcell/v2 v2.6.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/rivo/uniseg v0.4.3 // indirect + golang.org/x/sys v0.5.0 // indirect + golang.org/x/term v0.5.0 // indirect + golang.org/x/text v0.7.0 // indirect +) diff --git a/go.sum b/go.sum index 5bf91e2..846957a 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,65 @@ +code.rocketnine.space/tslocum/cbind v0.1.5 h1:i6NkeLLNPNMS4NWNi3302Ay3zSU6MrqOT+yJskiodxE= +code.rocketnine.space/tslocum/cbind v0.1.5/go.mod h1:LtfqJTzM7qhg88nAvNhx+VnTjZ0SXBJtxBObbfBWo/M= +code.rocketnine.space/tslocum/cview v1.5.8 h1:G90dP78WmZ8obrLNggx9z4kPXgJDT/MhsKpEAoqqto8= +code.rocketnine.space/tslocum/cview v1.5.8/go.mod h1:+MjJYGHPuMPgVgZEir/JZNIDFvsL87kOTzwIba6au2A= +github.com/HikariKnight/ls-iommu v0.0.0-20230405132048-f543a620b6b6 h1:jaGl8YTbuuOVMzXbym2h6vOC+E7HvYaRyNvFH6oRv6s= +github.com/HikariKnight/ls-iommu v0.0.0-20230405132048-f543a620b6b6/go.mod h1:1uNtCpUQNhh8GGCg+WqgRNTJf7jYDHvoGVvUJCKlEuE= github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc= github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= +github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4= +github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4= +github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= +github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= +github.com/gdamore/tcell/v2 v2.2.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU= +github.com/gdamore/tcell/v2 v2.5.2/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo= +github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg= +github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y= +github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +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-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.14-0.20220323023645-f9d555329d96/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.3.1/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= +github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210309040221-94ec62e08169/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-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/ls_iommu_downloader/ls_iommu_downloader.go b/internal/ls_iommu_downloader/ls_iommu_downloader.go new file mode 100644 index 0000000..714c2d8 --- /dev/null +++ b/internal/ls_iommu_downloader/ls_iommu_downloader.go @@ -0,0 +1,143 @@ +package ls_iommu_downloader + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "os" + "time" + + "github.com/HikariKnight/ls-iommu/pkg/errorcheck" + "github.com/HikariKnight/quickpassthrough/internal/untar" + "github.com/cavaliergopher/grab/v3" +) + +// Generated from github API response using https://mholt.github.io/json-to-go/ +type Response struct { + URL string `json:"url"` + AssetsURL string `json:"assets_url"` + UploadURL string `json:"upload_url"` + HTMLURL string `json:"html_url"` + ID int `json:"id"` + Author struct { + Login string `json:"login"` + ID int `json:"id"` + NodeID string `json:"node_id"` + AvatarURL string `json:"avatar_url"` + GravatarID string `json:"gravatar_id"` + URL string `json:"url"` + HTMLURL string `json:"html_url"` + FollowersURL string `json:"followers_url"` + FollowingURL string `json:"following_url"` + GistsURL string `json:"gists_url"` + StarredURL string `json:"starred_url"` + SubscriptionsURL string `json:"subscriptions_url"` + OrganizationsURL string `json:"organizations_url"` + ReposURL string `json:"repos_url"` + EventsURL string `json:"events_url"` + ReceivedEventsURL string `json:"received_events_url"` + Type string `json:"type"` + SiteAdmin bool `json:"site_admin"` + } `json:"author"` + NodeID string `json:"node_id"` + TagName string `json:"tag_name"` + TargetCommitish string `json:"target_commitish"` + Name string `json:"name"` + Draft bool `json:"draft"` + Prerelease bool `json:"prerelease"` + CreatedAt time.Time `json:"created_at"` + PublishedAt time.Time `json:"published_at"` + Assets []struct { + URL string `json:"url"` + ID int `json:"id"` + NodeID string `json:"node_id"` + Name string `json:"name"` + Label string `json:"label"` + Uploader struct { + Login string `json:"login"` + ID int `json:"id"` + NodeID string `json:"node_id"` + AvatarURL string `json:"avatar_url"` + GravatarID string `json:"gravatar_id"` + URL string `json:"url"` + HTMLURL string `json:"html_url"` + FollowersURL string `json:"followers_url"` + FollowingURL string `json:"following_url"` + GistsURL string `json:"gists_url"` + StarredURL string `json:"starred_url"` + SubscriptionsURL string `json:"subscriptions_url"` + OrganizationsURL string `json:"organizations_url"` + ReposURL string `json:"repos_url"` + EventsURL string `json:"events_url"` + ReceivedEventsURL string `json:"received_events_url"` + Type string `json:"type"` + SiteAdmin bool `json:"site_admin"` + } `json:"uploader"` + ContentType string `json:"content_type"` + State string `json:"state"` + Size int `json:"size"` + DownloadCount int `json:"download_count"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + BrowserDownloadURL string `json:"browser_download_url"` + } `json:"assets"` + TarballURL string `json:"tarball_url"` + ZipballURL string `json:"zipball_url"` + Body string `json:"body"` +} + +func GetLsIOMMU() { + // Check the API for releases + resp, err := http.Get("https://api.github.com/repos/hikariknight/ls-iommu/releases/latest") + errorcheck.ErrorCheck(err) + + // Close the response when function ends + defer resp.Body.Close() + + // Get the response body + body, err := io.ReadAll(resp.Body) + errorcheck.ErrorCheck(err) + + var result Response + if err := json.Unmarshal(body, &result); err != nil { + fmt.Println("Cant decode JSON") + } + + // Make the directory for ls-iommu if it does not exist + path := "utils" + if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { + err := os.Mkdir(path, os.ModePerm) + errorcheck.ErrorCheck(err) + } + + // Generate the download url + downloadUrl := fmt.Sprintf( + "https://github.com/HikariKnight/ls-iommu/releases/download/%s/ls-iommu_%s_Linux_x86_64.tar.gz", + result.TagName, + result.TagName, + ) + + // Create blank file + fileName := fmt.Sprintf("%s/ls-iommu_Linux_x86_64.tar.gz", path) + grabClient := grab.NewClient() + req, _ := grab.NewRequest(fileName, downloadUrl) + + // Download ls-iommu + download := grabClient.Do(req) + + // check for errors + if err := download.Err(); err != nil { + fmt.Fprintf(os.Stderr, "Download failed: %v\n", err) + os.Exit(1) + } + + fmt.Printf("Download saved to ./%v \n", download.Filename) + + r, err := os.Open(fileName) + errorcheck.ErrorCheck(err) + err = untar.Untar(fmt.Sprintf("%s/", path), r) + errorcheck.ErrorCheck(err) + +} diff --git a/internal/tuimode/tuimode.go b/internal/tuimode/tuimode.go new file mode 100644 index 0000000..18cf336 --- /dev/null +++ b/internal/tuimode/tuimode.go @@ -0,0 +1,28 @@ +package tuimode + +import ( + "code.rocketnine.space/tslocum/cview" +) + +func App() { + // Create a TUI app + app := cview.NewApplication() + defer app.HandlePanic() + app.EnableMouse(true) + + // Generate flex grid + flex := cview.NewFlex() + flex.SetDirection(cview.FlexRow) + + tv := cview.NewTextView() + tv.SetBorder(true) + tv.SetTitle("Hello, world!") + tv.SetText("Lorem ipsum dolor sit amet") + flex.AddItem(tv, 0, 0, true) + flex.AddItem(tv, 0, 1, false) + + app.SetRoot(flex, true) + if err := app.Run(); err != nil { + panic(err) + } +} diff --git a/internal/untar/untar.go b/internal/untar/untar.go new file mode 100644 index 0000000..4cb0e0a --- /dev/null +++ b/internal/untar/untar.go @@ -0,0 +1,77 @@ +package untar + +import ( + "archive/tar" + "compress/gzip" + "io" + "os" + "path/filepath" +) + +// Source: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07 + +// Untar takes a destination path and a reader; a tar reader loops over the tarfile +// creating the file structure at 'dst' along the way, and writing any files +func Untar(dst string, r io.Reader) error { + + gzr, err := gzip.NewReader(r) + if err != nil { + return err + } + defer gzr.Close() + + tr := tar.NewReader(gzr) + + for { + header, err := tr.Next() + + switch { + + // if no more files are found return + case err == io.EOF: + return nil + + // return any other error + case err != nil: + return err + + // if the header is nil, just skip it (not sure how this happens) + case header == nil: + continue + } + + // the target location where the dir/file should be created + target := filepath.Join(dst, header.Name) + + // the following switch could also be done using fi.Mode(), not sure if there + // a benefit of using one vs. the other. + // fi := header.FileInfo() + + // check the file type + switch header.Typeflag { + + // if its a dir and it doesn't exist create it + case tar.TypeDir: + if _, err := os.Stat(target); err != nil { + if err := os.MkdirAll(target, 0755); err != nil { + return err + } + } + + // if it's a file create it + case tar.TypeReg: + f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) + if err != nil { + return err + } + + // copy over contents + if _, err := io.Copy(f, tr); err != nil { + return err + } + + // manually close here after each file operation; defering would cause each file close to wait until all operations have completed. + f.Close() + } + } +} diff --git a/main.go b/main.go deleted file mode 120000 index 3788640..0000000 --- a/main.go +++ /dev/null @@ -1 +0,0 @@ -cmd/main.go \ No newline at end of file diff --git a/pkg/params/params.go b/pkg/params/params.go index 08ccf79..dab85bb 100644 --- a/pkg/params/params.go +++ b/pkg/params/params.go @@ -48,10 +48,10 @@ func NewParams() *Params { parser := argparse.NewParser("quickpassthrough", "A utility to help you configure your host for GPU Passthrough") // Configure arguments - /*gpu := parser.Flag("g", "gpu", &argparse.Options{ + gui := parser.Flag("g", "gui", &argparse.Options{ Required: false, - Help: "List all GPUs. (use -i # to only display results from specified IOMMU group)", - })*/ + Help: "Launch GUI (placeholder for now)", + }) // Parse arguments err := parser.Parse(os.Args) @@ -72,6 +72,7 @@ func NewParams() *Params { } // Add all parsed arguments to a struct for portability since we will use them all over the program + pArg.addFlag("gui", *gui) /*pArg.addFlag("gpu", *gpu) pArg.addFlag("usb", *usb) pArg.addFlag("nic", *nic)