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?

Uithubライクのシェルスクリプトメモ

Posted at

はじめに

ローカルでディレクトリ構造をUithubのように一括で表示するツールが欲しかったので、
シェルスクリプトの練習を兼ねて作成してみました。
また、知識的な都合もあり、できるだけシンプルになるよう意識して作成しました。

Uithub
GitHubのリポジトリ内容をブラウザ上でテキスト化して表示できるサービス

出力例

$ uitnize

.
├─ main.tf
├─ output.tf
└─ variables.tf

0 directories, 3 files

------------------------------------------------------
./output.tf
------------------------------------------------------

     1 output "nat_gateway_id" {
     2   value = aws_nat_gateway.this.id
     3 }
     4
     5 output "eip_id" {
     6   value = aws_eip.nat.id
     7 }
     8

------------------------------------------------------
./variables.tf
------------------------------------------------------

     1 variable "vpc_id" {
     2   type = string
     3 }
     4
     5 variable "private_subnet_ids" {
     6   type = list(string)
     7 }
     8

------------------------------------------------------
./main.tf
------------------------------------------------------

     1 resource "aws_nat_gateway" "this" {
     2   allocation_id = aws_eip.nat.id
     3   subnet_id     = var.public_subnet_ids[0]
     4 }
     5
     6 resource "aws_eip" "nat" {
     7   domain = "vpc"
     8 }
     9

機能

  • カレントディレクトリ以下の構造をツリー形式で表示(隠しファイルは無視)
  • ファイルごとに全文を表示
  • 行数の表示

本家にある、YAMLやJSONへの変換など上記以外の機能はありません。

作成手順

~/scriptsディレクトリの下に、スクリプトを記述するファイルuitnizeを作成します。
※ファイル名がスクリプトを実行するコマンドになります。

$ mkdir ~/scripts

$ cd scripts

$ vim uitnize

vimで下記内容を記述し、:wqで保存します。

#!/bin/bash

{
  printf "\n"
  tree -I ".*" 
  printf "\n\n"

  files=$(find . -type f ! -path "*/.*")

  for f in $files; do
    printf "%s\n" "----------------------------------------------------"
    printf "%s\n" "$f"
    printf "%s\n\n" "----------------------------------------------------"
    nl -ba "$f"
    printf "\n\n"
  done

} | less -R

ポイント

tree -I ".\*"
隠しディレクトリを除外してツリー表示

find . -type f ! -path "\*/.*"
隠しファイルを除外してファイル一覧を取得

nl -ba
空行も含めて行番号を追加

less -R
出力をページャで表示

スクリプトファイルに実行権限を付与します。

$ chmod +x uitnize

~/scriptsディレクトリを永続的に$PATH環境変数に登録します。

$ vim ~/.bashrc
#下記の1行を追記
 export PATH="$HOME/scripts:$PATH"

設定を反映します。

$ source ~/.bashrc

uitnizeで実行できます。

$ uitnize

まとめ

本家Uithubが使えなくなっていたので、作成してみました。

コピー自体は手動ですし、スクリプトを作るまでもないことかもしれませんが、意外と役に立っているので良かったです。

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?