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