Skip to content

Commit

Permalink
refactor: Move strategy enum to strings utils
Browse files Browse the repository at this point in the history
  • Loading branch information
caffeine-addictt committed Apr 15, 2024
1 parent aa5e1e7 commit 98dd0d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
37 changes: 5 additions & 32 deletions src/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package src

import (
"bufio"
"errors"
"fmt"
"io"
"mime"
Expand All @@ -19,40 +18,14 @@ import (
"github.com/spf13/cobra"
)

// Strategy
type strategyEnum string

const (
strategySynchronous strategyEnum = "synchronous"
strategyConcurrent strategyEnum = "concurrent"
)

func (e *strategyEnum) String() string {
return string(*e)
}

func (e *strategyEnum) Set(value string) error {
switch value {
case "concurrent", "synchronous":
*e = strategyEnum(value)
return nil
default:
return errors.New("must be one of 'synchronous' or 'concurrent'")
}
}

func (e *strategyEnum) Type() string {
return "<concurrent|synchronous>"
}

// Non URL similarity
// The resolved URLs from cache to the similarity score
type Similarity map[string]int8

// Command stuff
var getFlags struct {
inputFile string
strategy strategyEnum
strategy utils.StrategyEnum
maxConcurrency int
}

Expand All @@ -62,7 +35,7 @@ var getCommand = &cobra.Command{
Long: `Get and download videos from passed file and url(s)`,
Run: func(cmd *cobra.Command, args []string) {
// Warn on inefficient settings
if getFlags.maxConcurrency == 1 && getFlags.strategy == strategyConcurrent {
if getFlags.maxConcurrency == 1 && getFlags.strategy == utils.StrategyConcurrent {
fmt.Println("WARNING: Setting -m to 1 with -s concurrent may not be efficient, please consider using -s synchronous instead.")
}

Expand Down Expand Up @@ -324,7 +297,7 @@ var getCommand = &cobra.Command{

// Handle downloading
switch getFlags.strategy {
case strategyConcurrent:
case utils.StrategyConcurrent:
// Concurrency with no limit
if getFlags.maxConcurrency == 0 {
fmt.Printf("Downloading concurrently... [Use: %d, Max: No limit]", len(argSet))
Expand Down Expand Up @@ -361,7 +334,7 @@ var getCommand = &cobra.Command{

close(ch)
}
case strategySynchronous:
case utils.StrategySynchronous:
fmt.Println("Downloading synchronously...")

for url := range argSet {
Expand All @@ -374,7 +347,7 @@ var getCommand = &cobra.Command{
}

func init() {
getFlags.strategy = strategyConcurrent
getFlags.strategy = utils.StrategyConcurrent

rootCommand.AddCommand(getCommand)
getCommand.Flags().IntVarP(&getFlags.maxConcurrency, "max-concurrency", "m", 10, "Maximum number of concurrent downloads [0 = unlimited] (default is 10)")
Expand Down
29 changes: 29 additions & 0 deletions src/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,37 @@ import (
"fmt"
"os"
"strings"

"github.com/pkg/errors"
)

// Strategy Enum
type StrategyEnum string

const (
StrategySynchronous StrategyEnum = "synchronous"
StrategyConcurrent StrategyEnum = "concurrent"
)

func (e *StrategyEnum) String() string {
return string(*e)
}

func (e *StrategyEnum) Set(value string) error {
switch value {
case "concurrent", "synchronous":
*e = StrategyEnum(value)
return nil
default:
return errors.New("must be one of 'synchronous' or 'concurrent'")
}
}

func (e *StrategyEnum) Type() string {
return "<concurrent|synchronous>"
}

// Functions
func Multiline(line ...string) string {
return strings.Join(line, "\n")
}
Expand Down

0 comments on commit 98dd0d5

Please sign in to comment.