├── .gitignore ├── LICENSE ├── README.org ├── copycat.cabal └── src ├── Copycat └── Opts.hs └── Main.hs /.gitignore: -------------------------------------------------------------------------------- 1 | /cabal.sandbox.config 2 | /.cabal-sandbox 3 | /dist 4 | /tmp 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2015 Elasticsearch 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | * Copycat 2 | 3 | [[http://upload.wikimedia.org/wikipedia/en/0/05/Copycat_Vanessa_Carlysle.jpg]] 4 | 5 | http://en.wikipedia.org/wiki/Copycat_(Marvel_Comics) 6 | 7 | ** Usage 8 | 9 | #+BEGIN_SRC 10 | % es master 11 | MDmFVH3GQ-ifxG6xm7zsIQ hotel.local 10.0.1.200 Wolverine 12 | % es shards | tail -1 13 | foo 98 p STARTED 104575 67.6mb 10.0.1.200 Wolverine 14 | % es indices -v 15 | health status index pri rep docs.count docs.deleted store.size pri.store.size 16 | green open foo 100 1 10500000 0 12.9gb 6.9gb 17 | % es indices -c health,status,index 18 | green open foo 19 | % es indices -v -c health,status,index 20 | health status index 21 | green open foo 22 | % es -u http://localhost:9200 health 23 | 1424982441 14:27:21 drewr.hotel green 3 2 200 100 0 0 0 24 | #+END_SRC 25 | 26 | ** Install 27 | *** Linux x86_64 28 | #+BEGIN_SRC 29 | curl https://download.elasticsearch.org/copycat/copycat-x86_64-linux >es 30 | chmod +x es 31 | #+END_SRC 32 | 33 | *** Mac OS X x86_64 34 | #+BEGIN_SRC 35 | curl https://download.elasticsearch.org/copycat/copycat-x86_64-osx >es 36 | chmod +x es 37 | #+END_SRC 38 | -------------------------------------------------------------------------------- /copycat.cabal: -------------------------------------------------------------------------------- 1 | Name: copycat 2 | 3 | Version: 1.0.0 4 | Synopsis: The cat API companion 5 | 6 | Description: When you're tired of typing `curl -s localhost:9200` 7 | 8 | Homepage: https://github.com/drewr/copycat 9 | License: OtherLicense 10 | License-file: LICENSE 11 | Author: Drew Raines 12 | Maintainer: Drew Raines 13 | Category: Utils 14 | Build-type: Simple 15 | Cabal-version: >= 1.20 16 | 17 | Extra-Source-Files: README.md 18 | 19 | flag developer 20 | description: build in developer mode 21 | default: False 22 | manual: True 23 | 24 | library 25 | hs-source-dirs: src 26 | ghc-options: -Wall -fwarn-tabs -funbox-strict-fields 27 | if flag(developer) 28 | ghc-options: -Werror 29 | default-language: Haskell98 30 | other-modules: 31 | Copycat.Opts 32 | Build-depends: 33 | base >= 4.5 && < 5 34 | , optparse-applicative 35 | , lens 36 | , bytestring 37 | , wreq 38 | , text 39 | 40 | Executable es 41 | hs-source-dirs: src 42 | Main-is: Main.hs 43 | Ghc-options: -threaded 44 | default-language: Haskell98 45 | Build-depends: 46 | base >= 4.5 && < 5 47 | , optparse-applicative 48 | , wreq 49 | , bytestring 50 | , lens 51 | , text 52 | 53 | Source-repository head 54 | type: git 55 | location: https://github.com/drewr/copycat 56 | -------------------------------------------------------------------------------- /src/Copycat/Opts.hs: -------------------------------------------------------------------------------- 1 | module Copycat.Opts ( CommandLine(..) 2 | , Opts(..) 3 | , Verbosity(..) 4 | , Columns 5 | , parseArgs 6 | ) where 7 | 8 | import Options.Applicative 9 | 10 | type Url = String 11 | type Command = String 12 | type Columns = String 13 | data Verbosity = Normal 14 | | Verbose 15 | deriving (Show, Read) 16 | 17 | data Opts = Opts 18 | { url :: Url 19 | , columns :: Columns 20 | , verbose :: Verbosity 21 | } 22 | 23 | data CommandLine = CommandLine Command Opts 24 | 25 | opts :: Parser Opts 26 | opts = Opts 27 | <$> strOption ( long "url" 28 | <> short 'u' 29 | <> value "http://localhost:9200" 30 | <> metavar "URL" 31 | <> help "Instance URL" ) 32 | <*> strOption ( long "columns" 33 | <> short 'c' 34 | <> value "default" 35 | <> metavar "COLUMNS" 36 | <> help "What columns to return" ) 37 | <*> flag Normal Verbose ( long "verbose" 38 | <> short 'v' 39 | <> help "Show column headers?" ) 40 | 41 | args :: Parser Command 42 | args = argument str ( metavar "API" <> help "cat API to call" ) 43 | 44 | parseCommandLine :: Parser CommandLine 45 | parseCommandLine = CommandLine <$> args <*> opts 46 | 47 | parseArgs :: IO (CommandLine) 48 | parseArgs = execParser p 49 | where 50 | p = info (helper <*> parseCommandLine) 51 | ( fullDesc <> progDesc "copycat!" <> header "the _cat companion" ) 52 | -------------------------------------------------------------------------------- /src/Main.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE OverloadedStrings #-} 2 | 3 | module Main where 4 | 5 | import Copycat.Opts 6 | import qualified Data.Text as T 7 | import qualified Data.ByteString.Lazy.Char8 as BS 8 | import Control.Lens 9 | import Network.Wreq (defaults, param, getWith, responseBody) 10 | 11 | main :: IO () 12 | main = parseArgs >>= go 13 | 14 | go :: CommandLine -> IO () 15 | go (CommandLine cmd opts) = do 16 | let httpopts1 = defaults & (param "v" .~ [verbosity $ verbose opts]) 17 | let httpopts2 = if columns opts == "default" 18 | then httpopts1 19 | else httpopts1 & (param "h" .~ [cols $ columns opts]) 20 | r <- getWith httpopts2 $ url opts ++ "/_cat/" ++ cmd 21 | putStr $ BS.unpack $ r ^. responseBody 22 | 23 | verbosity :: Verbosity -> T.Text 24 | verbosity Normal = "false" 25 | verbosity Verbose = "true" 26 | 27 | cols :: Columns -> T.Text 28 | cols h = T.pack h 29 | --------------------------------------------------------------------------------