Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Pull common config functionality out into Trajectory.Private .

  • Loading branch information...
commit 61501cab8552934fd72e216f93fcbda0c0555858 1 parent ccdc444
@mike-burns authored
Showing with 48 additions and 48 deletions.
  1. +4 −18 FindStory.hs
  2. +13 −28 InitTj.hs
  3. +25 −0 Trajectory/Private.hs
  4. +6 −2 trajectory.cabal
View
22 FindStory.hs
@@ -18,10 +18,11 @@ import Data.Attoparsec
import Control.Applicative
import Data.Data
-import System.Environment (getEnv)
import qualified Data.Text
import qualified Data.Map
+import Trajectory.Private
+
main = do
potentiallyAllStories <- getAllStories :: IO (Either Error Stories)
case potentiallyAllStories of
@@ -47,27 +48,12 @@ formatStory story =
allStoriesUrl = do
- configFileName <- getConfigFileName
- jsonString <- BS.readFile configFileName
- let (Done _ config) = parse json jsonString
- (Object mapping) = config
- (String keyText) = fromJust $ Data.Map.lookup (Data.Text.pack "default") mapping
+ config <- getConfig
+ let (String keyText) = fromJust $ Data.Map.lookup (Data.Text.pack "default") config
key = Data.Text.unpack keyText
return $ "https://www.apptrajectory.com/api/" ++ key ++ "/accounts/923bc9b85eaa4a9213c5/projects/activeblueleaf/stories.json"
-getConfigFileName = do
- getEnv "TRAJECTORY_CONFIG_FILE"
- `catch`
- (const (getEnv "HOME" >>= return . withHomeDir))
- `catch`
- (const (getEnv "USER" >>= return . withUserName))
- `catch`
- (const (return "/.trajectory"))
- where
- withHomeDir homeDir = homeDir ++ "/.trajectory"
- withUserName userName = "/usr/"++userName++"/.trajectory"
-
deriving instance Eq Network.HTTP.Enumerator.HttpException
data Error =
HTTPConnectionError E.IOException
View
41 InitTj.hs
@@ -1,11 +1,16 @@
module Main where
import Text.RegexPR (matchRegexPR)
-import System.Environment (getArgs, getEnv)
+import System.Environment (getArgs)
import Data.Maybe (isJust, fromMaybe)
import System.IO (hFlush, stdout)
-import Data.Object.Json (encodeFile, decodeFile)
-import Data.Object (Object(..))
+
+-- this is all used for writeKey:
+import Trajectory.Private
+import qualified Data.Map as M
+import qualified Data.Text as T
+import qualified Data.ByteString.Lazy as BS
+import Data.Aeson (encode, toJSON, Value(..))
main = do
args <- getArgs
@@ -20,35 +25,15 @@ getProfileNameFrom args =
getKey = promptWhile isBlank "API key: "
-getConfigFileName = do
- getEnv "TRAJECTORY_CONFIG_FILE"
- `catch`
- (const (getEnv "HOME" >>= return . withHomeDir))
- `catch`
- (const (getEnv "USER" >>= return . withUserName))
- `catch`
- (const (return "/.trajectory"))
- where
- withHomeDir homeDir = homeDir ++ "/.trajectory"
- withUserName userName = "/usr/"++userName++"/.trajectory"
-
writeKey configFileName profileName key = do
- priorConfig <- decodeFile configFileName
- let profileYaml = Mapping [(profileName, Scalar key)]
- let yaml = case priorConfig of
- Nothing -> profileYaml
- Just object -> mergeObject object profileYaml
- encodeFile configFileName yaml
+ configFileName <- getConfigFileName
+ priorConfig <- getConfig
+ let config = M.insert (T.pack profileName) (String $ T.pack key) priorConfig
+ json = toJSON config
+ BS.writeFile configFileName $ encode json
-- generally useful functions below; maybe they exist elsewhere:
-mergeObject (Mapping m1) (Mapping m2) = Mapping (m2 ++ m1)
-mergeObject (Sequence s1) (Sequence s2) = Sequence (s1 ++ s2)
-mergeObject (Sequence s) o2@(Scalar v) = Sequence (s ++ [o2])
-mergeObject o1@(Scalar v) (Sequence s) = Sequence (o1:s)
-mergeObject o1@(Scalar v1) o2@(Scalar v2) = Sequence [o1,o2]
-mergeObject o1 o2 = Sequence [o1,o2]
-
elementAfter [] _ = Nothing
elementAfter (x:xs) match
| x == match = nextElement xs
View
25 Trajectory/Private.hs
@@ -0,0 +1,25 @@
+module Trajectory.Private where
+
+import qualified Data.ByteString.Char8 as BS
+import System.Environment (getEnv)
+import Data.Aeson (json, Value(..))
+import Data.Attoparsec (parse, Result(..))
+
+getConfig = do
+ configFileName <- getConfigFileName
+ jsonString <- BS.readFile configFileName
+ let (Done _ config) = parse json jsonString
+ (Object mapping) = config
+ return mapping
+
+getConfigFileName = do
+ getEnv "TRAJECTORY_CONFIG_FILE"
+ `catch`
+ (const (getEnv "HOME" >>= return . withHomeDir))
+ `catch`
+ (const (getEnv "USER" >>= return . withUserName))
+ `catch`
+ (const (return "/.trajectory"))
+ where
+ withHomeDir homeDir = homeDir ++ "/.trajectory"
+ withUserName userName = "/usr/"++userName++"/.trajectory"
View
8 trajectory.cabal
@@ -50,8 +50,10 @@ Executable inittj
-- Packages needed in order to build this package.
Build-depends: base
,regexpr
- ,data-object-json
- ,data-object
+ ,aeson
+ ,bytestring
+ ,text
+ ,containers
-- Modules not exported by this package.
-- Other-modules:
@@ -71,6 +73,8 @@ Executable findstory
,aeson
,attoparsec
,http-types
+ ,text
+ ,containers
-- Modules not exported by this package.
-- Other-modules:
Please sign in to comment.
Something went wrong with that request. Please try again.