Skip to content

Commit

Permalink
New app structure with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehsan-saradar committed Jan 8, 2024
1 parent b9a8a18 commit 8cb99e3
Show file tree
Hide file tree
Showing 49 changed files with 2,001 additions and 579 deletions.
17 changes: 17 additions & 0 deletions app.ignite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '1.0.0'
apps:
explorer:
description: Easy to use terminal chain explorer for testing your Ignite blockchains
path: ./official/explorer
hello-world:
description: A simple hello world application
path: ./examples/hello-world
hooks:
description: A simple application that demonstrates the use of hooks
path: ./examples/hooks
hermes:
description: A wrapper around the hermes IBC relayer
path: ./official/hermes
marketplace:
description: Explore the world of Ignite applications and more!
path: ./official/marketplace
23 changes: 23 additions & 0 deletions examples/hello-world/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// NewHelloWorld creates a new hello-world command.
func NewHelloWorld() *cobra.Command {
c := &cobra.Command{
Use: "hello-world",
Short: "Say hello to the world of ignite!",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Hello, world!")
return nil
},
}

return c
}
162 changes: 10 additions & 152 deletions integration/go.mod → examples/hello-world/go.mod

Large diffs are not rendered by default.

408 changes: 16 additions & 392 deletions integration/go.sum → examples/hello-world/go.sum

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions examples/hello-world/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package integration_test

import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

pluginsconfig "github.com/ignite/cli/v28/ignite/config/plugins"
"github.com/ignite/cli/v28/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/v28/ignite/services/plugin"
envtest "github.com/ignite/cli/v28/integration"
)

func TestHelloWorld(t *testing.T) {
var (
require = require.New(t)
assert = assert.New(t)
env = envtest.New(t)
app = env.Scaffold("github.com/test/test")

assertPlugins = func(expectedLocalPlugins, expectedGlobalPlugins []pluginsconfig.Plugin) {
localCfg, err := pluginsconfig.ParseDir(app.SourcePath())
require.NoError(err)
assert.ElementsMatch(expectedLocalPlugins, localCfg.Apps, "unexpected local apps")

globalCfgPath, err := plugin.PluginsPath()
require.NoError(err)
globalCfg, err := pluginsconfig.ParseDir(globalCfgPath)
require.NoError(err)
assert.ElementsMatch(expectedGlobalPlugins, globalCfg.Apps, "unexpected global apps")
}
)

dir, err := os.Getwd()
require.NoError(err)
pluginPath := filepath.Join(filepath.Dir(filepath.Dir(dir)), "hello-world")

env.Must(env.Exec("add hello-world plugin",
step.NewSteps(step.New(
step.Exec(envtest.IgniteApp, "app", "install", "-g", pluginPath),
step.Workdir(app.SourcePath()),
)),
))

// one local plugin expected
assertPlugins(
nil,
[]pluginsconfig.Plugin{
{
Path: pluginPath,
},
},
)

buf := &bytes.Buffer{}
env.Must(env.Exec("run hello-world",
step.NewSteps(step.New(
step.Exec(
envtest.IgniteApp,
"hello-world",
),
step.Workdir(app.SourcePath()),
step.Stdout(buf),
)),
))
assert.Equal("Hello, world!\n", buf.String())
}
53 changes: 53 additions & 0 deletions examples/hello-world/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"os"

hplugin "github.com/hashicorp/go-plugin"

"github.com/ignite/apps/examples/hello-world/cmd"
"github.com/ignite/cli/v28/ignite/services/plugin"
)

type app struct{}

func (app) Manifest(_ context.Context) (*plugin.Manifest, error) {
m := &plugin.Manifest{
Name: "hello-world",
}
m.ImportCobraCommand(cmd.NewHelloWorld(), "ignite")
return m, nil
}

func (app) Execute(_ context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error {
// Instead of a switch on c.Use, we run the root command like if
// we were in a command line context. This implies to set os.Args
// correctly.
// Remove the first arg "ignite" from OSArgs because our hello-world
// command root is "hello-world" not "ignite".
os.Args = c.OsArgs[1:]
return cmd.NewHelloWorld().Execute()
}

func (app) ExecuteHookPre(_ context.Context, _ *plugin.ExecutedHook, _ plugin.ClientAPI) error {
return nil
}

func (app) ExecuteHookPost(_ context.Context, _ *plugin.ExecutedHook, _ plugin.ClientAPI) error {
return nil
}

func (app) ExecuteHookCleanUp(_ context.Context, _ *plugin.ExecutedHook, _ plugin.ClientAPI) error {
return nil
}

func main() {
hplugin.Serve(&hplugin.ServeConfig{
HandshakeConfig: plugin.HandshakeConfig(),
Plugins: map[string]hplugin.Plugin{
"hello-world": plugin.NewGRPC(&app{}),
},
GRPCServer: hplugin.DefaultGRPCServer,
})
}
19 changes: 19 additions & 0 deletions examples/hooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
*.app

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
5 changes: 5 additions & 0 deletions examples/hooks/app.ignite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: '1.0.0'
apps:
hooks:
description: hooks is an awesome Ignite application!
path: ./
24 changes: 24 additions & 0 deletions examples/hooks/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// NewHooks creates a new hooks command.
func NewHooks() *cobra.Command {
c := &cobra.Command{
Use: "hooks",
Short: "This is a example Ignite App that demonstrates hooks",
Long: "To use either run \"ignite scaffold chain\" or \"ignite chain serve\" and see the output.",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(cmd.Long)
return nil
},
}

return c
}
Loading

0 comments on commit 8cb99e3

Please sign in to comment.