Read settings.json and list the directory

-basically chat-gpt bootstrapping this script
This commit is contained in:
towards-a-new-leftypol 2023-10-04 15:23:58 -04:00
parent 5466849e33
commit 5af7924779
4 changed files with 61 additions and 5 deletions

View File

@ -1,4 +1,5 @@
{ {
"postgrest_url": "http://localhost:3000", "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"
} }

View File

@ -71,7 +71,11 @@ executable chan-delorean
-- other-extensions: -- other-extensions:
-- Other library packages from which modules are imported. -- 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. -- Directories containing source files.
hs-source-dirs: src hs-source-dirs: src

View File

@ -4,7 +4,7 @@ let
inherit (nixpkgs) pkgs; inherit (nixpkgs) pkgs;
f = { mkDerivation, base, stdenv, cabal-install, f = { mkDerivation, base, stdenv, cabal-install,
aeson, safe-exceptions aeson, safe-exceptions, bytestring, cmdargs
}: }:
mkDerivation { mkDerivation {
pname = "chan-delorean"; pname = "chan-delorean";
@ -13,7 +13,7 @@ let
isLibrary = false; isLibrary = false;
isExecutable = true; isExecutable = true;
executableHaskellDepends = [ executableHaskellDepends = [
base safe-exceptions aeson base safe-exceptions aeson bytestring cmdargs
]; ];
testHaskellDepends = [ cabal-install ]; testHaskellDepends = [ cabal-install ];
license = "unknown"; license = "unknown";

View File

@ -1,4 +1,55 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where 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 :: 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