Skip to content

Commit c3ec490

Browse files
authored
feat(cmd): add --output-file flag to chain serve (#4770)
1 parent 3edae21 commit c3ec490

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
## Unreleased
44

5+
## [`v29.2.0`](https://github.com/ignite/cli/releases/tag/v29.2.0)
6+
57
### Features
68

79
- [#4676](https://github.com/ignite/cli/issues/4676) Add Decimal Coin Type.
810
- [#4765](https://github.com/ignite/cli/pull/4765) Create `scaffold type-list` command.
11+
- [#4770](https://github.com/ignite/cli/pull/4770) Add `--output-file` flag to `chain serve` command to improve running `chain serve` in the background.
912

1013
### Changes
1114

1215
- [#4759](https://github.com/ignite/cli/pull/4759) Remove undocumented RPC address override in services chainer.
1316
- [#4760](https://github.com/ignite/cli/pull/4760) Bump Cosmos SDK to `v0.53.3`.
1417

15-
1618
### Fixes
1719

1820
- [#4757](https://github.com/ignite/cli/pull/4757) Always delete temp folder from open api generation.

ignite/cmd/chain_serve.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package ignitecmd
22

33
import (
44
"context"
5+
"os"
6+
"os/signal"
7+
"syscall"
58

69
tea "github.com/charmbracelet/bubbletea"
710
"github.com/spf13/cobra"
@@ -22,6 +25,7 @@ const (
2225
flagGenerateClients = "generate-clients"
2326
flagQuitOnFail = "quit-on-fail"
2427
flagResetOnce = "reset-once"
28+
flagOutputFile = "output-file"
2529
)
2630

2731
// NewChainServe creates a new serve command to serve a blockchain.
@@ -78,11 +82,16 @@ production, you may want to run "appd start" manually.
7882
c.Flags().Bool(flagGenerateClients, false, "generate code for the configured clients on reset or source code change")
7983
c.Flags().Bool(flagQuitOnFail, false, "quit program if the app fails to start")
8084
c.Flags().StringSlice(flagBuildTags, []string{}, "parameters to build the chain binary")
85+
c.Flags().StringP(flagOutputFile, "o", "", "output file logging the chain output (no UI, no stdin, listens for SIGTERM, implies --yes) (default: stdout)")
8186

8287
return c
8388
}
8489

8590
func chainServeHandler(cmd *cobra.Command, _ []string) error {
91+
if cmd.Flags().Changed(flagOutputFile) {
92+
return daemonMode(cmd)
93+
}
94+
8695
options := []cliui.Option{cliui.WithoutUserInteraction(getYes(cmd))}
8796

8897
// Session must not handle events when the verbosity is the default
@@ -115,6 +124,38 @@ func chainServeHandler(cmd *cobra.Command, _ []string) error {
115124
return chainServe(cmd, session)
116125
}
117126

127+
// daemonMode runs the chain serve command without user interaction, UI in verbose mode. Useful to be used as daemon.
128+
func daemonMode(cmd *cobra.Command) error {
129+
// always yes, no user interaction
130+
options := []cliui.Option{cliui.WithoutUserInteraction(true)}
131+
options = append(options,
132+
cliui.WithVerbosity(uilog.VerbosityVerbose),
133+
)
134+
135+
// output file logic
136+
outputFile, _ := cmd.Flags().GetString(flagOutputFile)
137+
var output *os.File
138+
var err error
139+
if outputFile != "" {
140+
output, err = os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
141+
if err != nil {
142+
return err
143+
}
144+
defer output.Close()
145+
options = append(options, cliui.WithStdout(output), cliui.WithStderr(output))
146+
} else {
147+
options = append(options, cliui.WithStdout(os.Stdout), cliui.WithStderr(os.Stderr))
148+
}
149+
150+
session := cliui.New(options...)
151+
defer session.End()
152+
153+
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
154+
defer stop()
155+
cmd.SetContext(ctx)
156+
return chainServe(cmd, session)
157+
}
158+
118159
func chainServeCmd(cmd *cobra.Command, session *cliui.Session) tea.Cmd {
119160
return func() tea.Msg {
120161
if err := chainServe(cmd, session); err != nil && !errors.Is(err, context.Canceled) {

0 commit comments

Comments
 (0)