Refactor and split tui into smaller files
This commit is contained in:
parent
b339dab29f
commit
123d1ca18a
12 changed files with 603 additions and 508 deletions
|
@ -1,91 +0,0 @@
|
|||
package params
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/akamensky/argparse"
|
||||
)
|
||||
|
||||
/*
|
||||
The whole purpose of this module is to make a struct
|
||||
to just carry all our parsed arguments around between functions
|
||||
|
||||
Create a Params struct with all the argparse arguments
|
||||
pArg := params.NewParams()
|
||||
*/
|
||||
|
||||
type Params struct {
|
||||
Flag map[string]bool
|
||||
FlagCounter map[string]int
|
||||
IntList map[string][]int
|
||||
StringList map[string][]string
|
||||
String map[string]string
|
||||
}
|
||||
|
||||
func (p *Params) addFlag(name string, flag bool) {
|
||||
p.Flag[name] = flag
|
||||
}
|
||||
|
||||
func (p *Params) addFlagCounter(name string, flag int) {
|
||||
p.FlagCounter[name] = flag
|
||||
}
|
||||
|
||||
func (p *Params) addIntList(name string, flag []int) {
|
||||
p.IntList[name] = flag
|
||||
}
|
||||
|
||||
func (p *Params) addStringList(name string, flag []string) {
|
||||
p.StringList[name] = flag
|
||||
}
|
||||
|
||||
func (p *Params) addString(name string, flag string) {
|
||||
p.String[name] = flag
|
||||
}
|
||||
|
||||
func NewParams() *Params {
|
||||
// Setup the parser for arguments
|
||||
parser := argparse.NewParser("quickpassthrough", "A utility to help you configure your host for GPU Passthrough")
|
||||
|
||||
// Configure arguments
|
||||
gui := parser.Flag("g", "gui", &argparse.Options{
|
||||
Required: false,
|
||||
Help: "Launch GUI (placeholder for now)",
|
||||
})
|
||||
|
||||
// Parse arguments
|
||||
err := parser.Parse(os.Args)
|
||||
if err != nil {
|
||||
// In case of error print error and print usage
|
||||
// This can also be done by passing -h or --help flags
|
||||
fmt.Print(parser.Usage(err))
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
// Make our struct
|
||||
pArg := &Params{
|
||||
Flag: make(map[string]bool),
|
||||
FlagCounter: make(map[string]int),
|
||||
IntList: make(map[string][]int),
|
||||
StringList: make(map[string][]string),
|
||||
String: make(map[string]string),
|
||||
}
|
||||
|
||||
// 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)
|
||||
pArg.addFlag("sata", *sata)
|
||||
pArg.addFlagCounter("related", *related)
|
||||
pArg.addStringList("ignore", *ignore)
|
||||
pArg.addIntList("iommu_group", *iommu_group)
|
||||
pArg.addFlag("kernelmodules", *kernelmodules)
|
||||
pArg.addFlag("legacyoutput", *legacyoutput)
|
||||
pArg.addFlag("id", *id)
|
||||
pArg.addFlag("pciaddr", *pciaddr)
|
||||
pArg.addFlag("rom", *rom)
|
||||
pArg.addString("format", *format)*/
|
||||
|
||||
return pArg
|
||||
}
|
83
pkg/untar/untar.go
Normal file
83
pkg/untar/untar.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package untar
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
||||
)
|
||||
|
||||
// Slightly modified from source: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
|
||||
|
||||
// Untar takes a destination path and a path to a file; a tar reader loops over the tarfile
|
||||
// creating the file structure at 'dst' along the way, and writing any files
|
||||
func Untar(dst string, fileName string) error {
|
||||
r, err := os.Open(fileName)
|
||||
errorcheck.ErrorCheck(err, fmt.Sprintf("Failed to open: %s", fileName))
|
||||
defer r.Close()
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue