From 5af792477958438377ffc7ad29d4129aafd7f34b Mon Sep 17 00:00:00 2001 From: towards-a-new-leftypol Date: Wed, 4 Oct 2023 15:23:58 -0400 Subject: [PATCH] Read settings.json and list the directory -basically chat-gpt bootstrapping this script --- backfill_settings.json | 3 ++- chan-delorean.cabal | 6 ++++- shell.nix | 4 ++-- src/Backfill.hs | 53 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/backfill_settings.json b/backfill_settings.json index da91a37..087008a 100644 --- a/backfill_settings.json +++ b/backfill_settings.json @@ -1,4 +1,5 @@ { "postgrest_url": "http://localhost:3000", - "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiY2hhbl9hcmNoaXZlciJ9.rGIKZokTDKTuQLIv8138bUby5PELfDipYYIDpJzH02c" + "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiY2hhbl9hcmNoaXZlciJ9.rGIKZokTDKTuQLIv8138bUby5PELfDipYYIDpJzH02c", + "backup_read_root": "/home/phil/linixy/tmp/leftypol_back/lainchan.leftypol.org" } diff --git a/chan-delorean.cabal b/chan-delorean.cabal index da0d42e..78af0a6 100644 --- a/chan-delorean.cabal +++ b/chan-delorean.cabal @@ -71,7 +71,11 @@ executable chan-delorean -- other-extensions: -- Other library packages from which modules are imported. - build-depends: base ^>=4.16.4.0 + build-depends: base ^>=4.16.4.0, + aeson, + bytestring, + cmdargs, + directory -- Directories containing source files. hs-source-dirs: src diff --git a/shell.nix b/shell.nix index 9d6351c..8b37464 100644 --- a/shell.nix +++ b/shell.nix @@ -4,7 +4,7 @@ let inherit (nixpkgs) pkgs; f = { mkDerivation, base, stdenv, cabal-install, - aeson, safe-exceptions + aeson, safe-exceptions, bytestring, cmdargs }: mkDerivation { pname = "chan-delorean"; @@ -13,7 +13,7 @@ let isLibrary = false; isExecutable = true; executableHaskellDepends = [ - base safe-exceptions aeson + base safe-exceptions aeson bytestring cmdargs ]; testHaskellDepends = [ cabal-install ]; license = "unknown"; diff --git a/src/Backfill.hs b/src/Backfill.hs index 65ae4a0..9af07e1 100644 --- a/src/Backfill.hs +++ b/src/Backfill.hs @@ -1,4 +1,55 @@ +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE OverloadedStrings #-} + module Main where +import System.Exit +import Data.Aeson (FromJSON, decode) +import qualified Data.ByteString.Lazy as B +import System.Console.CmdArgs +import GHC.Generics +import System.Directory (listDirectory) + +data SettingsCLI = SettingsCLI + { jsonFile :: FilePath + } deriving (Show, Data, Typeable) + +data JSONSettings = JSONSettings + { postgrest_url :: String + , jwt :: String + , backup_read_root :: FilePath + } deriving (Show, Generic) + +instance FromJSON JSONSettings + +settingsCLI :: SettingsCLI +settingsCLI = SettingsCLI + { jsonFile = def &= args &= typ "settings-jsonfile-path" + } &= summary "Backfill v0.0.1" + + +-- Function to list all files and directories inside the backup_read_root +listBackupContents :: JSONSettings -> IO () +listBackupContents settings = + listDirectory (backup_read_root settings) >>= mapM_ print + main :: IO () -main = putStrLn "Hello, Haskell!" +main = do + settingsValue <- cmdArgs settingsCLI + let filePath = jsonFile settingsValue + if null filePath + then do + putStrLn "Error: No JSON settings file provided." + exitFailure + else do + putStrLn $ "Loading settings from: " ++ filePath + content <- B.readFile filePath + case decode content :: Maybe JSONSettings of + Nothing -> do + putStrLn "Error: Invalid JSON format." + exitFailure + Just settings -> do + putStrLn "JSON successfully read!" + print settings -- print the decoded JSON settings + listBackupContents settings