2020-06-30 11:12:51 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package t8ntool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-05-17 16:13:22 +03:00
|
|
|
"errors"
|
2020-06-30 11:12:51 +03:00
|
|
|
"fmt"
|
2024-03-22 20:53:53 +03:00
|
|
|
"io"
|
2020-06-30 11:12:51 +03:00
|
|
|
"math/big"
|
|
|
|
"os"
|
2024-03-12 12:00:34 +03:00
|
|
|
"path/filepath"
|
2020-06-30 11:12:51 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-02-25 17:26:57 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-08-01 12:58:45 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
|
2020-06-30 11:12:51 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2024-03-27 18:12:57 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
2020-06-30 11:12:51 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2023-12-18 17:16:25 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2021-11-25 15:17:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers/logger"
|
2020-06-30 11:12:51 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/tests"
|
2022-06-27 19:22:36 +03:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-06-30 11:12:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrorEVM = 2
|
2021-11-22 11:25:35 +03:00
|
|
|
ErrorConfig = 3
|
2020-06-30 11:12:51 +03:00
|
|
|
ErrorMissingBlockhash = 4
|
|
|
|
|
|
|
|
ErrorJson = 10
|
|
|
|
ErrorIO = 11
|
2021-11-22 11:25:35 +03:00
|
|
|
ErrorRlp = 12
|
2020-06-30 11:12:51 +03:00
|
|
|
|
|
|
|
stdinSelector = "stdin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NumberedError struct {
|
|
|
|
errorCode int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewError(errorCode int, err error) *NumberedError {
|
|
|
|
return &NumberedError{errorCode, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NumberedError) Error() string {
|
|
|
|
return fmt.Sprintf("ERROR(%d): %v", n.errorCode, n.err.Error())
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:22:43 +03:00
|
|
|
func (n *NumberedError) ExitCode() int {
|
2020-06-30 11:12:51 +03:00
|
|
|
return n.errorCode
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:22:43 +03:00
|
|
|
// compile-time conformance test
|
|
|
|
var (
|
|
|
|
_ cli.ExitCoder = (*NumberedError)(nil)
|
|
|
|
)
|
|
|
|
|
2020-06-30 11:12:51 +03:00
|
|
|
type input struct {
|
2024-02-16 21:05:33 +03:00
|
|
|
Alloc types.GenesisAlloc `json:"alloc,omitempty"`
|
|
|
|
Env *stEnv `json:"env,omitempty"`
|
|
|
|
Txs []*txWithKey `json:"txs,omitempty"`
|
|
|
|
TxRlp string `json:"txsRlp,omitempty"`
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 14:57:40 +03:00
|
|
|
func Transition(ctx *cli.Context) error {
|
2024-03-22 20:53:53 +03:00
|
|
|
var getTracer = func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error) { return nil, nil, nil }
|
2020-06-30 11:12:51 +03:00
|
|
|
|
2021-11-22 11:25:35 +03:00
|
|
|
baseDir, err := createBasedir(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return NewError(ErrorIO, fmt.Errorf("failed creating output basedir: %v", err))
|
2020-08-19 12:31:13 +03:00
|
|
|
}
|
2023-12-18 17:16:25 +03:00
|
|
|
|
|
|
|
if ctx.Bool(TraceFlag.Name) { // JSON opcode tracing
|
2020-06-30 11:12:51 +03:00
|
|
|
// Configure the EVM logger
|
2021-11-25 15:17:09 +03:00
|
|
|
logConfig := &logger.Config{
|
2021-09-13 19:59:52 +03:00
|
|
|
DisableStack: ctx.Bool(TraceDisableStackFlag.Name),
|
2023-12-18 17:16:25 +03:00
|
|
|
EnableMemory: ctx.Bool(TraceEnableMemoryFlag.Name),
|
|
|
|
EnableReturnData: ctx.Bool(TraceEnableReturnDataFlag.Name),
|
2021-09-13 19:59:52 +03:00
|
|
|
Debug: true,
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
2024-03-22 20:53:53 +03:00
|
|
|
getTracer = func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error) {
|
2024-03-12 12:00:34 +03:00
|
|
|
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.jsonl", txIndex, txHash.String())))
|
2020-06-30 11:12:51 +03:00
|
|
|
if err != nil {
|
2024-03-22 20:53:53 +03:00
|
|
|
return nil, nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
2024-03-27 18:12:57 +03:00
|
|
|
var l *tracing.Hooks
|
|
|
|
if ctx.Bool(TraceEnableCallFramesFlag.Name) {
|
|
|
|
l = logger.NewJSONLoggerWithCallFrames(logConfig, traceFile)
|
|
|
|
} else {
|
|
|
|
l = logger.NewJSONLogger(logConfig, traceFile)
|
|
|
|
}
|
2024-03-22 20:53:53 +03:00
|
|
|
tracer := &tracers.Tracer{
|
2024-03-27 18:12:57 +03:00
|
|
|
Hooks: l,
|
2024-03-22 20:53:53 +03:00
|
|
|
// jsonLogger streams out result to file.
|
|
|
|
GetResult: func() (json.RawMessage, error) { return nil, nil },
|
|
|
|
Stop: func(err error) {},
|
|
|
|
}
|
|
|
|
return tracer, traceFile, nil
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
2023-12-18 17:16:25 +03:00
|
|
|
} else if ctx.IsSet(TraceTracerFlag.Name) {
|
|
|
|
var config json.RawMessage
|
|
|
|
if ctx.IsSet(TraceTracerConfigFlag.Name) {
|
|
|
|
config = []byte(ctx.String(TraceTracerConfigFlag.Name))
|
|
|
|
}
|
2024-03-22 20:53:53 +03:00
|
|
|
getTracer = func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error) {
|
2024-03-12 12:00:34 +03:00
|
|
|
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.json", txIndex, txHash.String())))
|
2023-12-18 17:16:25 +03:00
|
|
|
if err != nil {
|
2024-03-22 20:53:53 +03:00
|
|
|
return nil, nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
|
2023-12-18 17:16:25 +03:00
|
|
|
}
|
|
|
|
tracer, err := tracers.DefaultDirectory.New(ctx.String(TraceTracerFlag.Name), nil, config)
|
|
|
|
if err != nil {
|
2024-03-22 20:53:53 +03:00
|
|
|
return nil, nil, NewError(ErrorConfig, fmt.Errorf("failed instantiating tracer: %w", err))
|
2023-12-18 17:16:25 +03:00
|
|
|
}
|
2024-03-22 20:53:53 +03:00
|
|
|
return tracer, traceFile, nil
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We need to load three things: alloc, env and transactions. May be either in
|
|
|
|
// stdin input or in files.
|
|
|
|
// Check if anything needs to be read from stdin
|
|
|
|
var (
|
|
|
|
prestate Prestate
|
2023-10-25 15:08:52 +03:00
|
|
|
txIt txIterator // txs to apply
|
2020-06-30 11:12:51 +03:00
|
|
|
allocStr = ctx.String(InputAllocFlag.Name)
|
|
|
|
|
|
|
|
envStr = ctx.String(InputEnvFlag.Name)
|
|
|
|
txStr = ctx.String(InputTxsFlag.Name)
|
|
|
|
inputData = &input{}
|
|
|
|
)
|
2021-02-25 17:26:57 +03:00
|
|
|
// Figure out the prestate alloc
|
2020-06-30 11:12:51 +03:00
|
|
|
if allocStr == stdinSelector || envStr == stdinSelector || txStr == stdinSelector {
|
|
|
|
decoder := json.NewDecoder(os.Stdin)
|
2021-05-17 09:52:32 +03:00
|
|
|
if err := decoder.Decode(inputData); err != nil {
|
2024-03-08 00:56:19 +03:00
|
|
|
return NewError(ErrorJson, fmt.Errorf("failed unmarshalling stdin: %v", err))
|
2021-05-17 09:52:32 +03:00
|
|
|
}
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
if allocStr != stdinSelector {
|
2021-11-22 11:25:35 +03:00
|
|
|
if err := readFile(allocStr, "alloc", &inputData.Alloc); err != nil {
|
|
|
|
return err
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 17:26:57 +03:00
|
|
|
prestate.Pre = inputData.Alloc
|
2020-06-30 11:12:51 +03:00
|
|
|
|
2021-02-25 17:26:57 +03:00
|
|
|
// Set the block environment
|
2020-06-30 11:12:51 +03:00
|
|
|
if envStr != stdinSelector {
|
|
|
|
var env stEnv
|
2021-11-22 11:25:35 +03:00
|
|
|
if err := readFile(envStr, "env", &env); err != nil {
|
|
|
|
return err
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
inputData.Env = &env
|
|
|
|
}
|
|
|
|
prestate.Env = *inputData.Env
|
|
|
|
|
2023-12-18 17:16:25 +03:00
|
|
|
vmConfig := vm.Config{}
|
2020-06-30 11:12:51 +03:00
|
|
|
// Construct the chainconfig
|
|
|
|
var chainConfig *params.ChainConfig
|
|
|
|
if cConf, extraEips, err := tests.GetChainConfig(ctx.String(ForknameFlag.Name)); err != nil {
|
2021-11-22 11:25:35 +03:00
|
|
|
return NewError(ErrorConfig, fmt.Errorf("failed constructing chain configuration: %v", err))
|
2020-06-30 11:12:51 +03:00
|
|
|
} else {
|
|
|
|
chainConfig = cConf
|
|
|
|
vmConfig.ExtraEips = extraEips
|
|
|
|
}
|
|
|
|
// Set the chain id
|
|
|
|
chainConfig.ChainID = big.NewInt(ctx.Int64(ChainIDFlag.Name))
|
|
|
|
|
2023-10-25 15:08:52 +03:00
|
|
|
if txIt, err = loadTransactions(txStr, inputData, prestate.Env, chainConfig); err != nil {
|
2023-08-22 15:33:39 +03:00
|
|
|
return err
|
2021-02-25 17:26:57 +03:00
|
|
|
}
|
2023-08-22 15:33:39 +03:00
|
|
|
if err := applyLondonChecks(&prestate.Env, chainConfig); err != nil {
|
|
|
|
return err
|
2021-02-25 17:26:57 +03:00
|
|
|
}
|
2023-08-22 15:33:39 +03:00
|
|
|
if err := applyShanghaiChecks(&prestate.Env, chainConfig); err != nil {
|
|
|
|
return err
|
2021-05-17 16:13:22 +03:00
|
|
|
}
|
2023-08-22 15:33:39 +03:00
|
|
|
if err := applyMergeChecks(&prestate.Env, chainConfig); err != nil {
|
|
|
|
return err
|
2023-01-25 17:32:25 +03:00
|
|
|
}
|
2023-08-22 15:33:39 +03:00
|
|
|
if err := applyCancunChecks(&prestate.Env, chainConfig); err != nil {
|
|
|
|
return err
|
2021-08-25 16:04:26 +03:00
|
|
|
}
|
2020-06-30 11:12:51 +03:00
|
|
|
// Run the test and aggregate the result
|
2023-10-25 15:08:52 +03:00
|
|
|
s, result, body, err := prestate.Apply(vmConfig, chainConfig, txIt, ctx.Int64(RewardFlag.Name), getTracer)
|
2020-06-30 11:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-12 11:40:00 +03:00
|
|
|
// Dump the execution result
|
2020-06-30 11:12:51 +03:00
|
|
|
collector := make(Alloc)
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 11:05:39 +03:00
|
|
|
s.DumpToCollector(collector, nil)
|
2021-02-25 17:26:57 +03:00
|
|
|
return dispatchOutput(ctx, baseDir, result, collector, body)
|
|
|
|
}
|
2020-06-30 11:12:51 +03:00
|
|
|
|
2023-08-22 15:33:39 +03:00
|
|
|
func applyLondonChecks(env *stEnv, chainConfig *params.ChainConfig) error {
|
|
|
|
if !chainConfig.IsLondon(big.NewInt(int64(env.Number))) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Sanity check, to not `panic` in state_transition
|
|
|
|
if env.BaseFee != nil {
|
|
|
|
// Already set, base fee has precedent over parent base fee.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if env.ParentBaseFee == nil || env.Number == 0 {
|
|
|
|
return NewError(ErrorConfig, errors.New("EIP-1559 config but missing 'currentBaseFee' in env section"))
|
|
|
|
}
|
|
|
|
env.BaseFee = eip1559.CalcBaseFee(chainConfig, &types.Header{
|
|
|
|
Number: new(big.Int).SetUint64(env.Number - 1),
|
|
|
|
BaseFee: env.ParentBaseFee,
|
|
|
|
GasUsed: env.ParentGasUsed,
|
|
|
|
GasLimit: env.ParentGasLimit,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyShanghaiChecks(env *stEnv, chainConfig *params.ChainConfig) error {
|
|
|
|
if !chainConfig.IsShanghai(big.NewInt(int64(env.Number)), env.Timestamp) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if env.Withdrawals == nil {
|
|
|
|
return NewError(ErrorConfig, errors.New("Shanghai config but missing 'withdrawals' in env section"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyMergeChecks(env *stEnv, chainConfig *params.ChainConfig) error {
|
|
|
|
isMerged := chainConfig.TerminalTotalDifficulty != nil && chainConfig.TerminalTotalDifficulty.BitLen() == 0
|
|
|
|
if !isMerged {
|
|
|
|
// pre-merge: If difficulty was not provided by caller, we need to calculate it.
|
|
|
|
if env.Difficulty != nil {
|
|
|
|
// already set
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case env.ParentDifficulty == nil:
|
|
|
|
return NewError(ErrorConfig, errors.New("currentDifficulty was not provided, and cannot be calculated due to missing parentDifficulty"))
|
|
|
|
case env.Number == 0:
|
|
|
|
return NewError(ErrorConfig, errors.New("currentDifficulty needs to be provided for block number 0"))
|
|
|
|
case env.Timestamp <= env.ParentTimestamp:
|
|
|
|
return NewError(ErrorConfig, fmt.Errorf("currentDifficulty cannot be calculated -- currentTime (%d) needs to be after parent time (%d)",
|
|
|
|
env.Timestamp, env.ParentTimestamp))
|
|
|
|
}
|
|
|
|
env.Difficulty = calcDifficulty(chainConfig, env.Number, env.Timestamp,
|
|
|
|
env.ParentTimestamp, env.ParentDifficulty, env.ParentUncleHash)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// post-merge:
|
|
|
|
// - random must be supplied
|
|
|
|
// - difficulty must be zero
|
|
|
|
switch {
|
|
|
|
case env.Random == nil:
|
|
|
|
return NewError(ErrorConfig, errors.New("post-merge requires currentRandom to be defined in env"))
|
|
|
|
case env.Difficulty != nil && env.Difficulty.BitLen() != 0:
|
|
|
|
return NewError(ErrorConfig, errors.New("post-merge difficulty must be zero (or omitted) in env"))
|
|
|
|
}
|
|
|
|
env.Difficulty = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyCancunChecks(env *stEnv, chainConfig *params.ChainConfig) error {
|
|
|
|
if !chainConfig.IsCancun(big.NewInt(int64(env.Number)), env.Timestamp) {
|
|
|
|
env.ParentBeaconBlockRoot = nil // un-set it if it has been set too early
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Post-cancun
|
|
|
|
// We require EIP-4788 beacon root to be set in the env
|
|
|
|
if env.ParentBeaconBlockRoot == nil {
|
|
|
|
return NewError(ErrorConfig, errors.New("post-cancun env requires parentBeaconBlockRoot to be set"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-16 21:05:33 +03:00
|
|
|
type Alloc map[common.Address]types.Account
|
2020-06-30 11:12:51 +03:00
|
|
|
|
|
|
|
func (g Alloc) OnRoot(common.Hash) {}
|
|
|
|
|
2023-05-23 13:10:26 +03:00
|
|
|
func (g Alloc) OnAccount(addr *common.Address, dumpAccount state.DumpAccount) {
|
|
|
|
if addr == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-23 16:51:58 +03:00
|
|
|
balance, _ := new(big.Int).SetString(dumpAccount.Balance, 0)
|
2020-06-30 11:12:51 +03:00
|
|
|
var storage map[common.Hash]common.Hash
|
|
|
|
if dumpAccount.Storage != nil {
|
2024-04-30 12:19:59 +03:00
|
|
|
storage = make(map[common.Hash]common.Hash, len(dumpAccount.Storage))
|
2020-06-30 11:12:51 +03:00
|
|
|
for k, v := range dumpAccount.Storage {
|
|
|
|
storage[k] = common.HexToHash(v)
|
|
|
|
}
|
|
|
|
}
|
2024-02-16 21:05:33 +03:00
|
|
|
genesisAccount := types.Account{
|
cmd/geth, eth, core: snapshot dump + unify with trie dump (#22795)
* cmd/geth, eth, core: snapshot dump + unify with trie dump
* cmd/evm: dump API fixes
* cmd/geth, core, eth: fix some remaining errors
* cmd/evm: dump - add limit, support address startkey, address review concerns
* cmd, core/state, eth: minor polishes, fix snap dump crash, unify format
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-05-12 11:05:39 +03:00
|
|
|
Code: dumpAccount.Code,
|
2020-06-30 11:12:51 +03:00
|
|
|
Storage: storage,
|
|
|
|
Balance: balance,
|
|
|
|
Nonce: dumpAccount.Nonce,
|
|
|
|
}
|
2023-05-23 13:10:26 +03:00
|
|
|
g[*addr] = genesisAccount
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 10:37:00 +03:00
|
|
|
// saveFile marshals the object to the given file
|
2020-08-19 12:31:13 +03:00
|
|
|
func saveFile(baseDir, filename string, data interface{}) error {
|
2020-06-30 11:12:51 +03:00
|
|
|
b, err := json.MarshalIndent(data, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
|
|
|
|
}
|
2024-03-12 12:00:34 +03:00
|
|
|
location := filepath.Join(baseDir, filename)
|
2022-05-16 12:59:35 +03:00
|
|
|
if err = os.WriteFile(location, b, 0644); err != nil {
|
2020-06-30 11:12:51 +03:00
|
|
|
return NewError(ErrorIO, fmt.Errorf("failed writing output: %v", err))
|
|
|
|
}
|
2021-02-25 17:26:57 +03:00
|
|
|
log.Info("Wrote file", "file", location)
|
2020-06-30 11:12:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatchOutput writes the output data to either stderr or stdout, or to the specified
|
|
|
|
// files
|
2021-02-25 17:26:57 +03:00
|
|
|
func dispatchOutput(ctx *cli.Context, baseDir string, result *ExecutionResult, alloc Alloc, body hexutil.Bytes) error {
|
2020-06-30 11:12:51 +03:00
|
|
|
stdOutObject := make(map[string]interface{})
|
|
|
|
stdErrObject := make(map[string]interface{})
|
2020-08-19 12:31:13 +03:00
|
|
|
dispatch := func(baseDir, fName, name string, obj interface{}) error {
|
2020-06-30 11:12:51 +03:00
|
|
|
switch fName {
|
|
|
|
case "stdout":
|
|
|
|
stdOutObject[name] = obj
|
|
|
|
case "stderr":
|
|
|
|
stdErrObject[name] = obj
|
2021-02-25 17:26:57 +03:00
|
|
|
case "":
|
|
|
|
// don't save
|
2020-06-30 11:12:51 +03:00
|
|
|
default: // save to file
|
2020-08-19 12:31:13 +03:00
|
|
|
if err := saveFile(baseDir, fName, obj); err != nil {
|
2020-06-30 11:12:51 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-19 12:31:13 +03:00
|
|
|
if err := dispatch(baseDir, ctx.String(OutputAllocFlag.Name), "alloc", alloc); err != nil {
|
2020-06-30 11:12:51 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-08-19 12:31:13 +03:00
|
|
|
if err := dispatch(baseDir, ctx.String(OutputResultFlag.Name), "result", result); err != nil {
|
2020-06-30 11:12:51 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-02-25 17:26:57 +03:00
|
|
|
if err := dispatch(baseDir, ctx.String(OutputBodyFlag.Name), "body", body); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-30 11:12:51 +03:00
|
|
|
if len(stdOutObject) > 0 {
|
2021-09-02 10:22:43 +03:00
|
|
|
b, err := json.MarshalIndent(stdOutObject, "", " ")
|
2020-06-30 11:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
|
|
|
|
}
|
|
|
|
os.Stdout.Write(b)
|
2021-10-13 18:31:02 +03:00
|
|
|
os.Stdout.WriteString("\n")
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
if len(stdErrObject) > 0 {
|
2021-09-02 10:22:43 +03:00
|
|
|
b, err := json.MarshalIndent(stdErrObject, "", " ")
|
2020-06-30 11:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
|
|
|
|
}
|
|
|
|
os.Stderr.Write(b)
|
2021-10-13 18:31:02 +03:00
|
|
|
os.Stderr.WriteString("\n")
|
2020-06-30 11:12:51 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|