Skip to content

Commit f604c77

Browse files
committed
initial commit
0 parents  commit f604c77

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "debug",
12+
"program": "${workspaceFolder}/cmd/ulid-cli/",
13+
"cwd": "${workspaceFolder}",
14+
"env": {},
15+
"args": []
16+
}
17+
]
18+
}

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ulid-cli
2+
3+
## Motivation
4+
5+
Interconversion between ULID strings and [16]bytes.
6+
7+
## Install
8+
9+
```
10+
go install https://github.com/Tim0401/ulid-cli/cmd/ulid-cli@{version}
11+
```
12+
13+
## Usage
14+
15+
string->byte
16+
ex) 01FM73V2R6A8G3T20KQ9SQDM6D
17+
```
18+
ulid-cli -sb ${ULID}
19+
```
20+
21+
byte->string
22+
ex) 0x17d0e3d8b0652203d0813ba7376d0cd
23+
```
24+
ulid-cli -bs ${ULID}
25+
```
26+
27+
## TODO
28+
29+
- テストを書く
30+
- ロジックのmain.goからの分離

cmd/ulid-cli/main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"strings"
9+
10+
"github.com/oklog/ulid/v2"
11+
)
12+
13+
func main() {
14+
var (
15+
sb = flag.Bool("sb", false, "string->byte")
16+
bs = flag.Bool("bs", false, "byte->string")
17+
)
18+
flag.Parse()
19+
if !(*sb || *bs) || (*sb && *bs) {
20+
log.Fatal("Specify either -sb or -bs")
21+
}
22+
arg := flag.Arg(0)
23+
24+
// string->byte
25+
if *sb {
26+
id, err := ulid.ParseStrict(arg)
27+
if err != nil {
28+
log.Fatal("Input error")
29+
}
30+
fmt.Println(hex.EncodeToString(id[:]))
31+
return
32+
}
33+
34+
// byte->string
35+
if *bs {
36+
arg = strings.TrimPrefix(arg, "0x")
37+
slice, err := hex.DecodeString(arg)
38+
if err != nil || len(slice) != 16 {
39+
log.Fatal("Input error")
40+
}
41+
array := [16]byte{}
42+
copy(array[:], slice)
43+
str := ulid.ULID(array).String()
44+
fmt.Println(str)
45+
return
46+
}
47+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/Tim0401/ulid-cli
2+
3+
go 1.17
4+
5+
require github.com/oklog/ulid/v2 v2.0.2

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/oklog/ulid/v2 v2.0.2 h1:r4fFzBm+bv0wNKNh5eXTwU7i85y5x+uwkxCUTNVQqLc=
2+
github.com/oklog/ulid/v2 v2.0.2/go.mod h1:mtBL0Qe/0HAx6/a4Z30qxVIAL1eQDweXq5lxOEiwQ68=
3+
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=

0 commit comments

Comments
 (0)