This repository was archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathformat
executable file
·82 lines (67 loc) · 2.12 KB
/
format
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
set -euo pipefail
# Run rustfmt on our code. Suitable for use in editor integrations as well as in
# CI.
#
# By setting this environment variable (as opposed to using the `+toolchain`
# syntax in the `cargo` command), we can automatically install the given
# toolchain if it doesn't already exist on the system. It'll take a while to
# download the first time, but thereafter, it will be quick!
#
# Note that you will need your Rustup profile set to something higher
# than "minimal" (e.g., "default"), since `rustfmt` isn't included in
# "minimal".
export RUSTUP_TOOLCHAIN="nightly-2022-11-02"
# Default to checking formatting in the absence of any options.
mode="check"
while (("$#")); do
case "$1" in
-e | --editor)
mode="editor"
shift
;;
-c | --check | --ci)
mode="check"
shift
;;
-u | --update)
mode="update"
shift
;;
-h | --help)
mode="help"
shift
;;
esac
done
if [ "${mode}" == "help" ]; then
cat >&2 << EOF
Usage: $0 <OPTIONS>
Options:
-c|--check|--ci: Check the formatting of all Rust code. Use this
in CI jobs. If no other options are given, this is the default
behavior.
-e|--editor: Run rustfmt on the contents of a file passed on
standard input; returns formatted code on standard output. Use
this for local editor integrations.
-h|--help: Print this help message.
-u|--update: Format all Rust code. Use this after updating the
nightly version of Rust used for formatting, updating
configuration options, or any other time you just want to make
sure all the code is up to date.
EOF
exit
fi
if [ "${mode}" == "editor" ]; then
# Pass the contents of an individual file on standard input and
# write formatted code back to standard output.
rustfmt < /dev/stdin
exit $?
fi
if [ "${mode}" == "update" ]; then
echo >&2 "$0: Updating all Rust code formatting"
cargo fmt --all
exit $?
fi
echo >&2 "$0: Checking all Rust code formatting"
cargo fmt --all -- --check