diff options
| author | Carlos Sosa <gnusosa@gnusosa.net> | 2020-04-18 20:08:00 -0700 |
|---|---|---|
| committer | Carlos Sosa <gnusosa@gnusosa.net> | 2020-04-23 09:21:02 -0700 |
| commit | c72953df05259b20a7fc87117aefbbe284a376a1 (patch) | |
| tree | 1a1515d0ada2adcc07e32095e841c8c2d4a0f982 /app/RunCmd.hs | |
Diffstat (limited to 'app/RunCmd.hs')
| -rwxr-xr-x | app/RunCmd.hs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/app/RunCmd.hs b/app/RunCmd.hs new file mode 100755 index 0000000..4007967 --- /dev/null +++ b/app/RunCmd.hs @@ -0,0 +1,99 @@ +{-# LANGUAGE OverloadedStrings #-} +module RunCmd + ( runCmd + ) +where + +import Options.Applicative +import Data.Semigroup ( (<>) ) +import Data.Text + +import System.Directory +import System.FilePath.Posix +import System.Exit +import Sound.MusicDirTrans + +data Options = Options + { revert :: Bool + , inputPath :: !FilePath } + deriving Show + +options :: Parser Options +options = + Options + <$> switch + ( long "revert" + <> short 'r' + <> help + "Revert the Directory name based on the dir.orig.name.txt file" + ) + <*> strArgument (metavar "DIR" <> help "Directory to transform") + +optsInfo :: ParserInfo Options +optsInfo = info + (options <**> helper) + ( fullDesc + <> progDesc + "Transform an Album directory into an Artist -> Album structure" + <> header "mds - metadata based directory transformer" + ) + +runCmd :: IO () +runCmd = do + opts <- execParser optsInfo + dir <- makeAbsolute $ inputPath opts + dirExist <- doesDirectoryExist dir + if dirExist + then do + let rev = revert opts + if rev + then runRevert dir + else do + paths <- runCreate dir + runCopy paths + runRevertFile paths + exitSuccess + else missingDir dir >> exitFailure + +runCreate :: FilePath -> IO ArtistPath +runCreate path = do + paths <- genArtistPathFromTracks path + mkNewDirs paths + return paths + +runCopy :: ArtistPath -> IO () +runCopy = copyToNewPath + +runRevertFile :: ArtistPath -> IO () +runRevertFile = mkRevertFile + +runRevert dir = do + let fpath = dir ++ revertFile + fileExist <- doesFileExist fpath + if fileExist + then do + origPath <- readFile fpath + let ap = mkRevertArtistPath dir origPath + createDirectoryIfMissing False (childPath ap) + copyToNewPath ap + removeFile $ childPath ap ++ "/" ++ revertFile + exitSuccess + else missingFile dir >> exitFailure + +missingDir :: FilePath -> IO () +missingDir dir = putStrLn errorMsg + where + errorMsg = + "mds can't find the given directory: " + ++ dir + ++ " does not exist. Goodbye." + +missingFile :: FilePath -> IO () +missingFile dir = putStrLn errorMsg + where + errorMsg = + "mds can't find dir.orig.name.txt file in the given directory:" + ++ " dir.orig.name.txt does not exist in " + ++ dir + ++ " . Goodbye." + |
