[#254] Revise dump-config command

Problem: xrefcheck does not allow to print the config to stdout instead
of writing it to a file. Also, it is easy to overwrite your changes by
mistake by executing the command again.

Solution: provide a --stdout flag to print the config to stdout, and do
not write it to a file unless a --force flag has been included.
This commit is contained in:
Adrián Enríquez 2022-12-22 13:20:05 +01:00
parent 16041d0afd
commit b30413dd41
No known key found for this signature in database
GPG Key ID: 1D2A049F5866F977
7 changed files with 116 additions and 4 deletions

View File

@ -41,6 +41,9 @@ Unreleased
* [#231](https://github.com/serokell/xrefcheck/pull/231)
+ Anchor analysis takes now into account the appropriate case-sensitivity depending on
the configured Markdown flavour.
* [#254](https://github.com/serokell/xrefcheck/pull/254)
+ Now the `dump-config` command does not overwrite a file unless explicitly told with a
`--force` flag. Also, a `--stdout` flag allows to print the config to stdout instead.
0.2.2
==========

View File

@ -10,7 +10,8 @@ import Universum
import Main.Utf8 (withUtf8)
import System.IO.CodePage (withCP65001)
import Xrefcheck.CLI (Command (..), getCommand)
import System.Directory (doesFileExist)
import Xrefcheck.CLI (Command (..), DumpConfigMode (..), getCommand)
import Xrefcheck.Command (defaultAction)
import Xrefcheck.Config (defConfigText)
@ -20,5 +21,10 @@ main = withUtf8 $ withCP65001 $ do
case command of
DefaultCommand options ->
defaultAction options
DumpConfig repoType path ->
DumpConfig repoType (DCMFile forceFlag path) -> do
whenM ((not forceFlag &&) <$> doesFileExist path) do
putTextLn "Output file exists. Use --force to overwrite."
exitFailure
writeFile path (defConfigText repoType)
DumpConfig repoType DCMStdout ->
putStr (defConfigText repoType)

View File

@ -130,6 +130,7 @@ executables:
dependencies:
- xrefcheck
- universum
- directory
- with-utf8
- code-page

View File

@ -9,6 +9,7 @@ module Xrefcheck.CLI
( VerifyMode (..)
, ExclusionOptions (..)
, Command (..)
, DumpConfigMode (..)
, Options (..)
, NetworkingOptions (..)
@ -70,7 +71,11 @@ modes =
data Command
= DefaultCommand Options
| DumpConfig Flavor FilePath
| DumpConfig Flavor DumpConfigMode
data DumpConfigMode
= DCMFile Bool FilePath
| DCMStdout
data Options = Options
{ oConfigPath :: Maybe FilePath
@ -218,7 +223,7 @@ dumpConfigOptions = hsubparser $
info parser $
progDesc "Dump default configuration into a file."
where
parser = DumpConfig <$> repoTypeOption <*> outputOption
parser = DumpConfig <$> repoTypeOption <*> mode
repoTypeOption =
option repoTypeReadM $
@ -231,6 +236,22 @@ dumpConfigOptions = hsubparser $
Case insensitive.
|]
mode =
stdoutMode <|> fileMode
fileMode =
DCMFile <$> forceMode <*> outputOption
stdoutMode =
flag' DCMStdout $
long "stdout" <>
help "Write the config file to stdout."
forceMode =
switch $
long "force" <>
help "Overwrite the config file if it already exists."
outputOption =
filepathOption $
short 'o' <>

View File

@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
#
# SPDX-License-Identifier: MPL-2.0

View File

@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
#
# SPDX-License-Identifier: MPL-2.0

View File

@ -0,0 +1,75 @@
#!/usr/bin/env bats
# SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
#
# SPDX-License-Identifier: MPL-2.0
load '../helpers/bats-support/load'
load '../helpers/bats-assert/load'
load '../helpers/bats-file/load'
load '../helpers'
@test "Dump config to stdout" {
to_temp xrefcheck dump-config --stdout -t GitHub
assert_diff ../../configs/github-config.yaml
}
@test "Dump config to existent default file error" {
run xrefcheck dump-config -t GitHub
assert_failure
assert_output "Output file exists. Use --force to overwrite."
}
@test "Dump config to existent file error" {
run xrefcheck dump-config -o .config.yaml -t GitHub
assert_failure
assert_output "Output file exists. Use --force to overwrite."
}
@test "Dump config to non existent default file" {
cd $TEST_TEMP_DIR
run xrefcheck dump-config -t GitHub
assert_success
assert_exists .xrefcheck.yaml
}
@test "Dump config to non existent file" {
cd $TEST_TEMP_DIR
run xrefcheck dump-config -o .config.yaml -t GitHub
assert_success
assert_exists .config.yaml
}
@test "Dump config to existent default file with force" {
cp .xrefcheck.yaml $TEST_TEMP_DIR
cd $TEST_TEMP_DIR
run xrefcheck dump-config -t GitHub --force
assert_success
assert_exists .xrefcheck.yaml
}
@test "Dump config to existent file with force" {
cp .config.yaml $TEST_TEMP_DIR
cd $TEST_TEMP_DIR
run xrefcheck dump-config -o .config.yaml -t GitHub --force
assert_success
assert_exists .config.yaml
}