Skip to content

Commit f89eea9

Browse files
committed
feat: support csv out for show command (#275)
1 parent 1d61388 commit f89eea9

File tree

2 files changed

+84
-16
lines changed

2 files changed

+84
-16
lines changed

pkg/operator/allocations/show.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package allocations
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"math/big"
78
"sort"
9+
"strings"
810

911
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
1012
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common/flags"
@@ -22,11 +24,6 @@ import (
2224
"github.com/urfave/cli/v2"
2325
)
2426

25-
var (
26-
// PrecisionFactor comes from the allocation manager contract
27-
PrecisionFactor = big.NewInt(1e18)
28-
)
29-
3027
func ShowCmd(p utils.Prompter) *cli.Command {
3128
showCmd := &cli.Command{
3229
Name: "show",
@@ -197,7 +194,18 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
197194
if config.outputType == string(common.OutputType_Json) {
198195
slashableMagnitudeHolders.PrintJSON()
199196
} else {
200-
slashableMagnitudeHolders.PrintPretty()
197+
if !common.IsEmptyString(config.output) {
198+
if !strings.HasSuffix(config.output, ".csv") {
199+
return errors.New("output file must be a .csv file")
200+
}
201+
err = slashableMagnitudeHolders.WriteToCSV(config.output)
202+
if err != nil {
203+
return err
204+
}
205+
logger.Infof("Allocation state written to file: %s", config.output)
206+
} else {
207+
slashableMagnitudeHolders.PrintPretty()
208+
}
201209
}
202210

203211
if len(dergisteredOpsets) > 0 {

pkg/operator/allocations/types.go

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package allocations
22

33
import (
4+
"encoding/csv"
45
"encoding/json"
56
"fmt"
67
"math/big"
8+
"os"
9+
"reflect"
710
"strings"
811

912
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
@@ -139,16 +142,73 @@ type showConfig struct {
139142
type SlashableMagnitudeHolders []SlashableMagnitudesHolder
140143

141144
type SlashableMagnitudesHolder struct {
142-
StrategyAddress gethcommon.Address
143-
AVSAddress gethcommon.Address
144-
OperatorSetId uint32
145-
SlashableMagnitude uint64
146-
NewMagnitude uint64
147-
NewAllocationShares *big.Int
148-
UpdateBlock uint32
149-
Shares *big.Int
150-
SharesPercentage string
151-
UpcomingSharesPercentage string
145+
StrategyAddress gethcommon.Address `csv:"strategy_address"`
146+
AVSAddress gethcommon.Address `csv:"avs_address"`
147+
OperatorSetId uint32 `csv:"operator_set_id"`
148+
SlashableMagnitude uint64 `csv:"-"`
149+
NewMagnitude uint64 `csv:"-"`
150+
Shares *big.Int `csv:"shares"`
151+
SharesPercentage string `csv:"shares_percentage"`
152+
NewAllocationShares *big.Int `csv:"new_allocation_shares"`
153+
UpcomingSharesPercentage string `csv:"upcoming_shares_percentage"`
154+
UpdateBlock uint32 `csv:"update_block"`
155+
}
156+
157+
func (s SlashableMagnitudeHolders) WriteToCSV(filePath string) error {
158+
file, err := os.Create(filePath)
159+
if err != nil {
160+
return fmt.Errorf("failed to create file: %v", err)
161+
}
162+
defer file.Close()
163+
164+
writer := csv.NewWriter(file)
165+
defer writer.Flush()
166+
167+
// Get fields and their CSV names, excluding skipped fields
168+
var headers []string
169+
var fieldIndices []int
170+
val := reflect.ValueOf(SlashableMagnitudesHolder{})
171+
typ := val.Type()
172+
173+
for i := 0; i < typ.NumField(); i++ {
174+
field := typ.Field(i)
175+
csvTag := field.Tag.Get("csv")
176+
177+
// Skip if tag is "-"
178+
if csvTag == "-" {
179+
continue
180+
}
181+
182+
// Use tag value if present, otherwise use field name
183+
if csvTag != "" {
184+
headers = append(headers, csvTag)
185+
} else {
186+
headers = append(headers, field.Name)
187+
}
188+
fieldIndices = append(fieldIndices, i)
189+
}
190+
191+
// Write headers
192+
if err := writer.Write(headers); err != nil {
193+
return fmt.Errorf("failed to write headers: %v", err)
194+
}
195+
196+
// Write data rows
197+
for _, eachRow := range s {
198+
val := reflect.ValueOf(eachRow)
199+
row := make([]string, len(fieldIndices))
200+
// Only include non-skipped fields
201+
for i, fieldIndex := range fieldIndices {
202+
field := val.Field(fieldIndex)
203+
row[i] = fmt.Sprintf("%v", field.Interface())
204+
}
205+
206+
if err := writer.Write(row); err != nil {
207+
return fmt.Errorf("failed to write row: %v", err)
208+
}
209+
}
210+
211+
return nil
152212
}
153213

154214
func (s SlashableMagnitudeHolders) PrintPretty() {

0 commit comments

Comments
 (0)