From b5d61674d8820c289b774b3e6f5d2eb4d9fe791a Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Sun, 26 Nov 2023 20:44:12 +0100 Subject: [PATCH] Initial import --- .envrc | 1 + .gitignore | 3 ++ README.org | 3 ++ app/tk2mqtt.hs | 6 +++ flake.lock | 79 ++++++++++++++++++++++++++++++++++++++++ flake.nix | 78 +++++++++++++++++++++++++++++++++++++++ fourmolu.yaml | 2 + package.yaml | 64 ++++++++++++++++++++++++++++++++ src/TK2MQTT.hs | 26 +++++++++++++ src/TK2MQTT/Internal.hs | 2 + test/doctest/Doctest.hs | 10 +++++ test/spec/Spec.hs | 1 + test/spec/TK2MQTTSpec.hs | 12 ++++++ 13 files changed, 287 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 README.org create mode 100644 app/tk2mqtt.hs create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 fourmolu.yaml create mode 100644 package.yaml create mode 100644 src/TK2MQTT.hs create mode 100644 src/TK2MQTT/Internal.hs create mode 100644 test/doctest/Doctest.hs create mode 100644 test/spec/Spec.hs create mode 100644 test/spec/TK2MQTTSpec.hs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6622ec1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.pre-commit-config.yaml +result +.direnv diff --git a/README.org b/README.org new file mode 100644 index 0000000..5cfcbb5 --- /dev/null +++ b/README.org @@ -0,0 +1,3 @@ +#+TITLE: Tankerkönig To MQTT gateway + +* Tankerkönig to MQTT gateway diff --git a/app/tk2mqtt.hs b/app/tk2mqtt.hs new file mode 100644 index 0000000..438f579 --- /dev/null +++ b/app/tk2mqtt.hs @@ -0,0 +1,6 @@ +module Main (main) where + +import TK2MQTT qualified + +main :: IO () +main = TK2MQTT.defaultMain diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..fbc46f6 --- /dev/null +++ b/flake.lock @@ -0,0 +1,79 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1644229661, + "narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1661626419, + "narHash": "sha256-CjdPtdwH7I5Es4SjdCGuNfeulIyaM1LP0dXGWi4dyuQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a5e05d62460ff0b7627559a8b55ab421041ebf0a", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1660830093, + "narHash": "sha256-HUhx3a82C7bgp2REdGFeHJdhEAzMGCk3V8xIvfBqg1I=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "8cb8ea5f1c7bc2984f460587fddd5f2e558f6eb8", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..bab44d0 --- /dev/null +++ b/flake.nix @@ -0,0 +1,78 @@ +{ + description = "Tankerkönig-to-MQTT gateway"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + + flake-utils.url = "github:numtide/flake-utils"; + + pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; + pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { self, nixpkgs, flake-utils, pre-commit-hooks }: + flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + overlay = final: prev: { + tk2mqtt = final.callCabal2nix "tk2mqtt" ./. { }; + }; + + haskellPackages = pkgs.haskell.packages.ghc924.extend overlay; + in { + packages.default = haskellPackages.tk2mqtt; + + apps = { + # run with: nix run #.tk2mqtt + tk2mqtt = { + type = "app"; + program = "${self.packages.${system}.default}/bin/tk2mqtt"; + }; + + # run with: nix run + default = self.apps.${system}.tk2mqtt; + }; + + checks = { + pre-commit-check = pre-commit-hooks.lib.${system}.run { + src = ./.; + settings = { ormolu.defaultExtensions = [ "GHC2021" ]; }; + tools.fourmolu = haskellPackages.fourmolu; + hooks = { + nixfmt.enable = true; + fourmolu.enable = true; + hpack.enable = true; + hlint.enable = true; + doctest = { + enable = false; + name = "Run documentation tests"; + entry = "${haskellPackages.doctest}/bin/doctest src app"; + files = "\\.l?hs$"; + pass_filenames = false; + }; + }; + }; + }; + + devShells.default = haskellPackages.shellFor { + inherit (self.checks.${system}.pre-commit-check) shellHook; + + packages = p: [ p.tk2mqtt ]; + + withHoogle = true; + + nativeBuildInputs = with pkgs; [ + haskellPackages.haskell-language-server + haskellPackages.fourmolu + haskellPackages.hspec-discover + haskellPackages.doctest + cabal-install + ghcid + nixfmt + hpack + hlint + ]; + }; + }); +} diff --git a/fourmolu.yaml b/fourmolu.yaml new file mode 100644 index 0000000..d8c5559 --- /dev/null +++ b/fourmolu.yaml @@ -0,0 +1,2 @@ +haddock-style: single-line +indentation: 2 diff --git a/package.yaml b/package.yaml new file mode 100644 index 0000000..ab369cd --- /dev/null +++ b/package.yaml @@ -0,0 +1,64 @@ +name: tk2mqtt +version: 0.0.1.0 +license: MIT +author: "Alexander Kobjolke" +maintainer: "alex@jakalx.net" +copyright: "Alexander Kobjolke 2023" +description: "Tankerkönig-to-MQTT gateway" + +extra-source-files: + - README.org + +dependencies: + - base >= 4.13 && < 5 + - time + - directory + - containers + - net-mqtt + - req + - aeson + - text +# - bytestring + +ghc-options: + - -Wall + - -fdefer-typed-holes + +default-extensions: + - BlockArguments + - OverloadedStrings + - ImportQualifiedPost + +library: + source-dirs: src + verbatim: + default-language: GHC2021 + +executables: + tk2mqtt: + source-dirs: app + main: tk2mqtt.hs + dependencies: + - tk2mqtt + verbatim: + default-language: GHC2021 + +tests: + spec: + cpp-options: -DTEST + main: Spec.hs + source-dirs: + - test/spec + dependencies: + - tk2mqtt + - hspec + - QuickCheck + - quickcheck-instances + build-tools: hspec-discover + verbatim: + default-language: GHC2021 + doctest: + main: Doctest.hs + source-dirs: + - test/doctest + build-tools: doctest diff --git a/src/TK2MQTT.hs b/src/TK2MQTT.hs new file mode 100644 index 0000000..1fbaeb8 --- /dev/null +++ b/src/TK2MQTT.hs @@ -0,0 +1,26 @@ +{-# LANGUAGE RecordWildCards #-} + +module TK2MQTT ( + runTK2MQTT, + defaultMain, +) where + +import System.Environment qualified as Env +import System.IO + +import Control.Exception qualified as Exception +import System.IO.Error qualified as IOError + +runTK2MQTT :: IO () +runTK2MQTT = do + putStrLn "Hello" + +eitherToError :: Show a => Either a b -> IO b +eitherToError = either (Exception.throwIO . IOError.userError . show) return + +handleError :: IOError -> IO () +handleError e = do + hPutStrLn stderr $ "I ran into an issue: " <> show e + +defaultMain :: IO () +defaultMain = Exception.catch runTK2MQTT handleError diff --git a/src/TK2MQTT/Internal.hs b/src/TK2MQTT/Internal.hs new file mode 100644 index 0000000..a7cb9b8 --- /dev/null +++ b/src/TK2MQTT/Internal.hs @@ -0,0 +1,2 @@ +-- | Internal module in order to facilitate testability. +module TK2MQTT.Internal where diff --git a/test/doctest/Doctest.hs b/test/doctest/Doctest.hs new file mode 100644 index 0000000..72c84cf --- /dev/null +++ b/test/doctest/Doctest.hs @@ -0,0 +1,10 @@ +-- | Rn the doctest executable +module Main where + +import System.Process (callProcess) + +doctest :: [String] -> IO () +doctest = callProcess "doctest" + +main :: IO () +main = doctest ["--fast", "src"] diff --git a/test/spec/Spec.hs b/test/spec/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/test/spec/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} diff --git a/test/spec/TK2MQTTSpec.hs b/test/spec/TK2MQTTSpec.hs new file mode 100644 index 0000000..66ed07b --- /dev/null +++ b/test/spec/TK2MQTTSpec.hs @@ -0,0 +1,12 @@ +module TK2MQTTSpec (spec) where + +import HCat.Internal + +import Test.Hspec +import Test.Hspec.QuickCheck (prop) + +spec :: Spec +spec = do + describe "parseArgs" do + it "returns the filename if given a single argument" do + True `shouldBe` True