refh (empty) → 0.1.0
raw patch · 4 files changed
+117/−0 lines, 4 filesdep +basedep +clipparddep +cmdargssetup-changed
Dependencies added: base, clippard, cmdargs, directory, filepath, haskheap, network
Files
- LICENSE +7/−0
- Setup.hs +2/−0
- refh.cabal +23/−0
- src/Main.hs +85/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2012 Anthony Grimes++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ refh.cabal view
@@ -0,0 +1,23 @@+-- Initial refh.cabal generated by cabal init. For further documentation, +-- see http://haskell.org/cabal/users-guide/++name: refh+version: 0.1.0+synopsis: A command-line tool for pasting to https://www.refheap.com+-- description: +homepage: https://github.com/Raynes/refh+license: MIT+license-file: LICENSE+author: Anthony Grimes+maintainer: i@raynes.me+-- copyright: +-- category: +build-type: Simple+cabal-version: >=1.8++executable refh+ main-is: Main.hs+ hs-source-dirs: src+ -- other-modules: + build-depends: base >=4.5 && < 4.7, haskheap ==0.1.2, clippard ==0.1.1, cmdargs ==0.10,+ filepath ==1.3.0.0, directory ==1.1.0.2, network ==2.3.1.0
+ src/Main.hs view
@@ -0,0 +1,85 @@+{-# LANGUAGE DeriveDataTypeable #-}+module Main where++import Network.Haskheap+import System.Clippard (paste)+import System.Directory (getHomeDirectory, doesFileExist)+import System.FilePath ((</>), takeExtension)+import Control.Monad (when)+import System.Console.CmdArgs+import Network.URI (URI)++config :: IO (Maybe (String, String))+config = do+ home <- getHomeDirectory+ let name = home </> ".refh"+ exists <- doesFileExist name+ if exists+ then do+ contents <- readFile name+ let [user, token] = lines contents+ return $ Just (user, token)+ else return Nothing++data Args = Args { userName :: String+ , token :: String+ , copy :: Bool+ , language :: String+ , anon :: Bool+ , file :: String+ , private :: Bool+ } deriving (Show, Data, Typeable)++defArgs :: IO Args+defArgs = do+ let a = Args { userName = def &= help "Refheap username. Overrides configuration in .refh"+ , token = def &= help "Refheap token. Overrides configuration in .refh."+ , copy = True &= help "True by default. If true, copy resulting link to clipboard."+ , language = def &= help "Must be exactly as the language name appears on refheap's dropdown."+ , anon = False &= help "Paste anonymously regardless of username and token settings."+ , file = def &= help "Paste from this file instead of stdin. If "+ ++ "extension is present, use it to determine language."+ , private = False &= help "False by default. If true, make paste private."+ } &= summary "refh v0.1.0"+ cfg <- config+ case cfg of+ Just (user, token) -> return a { userName = user+ , token = token+ }+ Nothing -> return a++pickLang :: String -> String -> String+pickLang file language+ | null ext && null language = "Plain Text"+ | not (null ext) && not (null language) = language+ | null ext = language+ | null language = ext+ where ext = takeExtension file++readBody :: String -> IO String+readBody "" = getContents+readBody file = readFile file++auth :: Bool -> String -> String -> Maybe (String, String)+auth True _ _ = Nothing+auth False "" _ = Nothing+auth False _ "" = Nothing+auth False user token = Just (user, token)++getUrlStr :: Maybe URI -> String+getUrlStr (Just url) = show url++main :: IO ()+main = do+ defaults <- defArgs+ parsedArgs <- cmdArgs defaults+ body <- readBody $ file parsedArgs+ let lang = pickLang (file parsedArgs) (language parsedArgs)+ creds = auth (anon parsedArgs) (userName parsedArgs) (token parsedArgs)+ pasted <- createPaste body (private parsedArgs) lang creds+ case pasted of+ Left (Error e) -> putStrLn e+ Right p -> let url = getUrlStr $ getURL p+ in do when (copy parsedArgs) $ paste url+ putStrLn url+