From 008b4af6e84923e4a250b90a2965d8e91da5d71e Mon Sep 17 00:00:00 2001 From: Eduardo Trujillo <ed@chromabits.com> Date: Sat, 7 Jan 2017 15:31:57 -0600 Subject: [PATCH] feat(CLI): Add latest and versions commands --- src/Shift.hs | 2 ++ src/Shift/CLI.hs | 14 +++++++++++++- src/Shift/Git.hs | 28 +++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Shift.hs b/src/Shift.hs index ef1cc1f..2fead35 100644 --- a/src/Shift.hs +++ b/src/Shift.hs @@ -19,6 +19,8 @@ shiftMain = do case currentOptions ^. soCommand of GenerateCommand -> tempMain currentOptions + VersionsCommand -> versionsCommand currentOptions + LatestCommand -> latestCommand currentOptions where opts = info (helper <*> shiftOptions) diff --git a/src/Shift/CLI.hs b/src/Shift/CLI.hs index 4f40dd5..e6fa1df 100644 --- a/src/Shift/CLI.hs +++ b/src/Shift/CLI.hs @@ -17,7 +17,11 @@ data ShiftOptions = ShiftOptions , _soGitHubToken :: Maybe String } deriving (Show, Eq) -data ShiftCommand = GenerateCommand deriving (Show, Eq, Enum) +data ShiftCommand + = GenerateCommand + | VersionsCommand + | LatestCommand + deriving (Show, Eq, Enum) data HostingType = GitHubType | GitType deriving (Show, Eq, Enum) @@ -57,6 +61,14 @@ shiftCommand = subparser $ (info (pure GenerateCommand) (progDesc "Generate changelog") ) + <> command "versions" + (info (pure VersionsCommand) + (progDesc "List all versions in the repository") + ) + <> command "latest" + (info (pure LatestCommand) + (progDesc "Generate a changelog for the latest version only") + ) hostingType :: ReadM HostingType hostingType = eitherReader $ \case diff --git a/src/Shift/Git.hs b/src/Shift/Git.hs index 459d343..b3305ba 100644 --- a/src/Shift/Git.hs +++ b/src/Shift/Git.hs @@ -4,7 +4,7 @@ module Shift.Git where -import Control.Monad (void) +import Control.Monad (forM_, void) import Data.Either (rights) import Data.List (sortBy) import Data.Maybe (catMaybes, fromMaybe) @@ -29,7 +29,7 @@ import Data.String.Conversions (cs) import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.IO as TIO -import Data.Versions (parseV) +import Data.Versions (parseV, prettyV) import Filesystem.Path (absolute, basename, parent, (</>)) import Filesystem.Path.CurrentOS (encodeString) import GitHub.Auth (Auth (OAuth)) @@ -54,13 +54,29 @@ repoPath opts = fromMaybe ".git" (opts ^. soRepositoryPath) withRepo_ :: ShiftOptions -> (Git -> IO c) -> IO c withRepo_ opts = withRepo (fromString (repoPath opts)) +sortedTagList tags = sortBy (flip compare) . rights $ parseTag <$> toList tags + tempMain :: ShiftOptions -> IO () tempMain opts = withRepo_ opts $ \repo -> do tags <- tagList repo - let sortedVersions = sortBy (flip compare) . rights $ parseTag <$> toList tags + let sortedVersions = sortedTagList tags + pairedTags = swap <$> pairs sortedVersions + + shift opts repo pairedTags + +latestCommand :: ShiftOptions -> IO () +latestCommand opts = withRepo_ opts $ \repo -> do + tags <- tagList repo + + let sortedVersions = sortedTagList tags pairedTags = swap <$> pairs sortedVersions + case pairedTags of + x:_ -> shift opts repo [x] + [] -> putStrLn "Unable to find two versions to compare" + +shift opts repo pairedTags = case opts ^. soHostingType of GitHubType -> do state <- initGitHubState opts @@ -72,6 +88,12 @@ tempMain opts = withRepo_ opts $ \repo -> do (void $ runStateT (mapM_ (renderDiff repo) pairedTags) GitClientState) opts +versionsCommand :: ShiftOptions -> IO () +versionsCommand opts = withRepo_ opts $ \repo -> do + tags <- tagList repo + + forM_ (sortedTagList tags) $ \version -> putStrLn (T.unpack . prettyV $ _tVersioning version) + initGitHubState :: ShiftOptions -> IO GitHubClientState initGitHubState opts = do manager <- newManager tlsManagerSettings -- GitLab