0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

小技まとめ

Last updated at Posted at 2022-04-21

マネージャー:「なんか記事書いてよ」
自分:「分報チャネルのやつ適当に抜粋して良い?」
マネージャー:「OK」

GitHub のリポジトリにまとめて Autolink をつける

JIRA-123https://example.com/PJ-123 みたいなかんじにできる。

# 対象のリポジトリを抽出
gh repo list ${GITHUB_ORG} --limit 100 --json name --jq '.[].name' > repos.txt
# API で設定
cat repos.txt | xargs -I{} -L 1 gh api -X POST /repos/{}/autolinks \
    -f key_prefix="JIRA-" \
    -f url_template="https://example.com/PJ-<num>"

brew で入れた Python の pip に入れたコマンド問題

Python を brew で入れて PATH 通しても pip で入れたコマンドには通らない問題、 pip show とかで確認すれば対処療法的にはわかるんだけど、これシンボリックリンクをたどった実体と同じ場所なのか。

PATH="$(dirname $(readlink -f $(which python3))):${PATH}"

これなら brew link で変えてもいちいち書き換えなくて良い。

GitHub Pages の URL を CLI で取得

gh api "repos/{owner}/{repo}/pages" -q .html_url

git stash -m

git stash-m でコメント付けられるのか。 list で見た時に何 push したか思い出しやすくなる。

白背景を余白付きでトリミングして透過

convert input.png -trim -bordercolor "white" -border 16x16 -fuzz 10% -transparent white output.png

月末を生成する

1 日 0:00 の 1s 前を計算すると 28, 29, 30, 31 の違いを吸収できる。

for i in {1..12}; do; date --utc --date "2019-01-01T00:00:00Z + $i month - 1 second"; done

出力例:

木  1 31 23:59:59 UTC 2019
木  2 28 23:59:59 UTC 2019
日  3 31 23:59:59 UTC 2019
火  4 30 23:59:59 UTC 2019
金  5 31 23:59:59 UTC 2019
日  6 30 23:59:59 UTC 2019
水  7 31 23:59:59 UTC 2019
土  8 31 23:59:59 UTC 2019
月  9 30 23:59:59 UTC 2019
木 10 31 23:59:59 UTC 2019
土 11 30 23:59:59 UTC 2019
火 12 31 23:59:59 UTC 2019

git archive/git bundle

git archive HEAD --output=name-$(git rev-parse --short HEAD).zip でリポジトリの内容を name-[SHA].zip にできる。 gitignore を考慮してくれるので便利。

git bundle を使うとリポジトリを 1 ファイルにおさめてくれる。受け取った側は git clone で展開できる。相手に合わせて使い分けると良さげ。

zsh で10進数→16進数

echo "$(([##16]248))"

Data Watchpoint and Trace Unit の Cycle Count Register

Cortex M4 には 0xE0001004 に CPU クロックの起動時点からのカウントが入っているらしい。

#define DWT_CYCCNT ((volatile uint32_t*) 0xE0001004)
#define CPU_CYCLES (*DWT_CYCCNT)

DWT_CTRL (0xE0001000) で停止・再開も。長い時間を計測するならオーバーフローに注意。

adb push

PC から Android にファイル送るのに adb push が便利。

2 の 10 乗

2 の 10 乗が 1024 なので 10 乗ごとに 10 進数だと 3 つぐらいゼロが増えるって覚えておくとスケールがわかって若干便利。

オブジェクトファイルからアセンブリを出力

objdump -dx filename.c.o > filename.c.s

merge commit を cherry pick

git cherry-pick -m 1 SHA

Pull Request と同じパッチを別のブランチにもう一度当てたいなら 1 。

C の static

C 言語でグローバル変数は、

  • .h に extern で宣言する
  • .c で定義する

が通常の方法。

よくある間違は、ヘッダに static で定義する。これだと include した .c (間接的も含む)ごとに変数が作られる。このときの static は、コンパイル単位( .c ファイルごと)で閉じた変数を定義することを示す。

まぁそもそもグローバル変数避けたいですけどね。

マージ済み・未マージのブランチ

git branch --merged, git branch --no-merged で現在のブランチに対してマージ済み・未マージのブランチが表示できる。便利。

jq で array から複数の要素を抜き出してbashで処理するやつ

jq -r '[.[] | [.a, .b]] | map(join("\n"))[]' | \
   while read A && read B; do
       echo "a: $A, b: $B"
   done

入出例:

[{"a": "value-a1", "b": "value-b1"}, {"a": "value-a2", "b": "value-b2", "c": "value-c1"}]

出力例

a: value-a1, b: value-b1
a: value-a2, b: value-b2

そこまでやるなら Shell じゃなくて pandas の from_records とか使ったほうが良い。シェル芸人向け。

jq の shebang

長くなりがちな jq のフィルタは #!/usr/bin/env jq -rMf を先頭に書いたファイルでスクリプトにできる。

そこまでやるなら jq じゃなくて(以下略

Basic認証の文字列を得るやつ

(tr -d "\n" | base64 -w0) <<< user:password

GitHub の Organization にタグをまとめて付ける

  1. https://github.com/settings/tokens で Personal access tokens 作る
  2. curl -u Username:トークン https://api.github.com/orgs/組織名/repos | jq -r '.[].name' > repos.txt みたいな感じで対象のリポジトリを作成
  3. xargs -I{} curl -X POST -sSf -u Username:トークン "https://api.github.com/repos/組織名/{}/labels" -d '{"name": "ラベル", "color": "色(#無しhex6桁)"}' < repos.txt

更新・削除も 3 みたいな感じで PUT/DELETE する。

今なら gh コマンド (https://github.com/cli/cli) でもうちょっと楽に書けるはず。

Docker for Mac の VM のシェルを開く

screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty

Ctrl+A, \ で抜ける

kubectl で Job の完了まで待つ

kubectl wait --for=condition=complete --timeout=30s job/myjob

.zshrc

utisam/dotfiles に公開しているにと同じものです。

.git/hooks/post-commit

.git/hooks/post-commit に設定するスクリプトです。

#!/bin/sh

echo ""
echo "    イエーイ☆ -(ノ゚Д゚)人(゚Д゚ )ノイエーイ☆"
echo ""
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?