hocker/docker2nix/Main.hs

80 lines
2.9 KiB
Haskell
Raw Permalink Normal View History

2016-10-20 23:47:41 +03:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS -fno-warn-orphans #-}
{-# OPTIONS -fno-warn-unused-do-bind #-}
2016-10-20 23:47:41 +03:00
-----------------------------------------------------------------------------
-- |
-- Module : docker2nix/Main
-- Copyright : (C) 2016 Awake Networks
2017-05-15 00:08:32 +03:00
-- License : Apache-2.0
2016-10-20 23:47:41 +03:00
-- Maintainer : Awake Networks <opensource@awakenetworks.com>
-- Stability : stable
----------------------------------------------------------------------------
module Main where
import Data.ByteString.Lazy.Char8 as C8L
import Data.Maybe (fromMaybe)
import Data.Text (Text)
2016-10-20 23:47:41 +03:00
import Options.Generic
import System.IO (hWaitForInput, stdin)
2016-10-20 23:47:41 +03:00
import Data.Docker.Image.Types
import Data.Docker.Nix.FetchDocker as Nix.FetchDocker
import Hocker.Lib
import Network.Wreq.Docker.Registry as Docker.Registry
import Hocker.Types
import Hocker.Types.ImageName
import Hocker.Types.ImageTag
2016-10-20 23:47:41 +03:00
-- | Top-level optparse-generic CLI args data type and specification.
data ProgArgs w = ProgArgs
{ -- | URI for the registry, optional
registry :: w ::: Maybe RegistryURI
<?> "URI of registry, defaults to the Docker Hub registry"
-- | Filepath to a file containing the manifest JSON
, manifest :: w ::: Maybe FilePath
<?> "Fetch image manifest from a path on the filesystem"
-- | Alternative docker image name made available in the Nix
-- expression fetchdocker derivation
, altImageName :: w ::: Maybe Text
2016-10-20 23:47:41 +03:00
<?> "Alternate image name provided in the `fetcdocker` derivation"
-- | Docker image name (includes the reponame, e.g: library/debian)
, name :: ImageName
-- | Docker image tag
, imageTag :: ImageTag
} deriving (Generic)
instance ParseRecord (ProgArgs Wrapped)
deriving instance Show (ProgArgs Unwrapped)
progSummary :: Text
2016-10-20 23:47:41 +03:00
progSummary = "Produce a Nix expression given a manifest for a docker image via stdin or via a filepath"
main :: IO ()
main = unwrapRecord progSummary >>= \ProgArgs{..} -> do
let (imageRepo, imageName) = Hocker.Lib.splitRepository name
2016-10-20 23:47:41 +03:00
dockerRegistry = fromMaybe defaultRegistry registry
manifestJSON <-
case manifest of
Just f -> C8L.readFile f
Nothing -> do
let h = stdin
hWaitForInput h (-1)
C8L.hGetContents h
exprs <- Nix.FetchDocker.generate HockerImageMeta{..}
either (Hocker.Lib.exitProgFail . show) Hocker.Lib.pprintNixExpr exprs