packages feed

craft-0.0.0.1: src/Craft/Ubuntu.hs

module Craft.Ubuntu where

import           Control.Lens
import           Data.ByteString.Lens
import           Data.String.Utils    (replace)
import           Formatting

import           Craft
import           Craft.Apt            as Apt
import qualified Craft.File           as File


data PPA = PPA { _ppaURL :: String }
  deriving (Eq, Show)
makeLenses ''PPA


findPPAFiles :: PPA -> Craft [File]
findPPAFiles (PPA url) = do
  fs <- File.find $(mkAbsDir "/etc/apt/sources.list.d") ["-name", "*" ++ replace "/" "*" url ++ "*.list"]
  let nonEmpty = (> 0) . length . view (fileContent . _Just . unpackedChars)
  return $ filter nonEmpty fs


instance Craftable PPA PPA where
  watchCraft ppa@(PPA url) = do
    fs <- findPPAFiles ppa
    if null fs
    then do
      craft_ $ package "software-properties-common"
      exec_ "add-apt-repository" ["-y", "ppa:" ++ url]
      Apt.update
      return (Created, ppa)
    else return (Unchanged, ppa)


instance Destroyable PPA where
  watchDestroy ppa@(PPA url) = do
    fsBefore <- findPPAFiles ppa
    if null fsBefore
    then return (Unchanged, Nothing)
    else do
      craft_ $ package "software-properties-common"
      exec_ "add-apt-repository" ["-y", "-r", "ppa:" ++ url]
      fsAfter <- findPPAFiles ppa
      unless (null fsAfter) $
        $craftError $ formatToString ("destroy PPA `"%shown%"` failed! Found: "%shown) ppa fsAfter
      Apt.update
      return (Removed, Just ppa)