diff --git a/escape-from-itunes.cabal b/escape-from-itunes.cabal index 4afcd44..63825d0 100644 --- a/escape-from-itunes.cabal +++ b/escape-from-itunes.cabal @@ -19,6 +19,7 @@ library , Args , Attributes , Channel + , Actions build-depends: base >= 4.7 && < 5 , idiii == 0.1.3.3 , optparse-applicative == 0.14.3.0 diff --git a/src/Actions.hs b/src/Actions.hs new file mode 100644 index 0000000..da7f11f --- /dev/null +++ b/src/Actions.hs @@ -0,0 +1,15 @@ +module Actions where + +import Itunes (Copy (Copy)) +import System.Directory (copyFile) + +data ActionType = DryRun + | CopyFiles + deriving (Show, Eq) + +getAction :: ActionType -> (Copy -> IO ()) +getAction DryRun = putStrLn . show +getAction CopyFiles = copy + +copy :: Copy -> IO () +copy (Copy from to) = copyFile from to diff --git a/src/Args.hs b/src/Args.hs index 9a77988..4a786f8 100644 --- a/src/Args.hs +++ b/src/Args.hs @@ -4,11 +4,13 @@ import Options.Applicative import Data.Semigroup ((<>)) import Text.Regex (mkRegex, splitRegex) import Attributes +import Actions (ActionType (DryRun, CopyFiles)) data Args = Args { source :: String , target :: String , attributes :: [Attribute] + , action :: ActionType } deriving (Show, Eq) @@ -23,6 +25,10 @@ getArgs = Args short 't' <> help "Root of new library") <*> attrList + <*> flag CopyFiles DryRun + ( long "dry-run" <> + short 'd' <> + help "Print intended work, but do not copy any files") attrList :: Parser [Attribute] attrList = option (maybeReader attrList) ( diff --git a/src/Itunes.hs b/src/Itunes.hs index 7c06094..0f67f5e 100644 --- a/src/Itunes.hs +++ b/src/Itunes.hs @@ -17,6 +17,9 @@ data Copy = Copy { from :: From , to :: To } +instance Show Copy where + show (Copy from to) = "Copy " ++ show from ++ " to " ++ to + type Action = Copy -> IO () type Transform = From -> IO To diff --git a/test/ArgsSpec.hs b/test/ArgsSpec.hs index 646341b..5a4ac1c 100644 --- a/test/ArgsSpec.hs +++ b/test/ArgsSpec.hs @@ -3,6 +3,7 @@ module ArgsSpec where import Test.Hspec import Options.Applicative import Attributes +import Actions import Args spec = do @@ -18,7 +19,8 @@ spec = do ]) `shouldBe` (Just $ Args "/Users/shterrett/iTunesMusic" "/Users/shterrett/music" - [Composer, Album, Artist]) + [Composer, Album, Artist] + CopyFiles) it "parses properly supplied long arguments" $ do getParseResult ( execParserPure defaultPrefs app [ "--source" @@ -30,4 +32,29 @@ spec = do ]) `shouldBe` (Just $ Args "/Users/shterrett/iTunesMusic" "/Users/shterrett/music" - [Composer, Album, Artist]) + [Composer, Album, Artist] + CopyFiles) + it "fails if the attribute list does not match allowed attributes" $ do + getParseResult ( + execParserPure defaultPrefs app [ "--source" + , "/Users/shterrett/iTunesMusic" + , "--target" + , "/Users/shterrett/music" + , "--attributes" + , "composer,period,artist" + ]) + `shouldBe` Nothing + it "sets the action to dry run when the flag is passed" $ do + getParseResult ( + execParserPure defaultPrefs app [ "--source" + , "/Users/shterrett/iTunesMusic" + , "--target" + , "/Users/shterrett/music" + , "--attributes" + , "composer,album,artist" + , "--dry-run" + ]) + `shouldBe` (Just $ Args "/Users/shterrett/iTunesMusic" + "/Users/shterrett/music" + [Composer, Album, Artist] + DryRun)