From 14fa8ec1484c530b469febed8800d0cb7e53f830 Mon Sep 17 00:00:00 2001 From: Parnell Springmeyer Date: Fri, 5 May 2023 11:21:47 -0700 Subject: [PATCH] docker2nix: `inprocWithErr` -> `procStrictWithErr` (#57) * docker2nix: `inprocWithErr` -> `procStrictWithErr` ... because `nix` utilities will sometimes return warnings and other information to `stderr`. The type of `inprocWithErr` makes it pretty clear that we should expect a result of _either_ the stderr text _or_ the stdout text. Not both! Normally, this is fine because errors will be returned on `stderr`, however this is problematic when nix outputs warnings as the utility will end up treating them as errors. So we need a function that will give us both and that's `procStrictWithErr`. * Fix CI * Cleanup a lot of annoying compiler warnings * Fixup prettyprinter deprecation warnings * One more prettyprinter fix * Swap the stdout and stderr return field bindings... * s/Prettyprint/Prettyprinter/ * Strip whitespace from result --- .github/workflows/ci.yml | 8 ++++---- src/Data/Docker/Nix/FetchDocker.hs | 3 +-- src/Data/Docker/Nix/Lib.hs | 13 ++++++------- src/Hocker/Lib.hs | 11 +++++------ src/Hocker/Types.hs | 2 -- src/Hocker/Types/Exceptions.hs | 1 - src/Hocker/Types/Hash.hs | 1 - src/Hocker/Types/ImageName.hs | 1 - src/Hocker/Types/ImageTag.hs | 1 - src/Hocker/Types/URI.hs | 9 ++++----- src/Network/Wreq/Docker/Image.hs | 3 +-- src/Network/Wreq/Docker/Image/Lib.hs | 1 - src/Network/Wreq/Docker/Registry.hs | 1 - src/Network/Wreq/ErrorHandling.hs | 1 - test/Tests/Data/Docker/Nix/FetchDocker.hs | 4 ++-- 15 files changed, 23 insertions(+), 37 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d409336..6bab331 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,11 +10,11 @@ jobs: steps: - uses: actions/checkout@v3.0.2 name: Checkout - - uses: cachix/install-nix-action@v17 + - uses: cachix/install-nix-action@v20 name: Install Nix with: nix_path: nixpkgs=./nix/pkgs.nix - - uses: cachix/cachix-action@v10 + - uses: cachix/cachix-action@v12 name: Set up Cachix with: name: awakesecurity @@ -27,11 +27,11 @@ jobs: steps: - uses: actions/checkout@v3.0.2 name: Checkout - - uses: cachix/install-nix-action@v17 + - uses: cachix/install-nix-action@v20 name: Install Nix with: nix_path: nixpkgs=./nix/pkgs.nix - - uses: cachix/cachix-action@v10 + - uses: cachix/cachix-action@v12 name: Set up Cachix with: name: awakesecurity diff --git a/src/Data/Docker/Nix/FetchDocker.hs b/src/Data/Docker/Nix/FetchDocker.hs index 06a082c..9875628 100644 --- a/src/Data/Docker/Nix/FetchDocker.hs +++ b/src/Data/Docker/Nix/FetchDocker.hs @@ -24,7 +24,6 @@ import qualified Data.Bifunctor as Bifunctor import Data.Coerce import Data.Fix import Data.Maybe -import Data.Semigroup ((<>)) import Data.Text (Text) import qualified Data.Text as Text import Data.Text.Encoding (decodeUtf8') @@ -40,7 +39,7 @@ import Network.Wreq.Docker.Registry (pluckLayersFrom) import Hocker.Types import Hocker.Types.Exceptions import Hocker.Types.ImageTag -import Text.Megaparsec.Pos (Pos, mkPos) +import Text.Megaparsec.Pos (Pos) -- | @hnix-0.5.0:inherit@ requires a source location as its final argument. inheritAdapter :: FilePath -> Pos -> Pos -> [NKeyName e] -> Binding e diff --git a/src/Data/Docker/Nix/Lib.hs b/src/Data/Docker/Nix/Lib.hs index b16926e..c69f0b1 100644 --- a/src/Data/Docker/Nix/Lib.hs +++ b/src/Data/Docker/Nix/Lib.hs @@ -2,7 +2,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE TupleSections #-} -{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleContexts #-} ----------------------------------------------------------------------------- -- | @@ -42,7 +42,7 @@ toBase32Nix (Base16Digest d16) = do let nixhash = Nix.Paths.nixHash let hockerExc m = HockerException m Nothing Nothing let convertDigest = - inprocWithErr + procStrictWithErr (Text.pack nixhash) [ "--type" , "sha256" @@ -58,8 +58,7 @@ toBase32Nix (Base16Digest d16) = do "nothing was returned by `nix-hash', not even an error" Nothing Nothing) - Just result -> - either - (throwError . hockerExc . Text.unpack . lineToText) - (return . Base32Digest . lineToText) - result + Just (ExitFailure _, _, errorText) -> + throwError (hockerExc (Text.unpack errorText)) + Just (ExitSuccess, resultText, _) -> + return (Base32Digest (Text.strip resultText)) diff --git a/src/Hocker/Lib.hs b/src/Hocker/Lib.hs index ec1274d..dca54b1 100644 --- a/src/Hocker/Lib.hs +++ b/src/Hocker/Lib.hs @@ -28,13 +28,12 @@ import Data.Aeson.Lens import qualified Data.ByteString.Char8 as C8 import Data.ByteString.Lazy.Char8 as C8L import Data.Coerce -import Data.Semigroup ((<>)) import Data.Text (Text) import qualified Data.Text as Text -import Data.Text.Prettyprint.Doc (LayoutOptions(..), +import Prettyprinter (LayoutOptions(..), PageWidth(..), SimpleDocStream) -import qualified Data.Text.Prettyprint.Doc -import qualified Data.Text.Prettyprint.Doc.Render.Text +import qualified Prettyprinter +import qualified Prettyprinter.Render.Text import Data.Text.Encoding (encodeUtf8) import qualified Network.Wreq as Wreq import Nix.Expr (NExpr) @@ -173,7 +172,7 @@ splitRepository (ImageName (Text.pack -> n)) = over _2 Text.tail $ Text.break (= -- | Given a nix expression AST, produce a pretty printer document. renderNixExpr :: NExpr -> SimpleDocStream ann renderNixExpr = - Data.Text.Prettyprint.Doc.layoutSmart layoutOptions . prettyNix + Prettyprinter.layoutSmart layoutOptions . prettyNix where layoutOptions = LayoutOptions { layoutPageWidth = AvailablePerLine 120 0.4 } @@ -181,7 +180,7 @@ renderNixExpr = -- printing renderer. pprintNixExpr :: NExpr -> IO () pprintNixExpr expr = - Data.Text.Prettyprint.Doc.Render.Text.renderIO System.IO.stdout stream + Prettyprinter.Render.Text.renderIO System.IO.stdout stream where stream = renderNixExpr expr diff --git a/src/Hocker/Types.hs b/src/Hocker/Types.hs index 52fcce7..da858b0 100644 --- a/src/Hocker/Types.hs +++ b/src/Hocker/Types.hs @@ -31,8 +31,6 @@ import Control.Monad.Reader.Class import qualified Crypto.Hash as Hash import qualified Data.ByteString.Lazy import Data.Char (toUpper) -import Data.Semigroup ((<>)) -import Data.Text (Text) import qualified Data.Text as Text import qualified Network.Wreq as Wreq import Network.Wreq.ErrorHandling diff --git a/src/Hocker/Types/Exceptions.hs b/src/Hocker/Types/Exceptions.hs index fc4fdec..90fbd34 100644 --- a/src/Hocker/Types/Exceptions.hs +++ b/src/Hocker/Types/Exceptions.hs @@ -19,7 +19,6 @@ module Hocker.Types.Exceptions where import Control.DeepSeq import Control.Exception -import Data.Semigroup ((<>)) import GHC.Generics data HockerException = HockerException diff --git a/src/Hocker/Types/Hash.hs b/src/Hocker/Types/Hash.hs index 4c39008..e917f7b 100644 --- a/src/Hocker/Types/Hash.hs +++ b/src/Hocker/Types/Hash.hs @@ -17,7 +17,6 @@ import qualified Crypto.Hash as Hash import qualified Data.ByteArray as BA import qualified Data.ByteArray.Encoding as BA import qualified Data.ByteString.Char8 as C8 -import Data.Semigroup ((<>)) import qualified Data.Text import qualified Options.Applicative as Options import Options.Generic diff --git a/src/Hocker/Types/ImageName.hs b/src/Hocker/Types/ImageName.hs index 1359709..d66a3c6 100644 --- a/src/Hocker/Types/ImageName.hs +++ b/src/Hocker/Types/ImageName.hs @@ -15,7 +15,6 @@ module Hocker.Types.ImageName where import Control.DeepSeq -import Data.Semigroup ((<>)) import qualified Options.Applicative as Options import Options.Generic diff --git a/src/Hocker/Types/ImageTag.hs b/src/Hocker/Types/ImageTag.hs index 3db9ac0..0958bfc 100644 --- a/src/Hocker/Types/ImageTag.hs +++ b/src/Hocker/Types/ImageTag.hs @@ -15,7 +15,6 @@ module Hocker.Types.ImageTag where import Control.DeepSeq -import Data.Semigroup ((<>)) import qualified Options.Applicative as Options import Options.Generic diff --git a/src/Hocker/Types/URI.hs b/src/Hocker/Types/URI.hs index fddaac7..c333263 100644 --- a/src/Hocker/Types/URI.hs +++ b/src/Hocker/Types/URI.hs @@ -16,7 +16,6 @@ module Hocker.Types.URI where import Control.Lens import qualified Data.ByteString.Char8 as C8 -import Data.Semigroup ((<>)) import qualified Data.Text as Text import qualified Options.Applicative as Options import Options.Applicative.Builder @@ -32,12 +31,12 @@ uriReader = Options.eitherReader parseURIArg instance ParseField (URIRef Absolute) where readField = uriReader - parseField help long short _value = + parseField helpMsg longStr shortStr _value = (Options.option uriReader $ ( Options.metavar "URI" - <> foldMap (Options.long . Text.unpack) long - <> foldMap Options.short short - <> foldMap (Options.help . Text.unpack) help + <> foldMap (Options.long . Text.unpack) longStr + <> foldMap Options.short shortStr + <> foldMap (Options.help . Text.unpack) helpMsg ) ) diff --git a/src/Network/Wreq/Docker/Image.hs b/src/Network/Wreq/Docker/Image.hs index e2dfa11..3cdc0c4 100644 --- a/src/Network/Wreq/Docker/Image.hs +++ b/src/Network/Wreq/Docker/Image.hs @@ -25,7 +25,6 @@ import Data.ByteString.Lazy.Char8 as C8L import Data.Coerce import Data.Either import Data.HashSet as Set -import Data.Semigroup ((<>)) import Data.Text (Text) import qualified Data.Text as Text import Data.Text.Encoding (decodeUtf8') @@ -129,7 +128,7 @@ fetchLayer = -- docker registry. fetchConfig :: HockerMeta -> IO (Either HockerException C8L.ByteString) fetchConfig = - runHocker $ ask >>= \HockerMeta{..} -> do + runHocker $ ask >>= \HockerMeta{} -> do configDigest <- fetchManifest >>= getConfigDigest . view Wreq.responseBody diff --git a/src/Network/Wreq/Docker/Image/Lib.hs b/src/Network/Wreq/Docker/Image/Lib.hs index c73828a..eaacc60 100644 --- a/src/Network/Wreq/Docker/Image/Lib.hs +++ b/src/Network/Wreq/Docker/Image/Lib.hs @@ -24,7 +24,6 @@ import Control.Monad.Reader import qualified Data.ByteString.Lazy.Char8 as C8L import Data.Coerce import qualified Data.HashMap.Strict as HashMap -import Data.Semigroup ((<>)) import qualified Data.Text as Text import qualified Network.Wreq as Wreq import qualified System.Directory as Directory diff --git a/src/Network/Wreq/Docker/Registry.hs b/src/Network/Wreq/Docker/Registry.hs index 2937962..114c5bc 100644 --- a/src/Network/Wreq/Docker/Registry.hs +++ b/src/Network/Wreq/Docker/Registry.hs @@ -34,7 +34,6 @@ import qualified Data.ByteString.Char8 as C8 import Data.Text.Encoding (decodeUtf8, encodeUtf8) import URI.ByteString import NeatInterpolation -import Data.Semigroup ((<>)) import qualified Data.Text as Text import qualified Network.Wreq as Wreq import System.Directory diff --git a/src/Network/Wreq/ErrorHandling.hs b/src/Network/Wreq/ErrorHandling.hs index f129064..2a0bc56 100644 --- a/src/Network/Wreq/ErrorHandling.hs +++ b/src/Network/Wreq/ErrorHandling.hs @@ -21,7 +21,6 @@ import Control.Exception.Lifted as Lifted import Control.Lens import Control.Monad.Except import Data.ByteString.Char8 as C8 -import Data.Semigroup ((<>)) import Network.HTTP.Client import Network.HTTP.Types.Status diff --git a/test/Tests/Data/Docker/Nix/FetchDocker.hs b/test/Tests/Data/Docker/Nix/FetchDocker.hs index 5d4177f..1cdee9b 100644 --- a/test/Tests/Data/Docker/Nix/FetchDocker.hs +++ b/test/Tests/Data/Docker/Nix/FetchDocker.hs @@ -19,7 +19,7 @@ import Data.ByteString as BS import Data.ByteString.Lazy.Char8 as C8L import Data.Either (either) import qualified Data.Text as Text -import qualified Data.Text.Prettyprint.Doc.Render.String +import qualified Prettyprinter.Render.String import Data.Word8 as W8 import Network.URI @@ -105,7 +105,7 @@ generateFetchDockerNix = do , altImageName = Nothing } - let display = Data.Text.Prettyprint.Doc.Render.String.renderString + let display = Prettyprinter.Render.String.renderString either (Hocker.Lib.die . Text.pack . show)