-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase62file.go
More file actions
86 lines (70 loc) · 2.51 KB
/
base62file.go
File metadata and controls
86 lines (70 loc) · 2.51 KB
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
83
84
85
86
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
const base62Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// int64をbase62文字列に変換する関数
func toBase62(num int64) string {
if num == 0 {
return string(base62Chars[0])
}
var result strings.Builder
base := int64(len(base62Chars))
for num > 0 {
remainder := num % base
result.WriteByte(base62Chars[remainder]) // 文字を追加
num /= base
}
// 結果は逆順になっているため、元に戻す
runes := []rune(result.String())
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func main() {
// 1. コマンドライン引数を確認
if len(os.Args) != 3 {
// os.Args[0] はプログラム名なので、引数は2つ必要
fmt.Fprintf(os.Stderr, "使用法: %s [file extension] [dist directory]\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "例: %s txt ./output\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
fileExt := os.Args[1]
distDir := os.Args[2]
// 拡張子の先頭にドットが付いている場合は削除 (例: ".txt" -> "txt")
if strings.HasPrefix(fileExt, ".") {
fileExt = fileExt[1:]
}
// 拡張子が空の場合はエラー
if fileExt == "" {
fmt.Fprintln(os.Stderr, "エラー: ファイル拡張子は空にできません。")
os.Exit(1)
}
// 2. 現在のUNIX時間を取得
unixTime := time.Now().Unix()
// 3. UNIX時間をbase62に変換
base62Name := toBase62(unixTime)
// 4. ファイル名とパスを構築
fileNameWithExt := fmt.Sprintf("%s.%s", base62Name, fileExt)
fullPath := filepath.Join(distDir, fileNameWithExt)
// 5. distディレクトリが存在しない場合は作成
// os.MkdirAll は親ディレクトリも必要に応じて作成し、ディレクトリが既に存在してもエラーにならない
err := os.MkdirAll(distDir, 0755) // 0755: rwxr-xr-x
if err != nil {
fmt.Fprintf(os.Stderr, "エラー: ディレクトリ %s の作成に失敗しました: %v\n", distDir, err)
os.Exit(1)
}
// 6. 空のファイルを作成
file, err := os.Create(fullPath)
if err != nil {
fmt.Fprintf(os.Stderr, "エラー: ファイル %s の作成に失敗しました: %v\n", fullPath, err)
os.Exit(1)
}
file.Close() // ファイルを作成するだけなので、すぐに閉じる
fmt.Printf("ファイルが正常に作成されました: %s\n", fullPath)
}