Skip to content

Commit c4a56a5

Browse files
committed
Add scrap hash to print its sha256 hash
1 parent 734e5bb commit c4a56a5

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

cmd/scrap/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var commands = []Command{
2121
{name: "eval", desc: "evaluates it", fn: evaluate},
2222
{name: "type", desc: "infers its type", fn: inferType},
2323
{name: "push", desc: "pushes it to the server", fn: pushScrap},
24+
{name: "hash", desc: "prints its sha256 hash", fn: hashScrap},
2425
}
2526

2627
var (
@@ -102,3 +103,10 @@ func pushScrap(args []string) {
102103
key := must(env.Push(scrap))
103104
fmt.Println(key)
104105
}
106+
107+
func hashScrap(args []string) {
108+
input := must(io.ReadAll(os.Stdin))
109+
env := makeEnv()
110+
scrap := must(env.Read(input))
111+
fmt.Println(scrap.Sha256())
112+
}

eval/env.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ type Scrap struct {
1717
value Value
1818
}
1919

20+
func (s Scrap) Sha256() string {
21+
return fmt.Sprintf("%x", sha256.Sum256(s.expr.Source.Bytes()))
22+
}
23+
2024
type Sha256Hash = [32]byte
2125

2226
type Environment struct {

0 commit comments

Comments
 (0)