-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcfdecode.go
48 lines (42 loc) · 916 Bytes
/
tcfdecode.go
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"github.com/LiveRamp/iabconsent"
"github.com/urfave/cli/v2"
)
func decodeTcf(consent string) string {
v2, _ := iabconsent.ParseV2(consent)
tcf2Json, _ := json.Marshal(v2)
return string(tcf2Json)
}
func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeCharDevice == 0
}
func main() {
app := &cli.App{
Name: "tcfdecode",
Usage: "decode tcf2 strings to json",
Action: func(cCtx *cli.Context) error {
if isInputFromPipe() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println(decodeTcf(scanner.Text()))
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading stdin:", err)
}
} else {
fmt.Println(decodeTcf(cCtx.Args().Get(0)))
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}