Skip to content

Commit

Permalink
Use cobra for arg parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Feb 12, 2024
1 parent 23747b6 commit 32fafa3
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 353 deletions.
79 changes: 42 additions & 37 deletions compressor/cmd/czip-compressor/extras.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,53 @@ package main
import (
"fmt"

"github.com/0xsequence/czip/compressor"
encoder "github.com/0xsequence/czip/compressor"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/go-sequence"
"github.com/spf13/cobra"
)

func encodeExtras(args *ParsedArgs) (string, error) {
if len(args.Positional) < 3 {
return "", fmt.Errorf("usage: encode_extra <code> <hex>")
}

data := common.FromHex(args.Positional[2])
buf := encoder.NewBuffer(encoder.METHOD_DECODE_ANY, nil, ParseAllowOpcodes(args), ParseUseStorage(args))

var err error

switch args.Positional[1] {
case "FLAG_SEQUENCE_NESTED_N_WORDS":
buf.WriteNWords(data)
case "SEQUENCE_DYNAMIC_SIGNATURE_PART":
err = encodeSequenceDynamicSignaturePart(buf, data)
case "SEQUENCE_BRANCH_SIGNATURE_PART":
buf.WriteSequenceBranchSignaturePart(data)
case "SEQUENCE_NESTED_SIGNATURE_PART":
err = encodeSequenceNestedSignaturePart(buf, data)
case "SEQUENCE_CHAINED_SIGNATURE":
buf.WriteSequenceChainedSignature(data)
case "FLAG_SEQUENCE_SIG":
buf.WriteSequenceSignature(data, false)
case "FLAG_SEQUENCE_EXECUTE":
err = encodeSequenceExecute(buf, data)
case "FLAG_SEQUENCE_SELF_EXECUTE":
err = encodeSequenceSelfExecute(buf, data)

default:
return "", fmt.Errorf("unknown extra: %s", args.Positional[1])
}

if err != nil {
return "", err
}

return fmt.Sprintf("0x%x", buf.Commited), nil
var extrasCmd = &cobra.Command{
Use: "extras",
Short: "Additional encoding methods, used for testing and debugging.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
data := common.FromHex(args[1])

buf, err := useBuffer(compressor.METHOD_DECODE_ANY, cmd)
if err != nil {
fail(err)
}

switch args[0] {
case "FLAG_SEQUENCE_NESTED_N_WORDS":
buf.WriteNWords(data)
case "SEQUENCE_DYNAMIC_SIGNATURE_PART":
err = encodeSequenceDynamicSignaturePart(buf, data)
case "SEQUENCE_BRANCH_SIGNATURE_PART":
buf.WriteSequenceBranchSignaturePart(data)
case "SEQUENCE_NESTED_SIGNATURE_PART":
err = encodeSequenceNestedSignaturePart(buf, data)
case "SEQUENCE_CHAINED_SIGNATURE":
buf.WriteSequenceChainedSignature(data)
case "FLAG_SEQUENCE_SIG":
buf.WriteSequenceSignature(data, false)
case "FLAG_SEQUENCE_EXECUTE":
err = encodeSequenceExecute(buf, data)
case "FLAG_SEQUENCE_SELF_EXECUTE":
err = encodeSequenceSelfExecute(buf, data)

default:
fail(fmt.Errorf("invalid method: %s", args[0]))
}

if err != nil {
fail(err)
}

fmt.Printf("0x%x\n", buf.Commited)
},
}

func encodeSequenceDynamicSignaturePart(buf *encoder.Buffer, data []byte) error {
Expand Down
37 changes: 26 additions & 11 deletions compressor/cmd/czip-compressor/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"os"

"github.com/0xsequence/czip/compressor"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/spf13/cobra"
)

func LoadCachedData(path string) (*compressor.Indexes, error) {
Expand Down Expand Up @@ -87,17 +89,23 @@ func toHumanReadable(from *compressor.Indexes) *compressor.Indexes {
return next
}

func UseIndexes(ctx context.Context, args *ParsedArgs) (*compressor.Indexes, error) {
func UseIndexes(ctx context.Context, cmd *cobra.Command) (*compressor.Indexes, error) {
var indexes *compressor.Indexes

if ParseUseStorage(args) {
provider, err := ParseProvider(args)
useStorage, err := cmd.Flags().GetBool("use-storage")
if err != nil {
return nil, err
}

if useStorage {
providerUrl, err := cmd.Flags().GetString("provider")
if err != nil {
return nil, err
}

if provider == nil {
return nil, fmt.Errorf("provider is required")
provider, err := ethrpc.NewProvider(providerUrl)
if err != nil {
return nil, err
}

chainId, err := provider.ChainID(ctx)
Expand All @@ -106,23 +114,30 @@ func UseIndexes(ctx context.Context, args *ParsedArgs) (*compressor.Indexes, err
}

// Load the cache file
var path string
if flag, ok := args.Flags["cache-file"]; ok {
path = flag
} else {
path = fmt.Sprintf("/tmp/czip-indexes-%d.json", chainId)
cachePath, err := cmd.Flags().GetString("cache-dir")
if err != nil {
return nil, err
}

// If path does not exist, create it
err = ensureDir(cachePath)
if err != nil {
return nil, err
}

path := fmt.Sprintf("%s/czip-indexes-%d.json", cachePath, chainId)

indexes, err = LoadCachedData(path)
if err != nil {
return nil, err
}

contract, err := ParseContractAddress(args)
contractAddr, err := cmd.Flags().GetString("contract")
if err != nil {
return nil, err
}

contract := common.HexToAddress(contractAddr)
if contract == (common.Address{}) {
return nil, fmt.Errorf("contract address is required")
}
Expand Down
Loading

0 comments on commit 32fafa3

Please sign in to comment.