Skip to content

Commit

Permalink
Add Cmd (#164)
Browse files Browse the repository at this point in the history
* First iteration of the cmd

* Update docs

* Run plausible only on prod
  • Loading branch information
ferranbt authored Mar 5, 2022
1 parent 1621d9f commit 8907487
Show file tree
Hide file tree
Showing 22 changed files with 476 additions and 41 deletions.
32 changes: 4 additions & 28 deletions abigen/main.go → cmd/abigen/abigen.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package main
package abigen

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
Expand All @@ -15,32 +14,14 @@ import (
"github.com/umbracle/ethgo/compiler"
)

const (
version = "0.1.0"
)

func main() {
var sources string
var pckg string
var output string
var name string

flag.StringVar(&sources, "source", "", "List of abi files")
flag.StringVar(&pckg, "package", "main", "Name of the package")
flag.StringVar(&output, "output", "", "Output directory")
flag.StringVar(&name, "name", "", "name of the contract")

flag.Parse()

func Parse(sources string, pckg string, output string) error {
config := &config{
Package: pckg,
Output: output,
Name: name,
}

if sources == "" {
fmt.Println(version)
os.Exit(0)
return fmt.Errorf("no source")
}

for _, source := range strings.Split(sources, ",") {
Expand Down Expand Up @@ -70,10 +51,10 @@ func main() {
}
}
}
return nil
}

const (
vyExt = 0
solExt = 1
abiExt = 2
jsonExt = 3
Expand All @@ -93,8 +74,6 @@ func process(sources string, config *config) (map[string]*compiler.Artifact, err
ext = abiExt
case ".sol":
ext = solExt
case ".vy", ".py":
ext = vyExt
case ".json":
ext = jsonExt
default:
Expand Down Expand Up @@ -154,9 +133,6 @@ func processAbi(sources []string, config *config) (map[string]*compiler.Artifact
// bin not found
bin = []byte{}
}
if len(sources) == 1 && config.Name != "" {
name = config.Name
}
artifacts[strings.Title(name)] = &compiler.Artifact{
Abi: string(content),
Bin: string(bin),
Expand Down
6 changes: 4 additions & 2 deletions abigen/gen.go → cmd/abigen/gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package abigen

import (
"bytes"
Expand All @@ -10,13 +10,13 @@ import (
"text/template"

"github.com/umbracle/ethgo/abi"
"github.com/umbracle/ethgo/cmd/version"
"github.com/umbracle/ethgo/compiler"
)

type config struct {
Package string
Output string
Name string
}

func cleanName(str string) string {
Expand Down Expand Up @@ -144,6 +144,7 @@ func gen(artifacts map[string]*compiler.Artifact, config *config, hash string) e
}
input := map[string]interface{}{
"Hash": hash,
"Version": version.Version,
"Ptr": strings.ToLower(string(name[0])),
"Config": config,
"Contract": artifact,
Expand Down Expand Up @@ -174,6 +175,7 @@ func gen(artifacts map[string]*compiler.Artifact, config *config, hash string) e

var templateAbiStr = `// Code generated by ethgo/abigen. DO NOT EDIT.
// Hash: {{.Hash}}
// Version: {{.Version}}
package {{.Config.Package}}
import (
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
53 changes: 53 additions & 0 deletions cmd/commands/abigen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package commands

import (
flag "github.com/spf13/pflag"
"github.com/umbracle/ethgo/cmd/abigen"
)

// VersionCommand is the command to show the version of the agent
type AbigenCommand struct {
*baseCommand

source string
pckg string
output string
}

// Help implements the cli.Command interface
func (c *AbigenCommand) Help() string {
return `Usage: ethgo abigen
Compute the abigen.
` + c.Flags().FlagUsages()
}

// Synopsis implements the cli.Command interface
func (c *AbigenCommand) Synopsis() string {
return "Compute the abigen"
}

func (c *AbigenCommand) Flags() *flag.FlagSet {
flags := c.baseCommand.Flags("abigen")

flags.StringVar(&c.source, "source", "", "Source data")
flags.StringVar(&c.pckg, "package", "main", "Name of the package")
flags.StringVar(&c.output, "output", ".", "Output directory")

return flags
}

// Run implements the cli.Command interface
func (c *AbigenCommand) Run(args []string) int {
flags := c.Flags()
if err := flags.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}

if err := abigen.Parse(c.source, c.pckg, c.output); err != nil {
c.UI.Error(err.Error())
return 1
}
return 0
}
42 changes: 42 additions & 0 deletions cmd/commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package commands

import (
"os"

"github.com/mitchellh/cli"
flag "github.com/spf13/pflag"
)

func Commands() map[string]cli.CommandFactory {
ui := &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}

baseCommand := &baseCommand{
UI: ui,
}

return map[string]cli.CommandFactory{
"abigen": func() (cli.Command, error) {
return &AbigenCommand{
baseCommand: baseCommand,
}, nil
},
"version": func() (cli.Command, error) {
return &VersionCommand{
UI: ui,
}, nil
},
}
}

type baseCommand struct {
UI cli.Ui
}

func (b *baseCommand) Flags(name string) *flag.FlagSet {
flags := flag.NewFlagSet(name, 0)
return flags
}
29 changes: 29 additions & 0 deletions cmd/commands/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package commands

import (
"github.com/mitchellh/cli"
"github.com/umbracle/ethgo/cmd/version"
)

// VersionCommand is the command to show the version of the agent
type VersionCommand struct {
UI cli.Ui
}

// Help implements the cli.Command interface
func (c *VersionCommand) Help() string {
return `Usage: ethgo version
Display the Ethgo version`
}

// Synopsis implements the cli.Command interface
func (c *VersionCommand) Synopsis() string {
return "Display the Ethgo version"
}

// Run implements the cli.Command interface
func (c *VersionCommand) Run(args []string) int {
c.UI.Output(version.GetVersion())
return 0
}
34 changes: 34 additions & 0 deletions cmd/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module github.com/umbracle/ethgo/cmd

go 1.17

require (
github.com/mitchellh/cli v1.1.2
github.com/umbracle/ethgo v0.0.0-20220303093617-1621d9ff042b
)

require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/posener/complete v1.1.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/umbracle/fastrlp v0.0.0-20211229195328-c1416904ae17 // indirect
github.com/valyala/fastjson v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
)
Loading

1 comment on commit 8907487

@vercel
Copy link

@vercel vercel bot commented on 8907487 Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.