Skip to content

Commit

Permalink
improve the log command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Nov 25, 2024
1 parent 05130c5 commit 1d41291
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 77 deletions.
20 changes: 20 additions & 0 deletions spaceship/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ ignite spaceship stop [email protected] --key $HOME/.ssh/id_rsa
To redeploy the chain on the same server without overwriting the home directory, use the `--init-chain` flag to
reinitialize the chain if necessary.

## Faucet

You can deploy your chain along with a faucet application passing the faucet flag to the deploy command

```ssh
ignite spaceship deploy [email protected] --key $HOME/.ssh/id_rsa --faucet
```

and also specify the faucet port:

```ssh
ignite spaceship deploy [email protected] --key $HOME/.ssh/id_rsa --faucet --faucet-port 8077
```

To check the faucet logs, pass the parameter `faucet` to the `--app` flag into the log command:

```sh
ignite spaceship log [email protected] --key $HOME/.ssh/id_rsa --app faucet
```

### Config

You can override the default [chain configuration](https://docs.ignite.com/references/config#validators) by using the
Expand Down
4 changes: 4 additions & 0 deletions spaceship/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/ignite/apps/spaceship/templates/script"
)

const (
flagInitChain = "init-chain"
)

// ExecuteSSHStatus executes the ssh status subcommand.
func ExecuteSSHStatus(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
session := cliui.New(cliui.StartSpinnerWithText(statusConnecting))
Expand Down
65 changes: 29 additions & 36 deletions spaceship/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package cmd

import "github.com/ignite/cli/v28/ignite/services/plugin"
import (
"fmt"
"strings"

"github.com/ignite/cli/v28/ignite/services/plugin"

"github.com/ignite/apps/spaceship/pkg/ssh"
)

var defaultFlags = []*plugin.Flag{
{
Expand Down Expand Up @@ -73,44 +80,30 @@ func GetCommands() []*plugin.Command {
{
Use: "log [host]",
Short: "get remote logs",
Commands: []*plugin.Command{
{
Use: "chain",
Short: "get chain logs if its running",
Flags: append(defaultFlags,
&plugin.Flag{
Name: flagLines,
Shorthand: "l",
Usage: "number of lines of chain logs",
Type: plugin.FlagTypeInt,
DefaultValue: "100",
},
&plugin.Flag{
Name: flagRealTime,
Usage: "show the logs in the real time",
Type: plugin.FlagTypeBool,
},
),
Flags: append(defaultFlags,
&plugin.Flag{
Name: flagLines,
Shorthand: "l",
Usage: "number of lines of chain logs",
Type: plugin.FlagTypeInt,
DefaultValue: "100",
},
{
Use: "faucet",
Short: "get faucet logs if its running",
Flags: append(defaultFlags,
&plugin.Flag{
Name: flagLines,
Shorthand: "l",
Usage: "number of lines of chain logs",
Type: plugin.FlagTypeInt,
DefaultValue: "100",
},
&plugin.Flag{
Name: flagRealTime,
Usage: "show the logs in the real time",
Type: plugin.FlagTypeBool,
},
&plugin.Flag{
Name: flagRealTime,
Usage: "show the logs in the real time",
Type: plugin.FlagTypeBool,
},
&plugin.Flag{
Name: flagAppLog,
Shorthand: "a",
Usage: fmt.Sprintf(
"the app to show the log (%s)",
strings.Join(ssh.LogTypes(), ","),
),
Type: plugin.FlagTypeString,
DefaultValue: ssh.LogChain.String(),
},
},
),
},
{
Use: "status [host]",
Expand Down
41 changes: 23 additions & 18 deletions spaceship/cmd/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/ignite/cli/v28/ignite/services/plugin"

"github.com/ignite/apps/spaceship/cmd"
"github.com/ignite/apps/spaceship/pkg/ssh"
)

func main() {
Expand Down Expand Up @@ -72,24 +74,27 @@ func main() {
return
}
case "log":
c.Flags = append(c.Flags, &plugin.Flag{
Name: "real-time",
Usage: "show the logs in the real time",
Type: plugin.FlagTypeBool,
Value: "true",
})
switch args[2] {
case "chain":
if err := cmd.ExecuteChainSSHLog(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
case "faucet":
if err := cmd.ExecuteFaucetSSHLog(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Fprintf(os.Stderr, "unknown log command: %s", args[2])
c.Flags = append(c.Flags,
&plugin.Flag{
Name: "real-time",
Usage: "show the logs in the real time",
Type: plugin.FlagTypeBool,
Value: "true",
},
&plugin.Flag{
Name: "app",
Shorthand: "a",
Usage: fmt.Sprintf(
"the app to show the log (%s)",
strings.Join(ssh.LogTypes(), ","),
),
Type: plugin.FlagTypeString,
DefaultValue: ssh.LogChain.String(),
Value: ssh.LogChain.String(),
},
)
if err := cmd.ExecuteSSHLog(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
case "status":
Expand Down
5 changes: 5 additions & 0 deletions spaceship/cmd/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/ignite/cli/v28/ignite/services/plugin"
)

const (
flagFaucet = "faucet"
flagFaucetPort = "faucet-port"
)

func faucetPort(f []*plugin.Flag) (uint64, error) {
flags := plugin.Flags(f)
port, err := flags.GetUint64(flagFaucetPort)
Expand Down
20 changes: 12 additions & 8 deletions spaceship/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ import (
"github.com/ignite/apps/spaceship/pkg/ssh"
)

func ExecuteChainSSHLog(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
return ExecuteSSHLog(ctx, ssh.LogChain, cmd, chain)
}

func ExecuteFaucetSSHLog(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
return ExecuteSSHLog(ctx, ssh.LogFaucet, cmd, chain)
}
const (
flagLines = "lines"
flagRealTime = "real-time"
flagAppLog = "app"
)

// ExecuteSSHLog executes the ssh log subcommand.
func ExecuteSSHLog(ctx context.Context, logType ssh.LogType, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
func ExecuteSSHLog(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
session := cliui.New(cliui.StartSpinnerWithText(statusConnecting))
defer session.End()

var (
flags = plugin.Flags(cmd.Flags)
lines, _ = flags.GetInt(flagLines)
realTime, _ = flags.GetBool(flagRealTime)
appLog, _ = flags.GetString(flagAppLog)
)

logType, err := ssh.ParseLogType(appLog)
if err != nil {
return err
}

c, err := executeSSH(cmd, chain)
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions spaceship/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const (
flagKey = "key"
flagRawKey = "raw-key"
flagKeyPassword = "key-password"
flagInitChain = "init-chain"
flagFaucet = "faucet"
flagFaucetPort = "faucet-port"
flagLines = "lines"
flagRealTime = "real-time"

statusConnecting = "Connecting..."
)
Expand Down
9 changes: 1 addition & 8 deletions spaceship/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, api plugin.Cl
case "stop":
return cmd.ExecuteSSHSStop(ctx, c, chainInfo)
case "log":
switch args[1] {
case "chain":
return cmd.ExecuteChainSSHLog(ctx, c, chainInfo)
case "faucet":
return cmd.ExecuteFaucetSSHLog(ctx, c, chainInfo)
default:
return fmt.Errorf("unknown log command: %s", args[1])
}
return cmd.ExecuteSSHLog(ctx, c, chainInfo)
case "faucet":
switch args[1] {
case "status":
Expand Down
24 changes: 22 additions & 2 deletions spaceship/pkg/ssh/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,30 @@ type (
const (
logExtension = ".log"

LogChain LogType = "chain_"
LogFaucet LogType = "faucet_"
LogChain LogType = "chain"
LogFaucet LogType = "faucet"
)

func (l LogType) String() string {
return string(l)
}

func LogTypes() []string {
return []string{LogChain.String(), LogFaucet.String()}
}

// ParseLogType parses the log type from a string.
func ParseLogType(logType string) (LogType, error) {
switch LogType(strings.ToLower(logType)) {
case LogChain:
return LogChain, nil
case LogFaucet:
return LogFaucet, nil
default:
return "", errors.New("invalid log type: " + logType)
}
}

func (a logs) Len() int { return len(a) }
func (a logs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a logs) Less(i, j int) bool { return a[i].time.Before(a[j].time) }
Expand Down

0 comments on commit 1d41291

Please sign in to comment.