feat: add format setting for completion

This commit is contained in:
Antoine Aflalo
2024-08-27 08:51:35 -04:00
parent eec6fe91dd
commit 01e5c78369
6 changed files with 65 additions and 13 deletions

View File

@@ -6,31 +6,57 @@ import (
"CBZOptimizer/converter/constant"
"fmt"
"github.com/spf13/cobra"
"github.com/thediveo/enumflag/v2"
"os"
"path/filepath"
"strings"
"sync"
)
var converterType constant.ConversionFormat
func init() {
command := &cobra.Command{
Use: "convert",
Short: "Convert CBZ files using a specified converter",
Use: "optimize [folder]",
Short: "Optimize all CBZ files in a folder recursively",
Long: "Optimize all CBZ files in a folder recursively.\nIt will take all the different pages in the CBZ files and convert them to the given format.\nThe original CBZ files will be kept intact depending if you choose to override or not.",
RunE: ConvertCbzCommand,
Args: cobra.ExactArgs(1),
}
formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive)
_ = formatFlag.RegisterCompletion(command, "format", constant.HelpText)
command.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)")
command.Flags().IntP("parallelism", "n", 2, "Number of chapters to convert in parallel")
command.Flags().BoolP("override", "o", false, "Override the original CBZ files")
command.PersistentFlags().VarP(
formatFlag,
"format", "f",
fmt.Sprintf("Format to convert the images to: %s", constant.ListAll()))
command.PersistentFlags().Lookup("format").NoOptDefVal = constant.DefaultConversion.String()
AddCommand(command)
}
// isValidFolder checks if the provided path is a valid directory
func isValidFolder(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
path := args[0]
if path == "" {
return fmt.Errorf("path is required")
}
if !isValidFolder(path) {
return fmt.Errorf("the path needs to be a folder")
}
quality, err := cmd.Flags().GetUint8("quality")
if err != nil {
return fmt.Errorf("invalid quality value")
@@ -46,7 +72,7 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid parallelism value")
}
chapterConverter, err := converter.Get(constant.ImageFormatWebP)
chapterConverter, err := converter.Get(converterType)
if err != nil {
return fmt.Errorf("failed to get chapterConverter: %v", err)
}

View File

@@ -1,8 +1,31 @@
package constant
type ConversionFormat string
import "github.com/thediveo/enumflag/v2"
type ConversionFormat enumflag.Flag
const (
ImageFormatWebP ConversionFormat = "webp"
ImageFormatUnknown ConversionFormat = ""
WebP ConversionFormat = iota
)
var CommandValue = map[ConversionFormat][]string{
WebP: {"webp"},
}
var HelpText = enumflag.Help[ConversionFormat]{
WebP: "WebP Image Format",
}
var DefaultConversion = WebP
func (c ConversionFormat) String() string {
return CommandValue[c][0]
}
func ListAll() []string {
var formats []string
for _, names := range CommandValue {
formats = append(formats, names[0])
}
return formats
}

View File

@@ -17,7 +17,7 @@ type Converter interface {
}
var converters = map[constant.ConversionFormat]Converter{
constant.ImageFormatWebP: webp.New(),
constant.WebP: webp.New(),
}
// Available returns a list of available converters.
@@ -28,11 +28,11 @@ func Available() []constant.ConversionFormat {
// Get returns a packer by name.
// If the packer is not available, an error is returned.
func Get(name constant.ConversionFormat) (Converter, error) {
if packer, ok := converters[name]; ok {
return packer, nil
if converter, ok := converters[name]; ok {
return converter, nil
}
return nil, fmt.Errorf("unkown converter \"%s\", available options are %s", name, strings.Join(lo.Map(Available(), func(item constant.ConversionFormat, index int) string {
return string(item)
return item.String()
}), ", "))
}

View File

@@ -25,7 +25,7 @@ type Converter struct {
}
func (converter *Converter) Format() (format constant.ConversionFormat) {
return constant.ImageFormatWebP
return constant.WebP
}
func New() *Converter {
@@ -203,8 +203,8 @@ func (converter *Converter) convertPage(container *packer2.PageContainer, qualit
return container, nil
}
// convert converts an image to the ImageFormatWebP format. It decodes the image from the input buffer,
// encodes it as a ImageFormatWebP file using the webp.Encode() function, and returns the resulting ImageFormatWebP
// convert converts an image to the WebP format. It decodes the image from the input buffer,
// encodes it as a WebP file using the webp.Encode() function, and returns the resulting WebP
// file as a bytes.Buffer.
func (converter *Converter) convert(image image.Image, quality uint) (*bytes.Buffer, error) {
var buf bytes.Buffer

1
go.mod
View File

@@ -25,6 +25,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/thediveo/enumflag/v2 v2.0.5 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/text v0.17.0 // indirect

2
go.sum
View File

@@ -56,6 +56,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/thediveo/enumflag/v2 v2.0.5 h1:VJjvlAqUb6m6mxOrB/0tfBJI0Kvi9wJ8ulh38xK87i8=
github.com/thediveo/enumflag/v2 v2.0.5/go.mod h1:0NcG67nYgwwFsAvoQCmezG0J0KaIxZ0f7skg9eLq1DA=
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=