Skip to content

Commit

Permalink
fixed remaining linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvodi committed Aug 31, 2024
1 parent fc5350f commit ad1869e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 84 deletions.
158 changes: 91 additions & 67 deletions cmd/limepipes-cli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,129 +8,153 @@ import (
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
)

var (
recursive bool
dryRun bool
importTypes []string
skipFailedFiles bool
argRecursive bool
argDryRun bool
argImportTypes []string
argSkipFailedFiles bool
)

func addImportFileTypes(cmd *cobra.Command) {

cmd.Flags().StringSliceVarP(&importTypes, "import-file-types", "i", []string{importtype.BWW.String()},
cmd.Flags().StringSliceVarP(&argImportTypes, "import-file-types", "i", []string{importtype.BWW.String()},
fmt.Sprintf("only import files of a specific type. Can be specified with a comma separated list like "+
"-i=bww,xml. Valid file types are (%s).", strings.Join(importtype.TypeStrings(), ", ")),
)
}

func addRecursive(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&recursive, "recursive", "r", false,
cmd.Flags().BoolVarP(&argRecursive, "recursive", "r", false,
"import files recursively for all given directory paths",
)
}

func addDryRun(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false,
cmd.Flags().BoolVarP(&argDryRun, "dry-run", "d", false,
"import files but only convert them and print infos. Don't import them to the database and don't stop on parsing errors.",
)
}

func addVerbose(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false,
cmd.PersistentFlags().BoolVarP(&argVerbose, "verbose", "v", false,
"verbose output",
)
}

func addSkipFailedFiles(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(&skipFailedFiles, "skip-failed", "s", false,
cmd.PersistentFlags().BoolVarP(&argSkipFailedFiles, "skip-failed", "s", false,
"only print error when file couldn't be parsed. If flag is false, the command returns an error on first failed parsing",
)
}

func hasInvalidImportTypes() []string {
func invalidImportTypesPassed() []string {
var invalidImportTypes []string
for _, importType := range importTypes {
isValidImportType := false
for _, allImportType := range importtype.TypeStrings() {
if allImportType == importType {
isValidImportType = true
break
}
}
if !isValidImportType {
invalidImportTypes = append(invalidImportTypes, importType)
for _, aImportType := range argImportTypes {
if !slices.Contains(importtype.TypeStrings(), aImportType) {
invalidImportTypes = append(invalidImportTypes, aImportType)
}
}

return invalidImportTypes
}

func getAllFilesFromArgs(args []string) ([]string, error) {
// getFilesFromDir returns the absolute paths from all files passed in the paths slice.
// If the path is a directory, it will return all files from that directory.
// If the path is a file and has the correct extension, it will return the file.
func getAllFilesFromPaths(paths []string) ([]string, error) {
var allFiles []string
for _, arg := range args {
stat, err := os.Stat(arg)
for _, p := range paths {
fp, err := getFilesFromPath(p)
if err != nil {
return nil, err
}
if stat.IsDir() {
dirFiles, err := getFilesFromDir(arg)
if err != nil {
return nil, err
}
allFiles = append(allFiles, dirFiles...)
} else {
if fileHasValidExtension(stat.Name()) {
allFiles = append(allFiles, arg)
} else {
if verbose {
log.Info().Msgf("file %s will be ignored because it doesn't have a valid"+
"extension (%s)", stat.Name(), strings.Join(importTypes, ", "))
}
}
}
allFiles = append(allFiles, fp...)
}

return allFiles, nil
}

func getFilesFromPath(path string) ([]string, error) {
var allFiles []string
stat, err := os.Stat(path)
if err != nil {
return nil, err
}

// file without valid extension will be ignored
if !stat.IsDir() && !fileHasValidExtension(stat.Name()) && argVerbose {
log.Info().Msgf("file %s will be ignored because it doesn't have a valid"+
"extension (%s)", stat.Name(), strings.Join(argImportTypes, ", "))
return allFiles, nil
}

// file with valid extension will be added to the list
if !stat.IsDir() && fileHasValidExtension(stat.Name()) {
allFiles = append(allFiles, path)
return allFiles, nil
}

// if path is a directory, add all files with valid extension to the list
var dirFiles []string
if argRecursive {
dirFiles, err = getFilesRecursively(path)
} else {
dirFiles, err = getFilesFromDir(path)
}
if err != nil {
return nil, err
}
allFiles = append(allFiles, dirFiles...)

return allFiles, nil
}

// getFileFromDir returns the absolute paths from all files in the given directory.
func getFilesFromDir(path string) ([]string, error) {
var filePaths []string
if recursive {
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
for _, file := range files {
if file.IsDir() {
continue
}

if fileHasValidExtension(file.Name()) {
filePaths = append(filePaths, filepath.Join(path, file.Name()))
}
}

if info.IsDir() {
return nil
}
return filePaths, nil
}

if fileHasValidExtension(path) {
filePaths = append(filePaths, path)
}
// getFilesRecursively returns the absolute paths from all files in the given
// directory and all subdirectories.
func getFilesRecursively(path string) ([]string, error) {
var filePaths []string

return nil
})
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return nil, err
return err
}
} else {
files, err := os.ReadDir(path)
if err != nil {
return nil, err

if info.IsDir() {
return nil
}
for _, file := range files {
if file.IsDir() {
continue
}

if fileHasValidExtension(file.Name()) {
filePaths = append(filePaths, filepath.Join(path, file.Name()))
}

if fileHasValidExtension(path) {
filePaths = append(filePaths, path)
}

return nil
})
if err != nil {
return nil, err
}

return filePaths, nil
Expand All @@ -139,7 +163,7 @@ func getFilesFromDir(path string) ([]string, error) {
func fileHasValidExtension(path string) bool {
ext := filepath.Ext(path)
ext = strings.Trim(ext, ".")
for _, importType := range importTypes {
for _, importType := range argImportTypes {
if ext == importType {
return true
}
Expand All @@ -149,7 +173,7 @@ func fileHasValidExtension(path string) bool {
}

func checkForInvalidImportTypes() error {
invalidTypes := hasInvalidImportTypes()
invalidTypes := invalidImportTypesPassed()
if len(invalidTypes) > 0 {
return fmt.Errorf(
"invalid import file types: %s",
Expand Down
22 changes: 11 additions & 11 deletions cmd/limepipes-cli/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

var (
verbose bool
argVerbose bool
)

// importCmd represents the import command
Expand All @@ -30,7 +30,7 @@ subdirectories when given the recursive flag.
If a given file that has an extension which is not in the import-file-types, it will be ignored.
`,
Args: cobra.MinimumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, paths []string) error {
utils.SetupConsoleLogger()
cfg, err := config.Init()
if err != nil {
Expand Down Expand Up @@ -63,7 +63,7 @@ If a given file that has an extension which is not in the import-file-types, it
return err
}

allFiles, err := getAllFilesFromArgs(args)
allFiles, err := getAllFilesFromPaths(paths)
if err != nil {
return fmt.Errorf("failed getting files: %s", err.Error())
}
Expand All @@ -74,7 +74,7 @@ If a given file that has an extension which is not in the import-file-types, it
for i, file := range allFiles {
fExt := filepath.Ext(file)
if fExt == "" {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msgf("import file %s does not have an extension", file)
continue
}
Expand All @@ -84,7 +84,7 @@ If a given file that has an extension which is not in the import-file-types, it

filePlugin, err := pluginLoader.PluginForFileExtension(".bww")
if err != nil {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msgf("failed getting plugin for file %s with extension %s",
file, fExt)
continue
Expand All @@ -95,7 +95,7 @@ If a given file that has an extension which is not in the import-file-types, it

fileData, err := os.ReadFile(file)
if err != nil {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msgf("failed reading file %s", file)
continue
}
Expand All @@ -107,15 +107,15 @@ If a given file that has an extension which is not in the import-file-types, it

tunesImport, err := filePlugin.Import(fileData)
if err != nil {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msgf("failed parsing file %s", file)
continue
}

return fmt.Errorf("failed parsing file %s: %v", file, err)
}

if verbose {
if argVerbose {
log.Info().Msgf("(%d/%d) successfully parsed %d tunes from file %s",
i+1,
allFileCnt,
Expand All @@ -126,7 +126,7 @@ If a given file that has an extension which is not in the import-file-types, it

fType, err := pluginLoader.FileTypeForFileExtension(fExt)
if err != nil {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msgf("failed getting file type for file %s with extension %s",
file, fExt)
continue
Expand All @@ -137,7 +137,7 @@ If a given file that has an extension which is not in the import-file-types, it

fInfo, err := common.NewImportFileInfoFromLocalFile(file, fType)
if err != nil {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msg("failed creating import file info")
continue
}
Expand All @@ -147,7 +147,7 @@ If a given file that has an extension which is not in the import-file-types, it

_, _, err = dbService.ImportTunes(tunesImport.ImportedTunes, fInfo)
if err != nil {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msg("failed importing tunes")
continue
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/limepipes-cli/cmd/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ If a given file that has an extension which is not in the import-file-types, it
log.Fatal().Err(err).Msgf("failed getting plugin for extension .bww")
}

allFiles, err := getAllFilesFromArgs(args)
allFiles, err := getAllFilesFromPaths(args)
if err != nil {
return fmt.Errorf("failed getting files: %s", err.Error())
}
if verbose {
if argVerbose {
log.Info().Msg("Processing files: ")
for _, file := range allFiles {
log.Info().Msg(file)
Expand All @@ -79,7 +79,7 @@ If a given file that has an extension which is not in the import-file-types, it
return fmt.Errorf("failed creating output directory: %s", err.Error())
}

if verbose {
if argVerbose {
log.Info().Msgf("successful parsed files will be moved to: %s", OutputDir)
}

Expand All @@ -90,12 +90,12 @@ If a given file that has an extension which is not in the import-file-types, it
return err
}

if verbose {
if argVerbose {
log.Info().Msgf("parsing file %d/%d %s", i+1, allFileCnt, file)
}
tunesImport, err := bwwPlugin.Import(fileData)
if err != nil {
if skipFailedFiles {
if argSkipFailedFiles {
log.Error().Err(err).Msgf("failed parsing file %s", file)
continue
}
Expand All @@ -112,7 +112,7 @@ If a given file that has an extension which is not in the import-file-types, it
}
}

if verbose {
if argVerbose {
log.Info().Msgf("(%d/%d) successfully parsed %d tunes from file %s",
i+1,
allFileCnt,
Expand Down

0 comments on commit ad1869e

Please sign in to comment.