refactoring (cq)

This commit is contained in:
Simon Vieille 2022-08-22 22:22:22 +02:00
parent 788f2a93d3
commit 12b93ad7ff
Signed by: deblan
GPG Key ID: 579388D585F70417
1 changed files with 164 additions and 89 deletions

253
main.go
View File

@ -22,8 +22,18 @@ import (
)
var Commands = []*cli.Command{}
var version = "dev"
var defaultListen = "127.0.0.1"
var defaultPort = "4000"
var defaultApiUrl = "http://127.0.0.1:4000"
var defaultServerDirectory = "."
var defaultDownloadDirectory = "."
var flagListen = "listen"
var flagPort = "port"
var flagApiUrl = "api-url"
var flagDirectory = "directory"
var flagDebug = "debug"
func main() {
app := &cli.App{
@ -33,39 +43,39 @@ func main() {
Aliases: []string{"s"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Name: flagListen,
Aliases: []string{"l"},
Value: "127.0.0.1",
Value: defaultListen,
},
&cli.StringFlag{
Name: "port",
Name: flagPort,
Aliases: []string{"p"},
Value: "4000",
Value: defaultPort,
},
&cli.StringFlag{
Name: "api-url",
Name: flagApiUrl,
Aliases: []string{"u"},
Value: "http://127.0.0.1:4000",
Value: defaultApiUrl,
},
&cli.StringFlag{
Name: "directory",
Name: flagDirectory,
Aliases: []string{"d"},
Value: ".",
Value: defaultServerDirectory,
},
&cli.StringFlag{
Name: "debug",
Name: flagDebug,
},
},
Usage: "run http server to serve api and files",
Action: func(ctx *cli.Context) error {
listen := ctx.String("listen")
port := ctx.Int64("port")
directory := strings.TrimSuffix(ctx.String("directory"), "/")
url := strings.TrimSuffix(ctx.String("api-url"), "/")
directory := strings.TrimSuffix(ctx.String(flagDirectory), "/")
url := strings.TrimSuffix(ctx.String(flagApiUrl), "/")
e := echo.New()
if ctx.Bool("debug") {
if ctx.Bool(flagDebug) {
e.Use(middleware.Logger())
e.Use(middleware.Recover())
}
@ -85,42 +95,37 @@ func main() {
},
{
Name: "play",
Usage: "run player",
Aliases: []string{"p"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api-url",
Name: flagApiUrl,
Aliases: []string{"u"},
Value: "http://127.0.0.1:4000",
},
&cli.StringFlag{
Name: "directory",
Aliases: []string{"d"},
Value: "$HOME/Videos",
Value: defaultApiUrl,
},
},
Usage: "run client",
Action: func(ctx *cli.Context) error {
return runShell(ctx, "play", strings.TrimSuffix(ctx.String("directory"), "/"))
return runShell(ctx, "play", "")
},
},
{
Name: "download",
Usage: "run downloder",
Aliases: []string{"d"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api-url",
Name: flagApiUrl,
Aliases: []string{"u"},
Value: "http://127.0.0.1:4000",
Value: defaultApiUrl,
},
&cli.StringFlag{
Name: "directory",
Name: flagDirectory,
Aliases: []string{"d"},
Value: ".",
Value: defaultDownloadDirectory,
},
},
Usage: "run client",
Action: func(ctx *cli.Context) error {
return runShell(ctx, "download", strings.TrimSuffix(ctx.String("directory"), "/"))
return runShell(ctx, "download", strings.TrimSuffix(ctx.String(flagDirectory), "/"))
},
},
},
@ -234,8 +239,94 @@ func apiList(e echo.Context, directory, url string) error {
return e.JSONPretty(http.StatusOK, files, " ")
}
func runShell(ctx *cli.Context, action, directory string) error {
url := strings.TrimSuffix(ctx.String("api-url"), "/")
func showFiles(files []File) {
size := len(files)
for key, file := range files {
fmt.Printf("[%3d] %s\n", size-key, file.Name)
}
}
func promptInput(defaultValue string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("> [%s] ", defaultValue)
input, _ := reader.ReadString('\n')
input = strings.Replace(input, "\n", "", -1)
if input == "" {
input = defaultValue
}
return input
}
func getFilesWyWildcard(files []File, result []File) []File {
for i := len(files) - 1; i >= 0; i-- {
result = append(result, files[i])
}
return result
}
func getFilesByRange1(files []File, result []File, a, b int) []File {
size := len(files)
if a > b {
fmt.Printf("%+v\n", a)
fmt.Printf("%+v\n", b)
for i := a; i >= b; i-- {
result = append(result, files[size-i])
}
fmt.Printf("%+v\n", result)
} else {
for i := a; i <= b; i++ {
result = append(result, files[size-i])
}
}
return result
}
func getFilesByRange2(files []File, result []File, a int) []File {
size := len(files)
for i := a; i >= 1; i-- {
result = append(result, files[size-i])
fmt.Println(i)
}
return result
}
func getFilesByRange3(files []File, result []File, a int) []File {
size := len(files)
for i := a; i <= size; i++ {
result = append(result, files[size-i])
fmt.Println(i)
}
return result
}
func getFilesByWordSplit(files []File, result []File, words []string) []File {
size := len(files)
for _, word := range words {
isInt, _ := regexp.MatchString("^[0-9]+$", word)
if isInt {
value, _ := strconv.Atoi(word)
result = append(result, files[size-value])
}
}
return result
}
func requestFileList(url string) []File {
response, err := http.Get(fmt.Sprintf("%s/api/list", url))
if err != nil {
fmt.Println(err)
@ -248,25 +339,34 @@ func runShell(ctx *cli.Context, action, directory string) error {
var files []File
json.Unmarshal([]byte(body), &files)
size := len(files)
for key, file := range files {
fmt.Printf("[%3d] %s\n", size-key, file.Name)
return files
}
func generatePlayerCmd(files []File) *exec.Cmd {
cmd := exec.Command("mpv", "-fs")
for _, f := range files {
cmd.Args = append(cmd.Args, f.Url)
}
reader := bufio.NewReader(os.Stdin)
return cmd
}
fmt.Print("> [1] ")
input, _ := reader.ReadString('\n')
input = strings.Replace(input, "\n", "", -1)
func generateDownloadCmds(files []File, directory string) []*exec.Cmd {
var cmds []*exec.Cmd
if input == "" {
input = "1"
for _, f := range files {
output := fmt.Sprintf("%s/%s", directory, f.Name)
cmd := exec.Command("wget", "-o", "/dev/stdout", "-c", "--show-progress", "-O", output, f.Url)
cmds = append(cmds, cmd)
}
if input == "q" {
return nil
}
return cmds
}
func selectFiles(files []File, input string) []File {
var result []File
range1Regex := "^([0-9]+)-([0-9]+)$"
range2Regex := "^([0-9]+)-$"
@ -276,12 +376,8 @@ func runShell(ctx *cli.Context, action, directory string) error {
isRange2, _ := regexp.MatchString(range2Regex, input)
isRange3, _ := regexp.MatchString(range3Regex, input)
var result []File
if input == "*" || input == "*+" {
for i := len(files) - 1; i >= 0; i-- {
result = append(result, files[i])
}
result = getFilesWyWildcard(files, result)
} else if input == "-*" {
result = files
} else if isRange1 { // a-b
@ -291,52 +387,40 @@ func runShell(ctx *cli.Context, action, directory string) error {
a, _ := strconv.Atoi(data[1])
b, _ := strconv.Atoi(data[2])
if a > b {
fmt.Printf("%+v\n", a)
fmt.Printf("%+v\n", b)
for i := a; i >= b; i-- {
result = append(result, files[size-i])
}
fmt.Printf("%+v\n", result)
} else {
for i := a; i <= b; i++ {
result = append(result, files[size-i])
}
}
result = getFilesByRange1(files, result, a, b)
} else if isRange2 { // a-
regex, _ := regexp.Compile(range2Regex)
data := regex.FindStringSubmatch(input)
a, _ := strconv.Atoi(data[1])
for i := a; i >= 1; i-- {
result = append(result, files[size-i])
fmt.Println(i)
}
result = getFilesByRange2(files, result, a)
} else if isRange3 { // a+
regex, _ := regexp.Compile(range3Regex)
data := regex.FindStringSubmatch(input)
a, _ := strconv.Atoi(data[1])
for i := a; i <= size; i++ {
result = append(result, files[size-i])
fmt.Println(i)
}
result = getFilesByRange3(files, result, a)
} else {
words := strings.Fields(input)
for _, word := range words {
isInt, _ := regexp.MatchString("^[0-9]+$", word)
if isInt {
value, _ := strconv.Atoi(word)
result = append(result, files[size-value])
}
}
result = getFilesByWordSplit(files, result, strings.Fields(input))
}
return result
}
func runShell(ctx *cli.Context, action, directory string) error {
files := requestFileList(strings.TrimSuffix(ctx.String(flagApiUrl), "/"))
showFiles(files)
input := promptInput("1")
if input == "q" {
return nil
}
result := selectFiles(files, input)
if len(result) == 0 {
fmt.Println("Empty list, aborded!")
@ -346,19 +430,10 @@ func runShell(ctx *cli.Context, action, directory string) error {
var cmds []*exec.Cmd
if action == "play" {
cmd := exec.Command("mpv", "-fs")
for _, f := range result {
cmd.Args = append(cmd.Args, f.Url)
}
cmd := generatePlayerCmd(result)
cmds = append(cmds, cmd)
} else {
for _, f := range result {
output := fmt.Sprintf("%s/%s", directory, f.Name)
cmd := exec.Command("wget", "-o", "/dev/stdout", "-c", "--show-progress", "-O", output, f.Url)
cmds = append(cmds, cmd)
}
cmds = generateDownloadCmds(result, directory)
}
for _, cmd := range cmds {