Skip to content

Commit

Permalink
If the keys have not changed, then avoid sudo. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
j6carey authored and Gabriella439 committed Apr 8, 2018
1 parent 7ba013f commit 6df18d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
1 change: 1 addition & 0 deletions nix-delegate.cabal
Expand Up @@ -13,6 +13,7 @@ Library
Hs-Source-Dirs: src
Exposed-Modules: Nix.Delegate
Build-Depends: base >= 4.7.0.0 && < 5
, bytestring >= 0.10.8.1 && < 1.0
, neat-interpolation < 0.4
, optparse-applicative >= 0.13.2.0 && < 0.15
, foldl < 1.4
Expand Down
52 changes: 32 additions & 20 deletions src/Nix/Delegate.hs
Expand Up @@ -33,6 +33,7 @@ import Turtle (ExitCode (..), FilePath, Line,

import qualified Control.Exception
import qualified Control.Foldl as Foldl
import qualified Data.ByteString.Lazy
import qualified Data.Text
import qualified NeatInterpolation
import qualified Options.Applicative as Options
Expand Down Expand Up @@ -260,24 +261,35 @@ exchangeKeys key host = do

liftIO (Control.Exception.handle handler1 download)

-- NB: path shouldn't is a FilePath and won't have any
-- newlines, so this should be okay
Turtle.err (Turtle.unsafeTextToLine $ Turtle.format ("[+] Installing: "%fp) path)

warnSudo

let install =
Turtle.procs "sudo"
[ "mv"
, Turtle.format fp localPath
, Turtle.format fp path
]
empty
let handler2 :: SomeException -> IO ()
handler2 e = do
let pathText = Turtle.format fp path
let exceptionText = Data.Text.pack (show e)
let msg = [NeatInterpolation.text|
new <- liftIO . Data.ByteString.Lazy.readFile . Data.Text.unpack $
Turtle.format fp localPath

old <- liftIO . Data.ByteString.Lazy.readFile . Data.Text.unpack $
Turtle.format fp path

if new == old
then do
let same = Turtle.format ("[+] Unchanged: "%fp) path
mapM_ Turtle.err (Turtle.Line.textToLines same)
else do
-- NB: path shouldn't is a FilePath and won't have any
-- newlines, so this should be okay
Turtle.err (Turtle.unsafeTextToLine $ Turtle.format ("[+] Installing: "%fp) path)

warnSudo

let install =
Turtle.procs "sudo"
[ "mv"
, Turtle.format fp localPath
, Turtle.format fp path
]
empty
let handler2 :: SomeException -> IO ()
handler2 e = do
let pathText = Turtle.format fp path
let exceptionText = Data.Text.pack (show e)
let msg = [NeatInterpolation.text|
[x] Could not install: $pathText

Debugging tips:
Expand All @@ -290,9 +302,9 @@ exchangeKeys key host = do

Original error: $exceptionText
|]
Turtle.die msg
Turtle.die msg

liftIO (Control.Exception.handle handler2 install)
liftIO (Control.Exception.handle handler2 install)

mirror privateKey
mirror publicKey
Expand Down

0 comments on commit 6df18d1

Please sign in to comment.