-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (155 loc) · 4.01 KB
/
main.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"log"
"os"
"sort"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"github.com/rensr/spack/config"
"github.com/rensr/spack/parser"
"github.com/rensr/spack/printer"
"github.com/rensr/spack/solidity"
)
func main() {
if err := newSpackApp().Run(os.Args); err != nil {
log.Fatal(err)
}
}
func newSpackApp() *cli.App {
var configFile string
var readFromFile, unpacked bool
app := &cli.App{
Name: "Spack",
Usage: "pack Solidity structs",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "loads a Solidity struct from a file",
Destination: &readFromFile,
},
&cli.BoolFlag{
Name: "unpacked",
Aliases: []string{"u"},
Usage: "does not pack the struct",
Destination: &unpacked,
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "location of the config file",
Destination: &configFile,
},
},
Commands: []*cli.Command{
{
Name: "pack",
Aliases: []string{"p"},
Usage: "packs a Solidity struct",
Action: func(c *cli.Context) error {
appConfig, err := NewAppSettings(configFile, readFromFile, unpacked, c.Args())
if err != nil {
return err
}
result, err := pack(&appConfig)
if err != nil {
return err
}
fmt.Println(result)
return nil
},
},
{
Name: "count",
Aliases: []string{"c"},
Usage: "count the slots of the given struct",
Action: func(c *cli.Context) error {
appConfig, err := NewAppSettings(configFile, readFromFile, unpacked, c.Args())
if err != nil {
return err
}
result, err := count(&appConfig)
if err != nil {
return err
}
fmt.Println(result)
return nil
},
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
return app
}
type AppSettings struct {
readFromFile bool
unpacked bool
printer *printer.Printer
args cli.Args
}
func NewAppSettings(configFile string, readFromFile, unpacked bool, args cli.Args) (AppSettings, error) {
configuration := config.GetDefaultConfig()
// If the user specified a config file, load it
if configFile != "" {
globalConfig, err := config.LoadConfigFromFile(configFile)
if err != nil {
return AppSettings{}, err
}
configuration = globalConfig
}
newPrinter, err := printer.NewPrinter(configuration.PrintingConfig)
if err != nil {
return AppSettings{}, err
}
return AppSettings{
printer: &newPrinter,
args: args,
readFromFile: readFromFile,
unpacked: unpacked,
}, nil
}
func pack(settings *AppSettings) (string, error) {
solidityStruct, err := getStruct(settings)
if err != nil {
return "", errors.Wrap(err, "Error parsing struct")
}
if settings.unpacked {
solidityStruct.StorageSlots = packStructCurrentFieldOrder(solidityStruct.Fields)
return settings.printer.PrintSolidityStruct(solidityStruct), nil
}
solidityStruct.StorageSlots = packStructOptimal(solidityStruct.Fields)
return settings.printer.PrintSolidityStruct(solidityStruct), nil
}
func count(settings *AppSettings) (int, error) {
structDef, err := getStruct(settings)
if err != nil {
return 0, errors.Wrap(err, "Error parsing struct")
}
if settings.unpacked {
structDef.StorageSlots = packStructCurrentFieldOrder(structDef.Fields)
return len(structDef.StorageSlots), nil
}
structDef.StorageSlots = packStructOptimal(structDef.Fields)
return len(structDef.StorageSlots), nil
}
func getStruct(settings *AppSettings) (solidity.Struct, error) {
input := settings.args.Get(0)
if input == "" {
return solidity.Struct{}, errors.New("No input specified")
}
structString := input
if settings.readFromFile {
fileByes, err := os.ReadFile(input)
if err != nil {
panic(err)
}
structString = string(fileByes)
}
structDef, err := parser.ParseStruct(structString)
if err != nil {
return solidity.Struct{}, errors.Wrap(err, "Error parsing struct")
}
return structDef, nil
}