From 1d412918743bd99693f60f190e03711d6661d2c0 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Mon, 25 Nov 2024 00:16:51 -0300 Subject: [PATCH] improve the log command --- spaceship/README.md | 20 ++++++++++++ spaceship/cmd/chain.go | 4 +++ spaceship/cmd/cmd.go | 65 +++++++++++++++++-------------------- spaceship/cmd/debug/main.go | 41 +++++++++++++---------- spaceship/cmd/faucet.go | 5 +++ spaceship/cmd/log.go | 20 +++++++----- spaceship/cmd/ssh.go | 5 --- spaceship/main.go | 9 +---- spaceship/pkg/ssh/log.go | 24 ++++++++++++-- 9 files changed, 116 insertions(+), 77 deletions(-) diff --git a/spaceship/README.md b/spaceship/README.md index d1165e12..5417b8e8 100644 --- a/spaceship/README.md +++ b/spaceship/README.md @@ -66,6 +66,26 @@ ignite spaceship stop root@127.0.0.1 --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 root@127.0.0.1 --key $HOME/.ssh/id_rsa --faucet +``` + +and also specify the faucet port: + +```ssh +ignite spaceship deploy root@127.0.0.1 --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 root@127.0.0.1 --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 diff --git a/spaceship/cmd/chain.go b/spaceship/cmd/chain.go index 7bf34d8a..491ba7ba 100644 --- a/spaceship/cmd/chain.go +++ b/spaceship/cmd/chain.go @@ -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)) diff --git a/spaceship/cmd/cmd.go b/spaceship/cmd/cmd.go index 0189f90c..23cbaa8c 100644 --- a/spaceship/cmd/cmd.go +++ b/spaceship/cmd/cmd.go @@ -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{ { @@ -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]", diff --git a/spaceship/cmd/debug/main.go b/spaceship/cmd/debug/main.go index 4fd7c4f6..c85b0cd7 100644 --- a/spaceship/cmd/debug/main.go +++ b/spaceship/cmd/debug/main.go @@ -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() { @@ -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": diff --git a/spaceship/cmd/faucet.go b/spaceship/cmd/faucet.go index a63a4b13..efe73e9c 100644 --- a/spaceship/cmd/faucet.go +++ b/spaceship/cmd/faucet.go @@ -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) diff --git a/spaceship/cmd/log.go b/spaceship/cmd/log.go index 6a5f33bd..bbcef1bc 100644 --- a/spaceship/cmd/log.go +++ b/spaceship/cmd/log.go @@ -11,16 +11,14 @@ 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() @@ -28,8 +26,14 @@ func ExecuteSSHLog(ctx context.Context, logType ssh.LogType, cmd *plugin.Execute 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 diff --git a/spaceship/cmd/ssh.go b/spaceship/cmd/ssh.go index 11b7ca6c..2f49de21 100644 --- a/spaceship/cmd/ssh.go +++ b/spaceship/cmd/ssh.go @@ -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..." ) diff --git a/spaceship/main.go b/spaceship/main.go index 7b4d8edd..88db5020 100644 --- a/spaceship/main.go +++ b/spaceship/main.go @@ -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": diff --git a/spaceship/pkg/ssh/log.go b/spaceship/pkg/ssh/log.go index 1be0f945..8f32e7bc 100644 --- a/spaceship/pkg/ssh/log.go +++ b/spaceship/pkg/ssh/log.go @@ -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) }