Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom subcommands #80

Closed
srid opened this issue Jan 5, 2020 · 5 comments · Fixed by #113
Closed

Custom subcommands #80

srid opened this issue Jan 5, 2020 · 5 comments · Fixed by #113
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@srid
Copy link
Owner

srid commented Jan 5, 2020

Allow users to add custom subcommands to the built executable.

@srid srid added the enhancement New feature or request label Jan 5, 2020
@srid srid self-assigned this Jan 5, 2020
@srid srid added this to the 0.7 milestone Jan 5, 2020
@joelmccracken
Copy link

BTW this is something I want for myself too. Not sure what prompted you to make this issue but 👍

@srid
Copy link
Owner Author

srid commented Jan 6, 2020

I wanted to write a subcommand to be able to modify source files. Specifically to add a new entry to a .toml file (see below), but I imagine someone may want to use for other purposes, like create a new blog post draft with date and other template stuff automatically filled in.

Currently I hack around it like this:

main :: IO ()
main = do
  -- HACK: Until https://github.com/srid/rib/issues/80
  getArgs >>= \case
    ["tidbit", url] -> do
      -- TODO: Get og:title, og:image, etc..?
      today <- utctDay <$> getCurrentTime
      let tidbit = Tidbit today "TITLE" (toText url) [] Nothing
      modifyFile "a/tidbits.toml" $ \content ->
        toString $
          intercalate @Char
            "\n"
            [ "[[tidbit]]",
              toString $ Tidbit.toToml tidbit,
              content
            ]
    _ -> main'
  where
    modifyFile path f = do
      let tmpPath = path <> ".tmp"
      content <- f <$> readFile path
      writeFile tmpPath content
      copyFile tmpPath path
      removeFile tmpPath

main' :: IO ()
main' = Rib.run [reldir|a|] [reldir|b|] $ do
  Rib.buildStaticFiles [[relfile|static/**|]]
  [...]

@srid
Copy link
Owner Author

srid commented Feb 13, 2020

@srid srid modified the milestones: 0.7, 0.8 Feb 28, 2020
@srid srid pinned this issue Mar 3, 2020
@srid
Copy link
Owner Author

srid commented Mar 4, 2020

Turns out that this 'feature', as originally thought out, is unnecessarily complex as well.

All that rib needs to do is expose its own command line Parser type such that user applications can compose it as they wish, as part of their own CLI parsing mechanism.

@srid
Copy link
Owner Author

srid commented Mar 4, 2020

User-side composition will look like this:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants