Skip to content

Commit

Permalink
Add dry run option
Browse files Browse the repository at this point in the history
  • Loading branch information
shterrett committed Nov 12, 2018
1 parent 808bd8a commit 563fcdc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions escape-from-itunes.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/Actions.hs
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions src/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) (
Expand Down
3 changes: 3 additions & 0 deletions src/Itunes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 29 additions & 2 deletions test/ArgsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ArgsSpec where
import Test.Hspec
import Options.Applicative
import Attributes
import Actions
import Args

spec = do
Expand All @@ -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"
Expand All @@ -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)

0 comments on commit 563fcdc

Please sign in to comment.