Skip to content

Commit

Permalink
fuckport reborn
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Nov 27, 2024
1 parent a5e6c1b commit 56d96be
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions packages/fuckport.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
{ writeShellApplication, jc, jq, lsof, ... }:
# https://x.com/sridca/status/1861875950897578113
{ writers, haskellPackages, coreutils, jc, lsof, ... }:

writeShellApplication {
name = "fuckport";
runtimeInputs = [ jc jq lsof ];
meta.description = ''
Kill the process with the port open
writers.writeHaskellBin "fuckport"
{
libraries = with haskellPackages; [
shh
aeson
];
} ''
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DeriveAnyClass #-}
import Shh
import System.Environment
import Data.Aeson
import Data.Maybe
import GHC.Generics
import Control.Monad
Used only to kill stale ghc.
'';
# FIXME: This doesn't work when lsof returns *multiple* processes.
text = ''
lsof -i :"$1"
THEPID=$(lsof -i :"$1" | jc --lsof 2> /dev/null | jq '.[].pid')
echo "KILL $THEPID ?"
read -r
kill "$THEPID"
'';
}
loadFromBins ["${lsof}", "${coreutils}", "${jc}"]
data LsofRow = LsofRow
{ command :: String
, pid :: Int
, user :: String
}
deriving stock (Generic, Show)
deriving anyclass (FromJSON)
main :: IO ()
main = do
port <- Prelude.head <$> getArgs
s <- lsof "-i" (":" <> port) |> jc "--lsof" |> capture
let v = fromJust $ decode @[LsofRow] s
forM_ v $ \r -> do
putStrLn $ "Killing " <> show (pid r) <> " (" <> command r <> ")"
kill $ pid r
''

0 comments on commit 56d96be

Please sign in to comment.