Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cmd/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"fmt"
"os"

"github.com/spf13/cobra"

Expand All @@ -11,6 +12,16 @@ import (
"github.com/oasisprotocol/cli/config"
)

// Warnf prints a message to stderr with formatting.
func Warnf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
}

// Warn prints a message to stderr.
func Warn(msg string) {
fmt.Fprintln(os.Stderr, msg)
}

// CheckForceErr treats error as warning, if --force is provided.
func CheckForceErr(err interface{}) {
// No error.
Expand All @@ -20,7 +31,7 @@ func CheckForceErr(err interface{}) {

// --force is provided.
if IsForce() {
fmt.Printf("Warning: %s\nProceeding by force as requested\n", err)
Warnf("Warning: %s\nProceeding by force as requested", err)
return
}

Expand Down
20 changes: 17 additions & 3 deletions cmd/common/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@ package common

import (
"fmt"
"os"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
)

// SurveyStdio is the standard survey option to direct prompts to stderr.
var SurveyStdio = survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)

// Ask wraps survey.AskOne while forcing prompts to stderr.
func Ask(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
return survey.AskOne(p, response, append([]survey.AskOpt{SurveyStdio}, opts...)...)
}

// AskMulti wraps survey.Ask while forcing prompts to stderr.
func AskMulti(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
return survey.Ask(qs, response, append([]survey.AskOpt{SurveyStdio}, opts...)...)
}

var (
// PromptPassphrase is the standard passphrase prompt.
PromptPassphrase = &survey.Password{
Expand All @@ -27,12 +41,12 @@ var (
// Confirm asks the user for confirmation and aborts when rejected.
func Confirm(msg, abortMsg string) {
if answerYes {
fmt.Printf("? %s Yes\n", msg)
fmt.Fprintf(os.Stderr, "? %s Yes\n", msg)
return
}

var proceed bool
err := survey.AskOne(&survey.Confirm{Message: msg}, &proceed)
err := Ask(&survey.Confirm{Message: msg}, &proceed)
cobra.CheckErr(err)
if !proceed {
cobra.CheckErr(abortMsg)
Expand Down Expand Up @@ -61,7 +75,7 @@ func AskNewPassphrase() string {
},
},
}
err := survey.Ask(questions, &answers)
err := AskMulti(questions, &answers)
cobra.CheckErr(err)

return answers.Passphrase
Expand Down
7 changes: 4 additions & 3 deletions cmd/common/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package common

import (
"fmt"
"os"
"strings"

"github.com/AlecAivazis/survey/v2"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -61,9 +61,10 @@ func LoadAccount(cfg *config.Config, name string) wallet.Account {
var passphrase string
if af.RequiresPassphrase() && !answerYes {
// Ask for passphrase to decrypt the account.
fmt.Printf("Unlock your account.\n")
// Use stderr for prompts so they work when stdout is piped.
fmt.Fprintln(os.Stderr, "Unlock your account.")

err = survey.AskOne(PromptPassphrase, &passphrase)
err = Ask(PromptPassphrase, &passphrase)
cobra.CheckErr(err)
}

Expand Down
8 changes: 2 additions & 6 deletions cmd/network/governance/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,11 @@ var (
// Try to figure out the human readable names for all the entities.
fromRegistry, err := metadata.EntitiesFromRegistry(ctx)
if err != nil {
fmt.Println()
fmt.Printf("Warning: failed to query metadata registry: %v", err)
fmt.Println()
common.Warnf("\nWarning: failed to query metadata registry: %v\n", err)
}
fromOasisscan, err := metadata.EntitiesFromOasisscan(ctx)
if err != nil {
fmt.Println()
fmt.Printf("Warning: failed to query oasisscan: %v", err)
fmt.Println()
common.Warnf("\nWarning: failed to query oasisscan: %v\n", err)
}

getName := func(addr staking.Address) string {
Expand Down
3 changes: 2 additions & 1 deletion cmd/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"

"github.com/oasisprotocol/cli/cmd/common"
"github.com/oasisprotocol/cli/cmd/network/governance"
)

Expand Down Expand Up @@ -41,7 +42,7 @@ func networkDetailsFromSurvey(net *config.Network) {
Symbol string
Decimals uint8
}{}
err := survey.Ask(questions, &answers)
err := common.AskMulti(questions, &answers)
cobra.CheckErr(err)

net.Description = answers.Description
Expand Down
2 changes: 1 addition & 1 deletion cmd/network/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var rmCmd = &cobra.Command{
}

if !common.GetAnswerYes() && len(net.ParaTimes.All) > 0 {
fmt.Printf("WARNING: Network '%s' contains %d ParaTimes.\n", name, len(net.ParaTimes.All))
common.Warnf("WARNING: Network '%s' contains %d ParaTimes.", name, len(net.ParaTimes.All))
common.Confirm("Are you sure you want to remove the network?", "not removing network")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/network/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ WARNING:
fmt.Println("Trust height: ", trust.Height)
fmt.Println("Trust hash: ", trust.Hash)
fmt.Println()
fmt.Println("WARNING: Cannot be trusted unless the CLI is connected to the RPC endpoint you control.")
common.Warn("WARNING: Cannot be trusted unless the CLI is connected to the RPC endpoint you control.")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/paratime/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var (
},
}

err := survey.Ask(questions, &paratimeInfo)
err := common.AskMulti(questions, &paratimeInfo)
cobra.CheckErr(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/paratime/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ var statsCmd = &cobra.Command{
if err != nil {
// Non-fatal, this is informative and gathering stats is time
// consuming.
fmt.Printf("\nWarning: failed to query metadata registry: %v\n", err)
common.Warnf("\nWarning: failed to query metadata registry: %v", err)
}

stats.prepareEntitiesOutput(entityMetadataLookup)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rofl/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func setupBuildEnv(deployment *buildRofl.Deployment, npa *common.NPASelection) {
trustRoot, err := fetchTrustRoot(npa, deployment.TrustRoot)
if deployment.Debug && err != nil {
// Trust root is not mandatory for debug builds.
fmt.Printf("WARNING: no trust root will be provided during compilation: %v\n", err)
common.Warnf("WARNING: no trust root will be provided during compilation: %v", err)
return
}
cobra.CheckErr(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/rofl/build/sgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ func sgxSetupBuildEnv(deployment *buildRofl.Deployment, npa *common.NPASelection
return nil // No features.
case buildModeUnsafe:
// Unsafe debug builds.
fmt.Println("WARNING: Building in UNSAFE DEBUG mode with MOCK SGX.")
fmt.Println("WARNING: This build will NOT BE DEPLOYABLE outside local test environments.")
common.Warn("WARNING: Building in UNSAFE DEBUG mode with MOCK SGX.")
common.Warn("WARNING: This build will NOT BE DEPLOYABLE outside local test environments.")

os.Setenv("OASIS_UNSAFE_SKIP_AVR_VERIFY", "1")
os.Setenv("OASIS_UNSAFE_ALLOW_DEBUG_ENCLAVES", "1")
Expand Down
6 changes: 3 additions & 3 deletions cmd/rofl/build/tdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func tdxBuildRaw(
dep := pkgMeta.FindDependency("oasis-runtime-sdk")
switch dep {
case nil:
fmt.Println("WARNING: No oasis-runtime-sdk dependency found. Skipping validation of TDX binary.")
common.Warn("WARNING: No oasis-runtime-sdk dependency found. Skipping validation of TDX binary.")
default:
// Check for presence of TDX feature.
if !dep.HasFeature("tdx") {
Expand Down Expand Up @@ -336,8 +336,8 @@ func tdxSetupBuildEnv(deployment *buildRofl.Deployment, npa *common.NPASelection
}
case buildModeUnsafe:
// Unsafe debug builds.
fmt.Println("WARNING: Building in UNSAFE DEBUG mode with MOCK TDX.")
fmt.Println("WARNING: This build will NOT BE DEPLOYABLE outside local test environments.")
common.Warn("WARNING: Building in UNSAFE DEBUG mode with MOCK TDX.")
common.Warn("WARNING: This build will NOT BE DEPLOYABLE outside local test environments.")

os.Setenv("OASIS_UNSAFE_SKIP_AVR_VERIFY", "1")
os.Setenv("OASIS_UNSAFE_ALLOW_DEBUG_ENCLAVES", "1")
Expand Down
4 changes: 2 additions & 2 deletions cmd/rofl/common/policy.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package common

import "fmt"
import "github.com/oasisprotocol/cli/cmd/common"

// RentRefundWarning is a standardized message shown before renting, topping up, or canceling a machine.
const RentRefundWarning = "WARNING: Machine rental is non-refundable. You will not get a refund for the already paid term if you cancel."

// PrintRentRefundWarning prints the standardized, user-facing refund policy warning.
func PrintRentRefundWarning() {
fmt.Println(RentRefundWarning)
common.Warn(RentRefundWarning)
}
2 changes: 1 addition & 1 deletion cmd/rofl/machine/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var (

fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
fmt.Printf("Canceling machine: %s [%s]\n", machineName, machine.ID)
fmt.Printf("WARNING: Canceling a machine will permanently destroy it including any persistent storage!\n")
common.Warn("WARNING: Canceling a machine will permanently destroy it including any persistent storage!")
roflCommon.PrintRentRefundWarning()

// Prepare transaction.
Expand Down
4 changes: 2 additions & 2 deletions cmd/rofl/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ var (
cobra.CheckErr(err)
}

fmt.Printf("WARNING: Removing this ROFL app will DEREGISTER it, ERASE any on-chain secrets and local configuration!\n")
fmt.Printf("WARNING: THIS ACTION IS IRREVERSIBLE!\n")
common.Warn("WARNING: Removing this ROFL app will DEREGISTER it, ERASE any on-chain secrets and local configuration!")
common.Warn("WARNING: THIS ACTION IS IRREVERSIBLE!")
if !common.GetAnswerYes() {
common.Confirm(fmt.Sprintf("Remove ROFL app '%s' deployed on network '%s'", appID, npa.NetworkName), "not removing")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rofl/provider/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ var (
for _, offer := range existingOffers {
offerID, ok := offer.Metadata[provider.SchedulerMetadataOfferKey]
if !ok {
fmt.Printf("WARNING: On-chain offer '%s' is missing '%s' metadata, ignoring.\n", offer.ID, provider.SchedulerMetadataOfferKey)
common.Warnf("WARNING: On-chain offer '%s' is missing '%s' metadata, ignoring.", offer.ID, provider.SchedulerMetadataOfferKey)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var exportCmd = &cobra.Command{
Run: func(_ *cobra.Command, args []string) {
name := args[0]

fmt.Printf("WARNING: Exporting the account will expose secret key material!\n")
common.Warn("WARNING: Exporting the account will expose secret key material!")
acc := common.LoadAccount(config.Global(), name)
accCfg, _ := common.LoadAccountConfig(config.Global(), name)

Expand Down
6 changes: 3 additions & 3 deletions cmd/wallet/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var (
}

var kindRaw string
err = survey.AskOne(&survey.Select{
err = common.Ask(&survey.Select{
Message: "Kind:",
Options: supportedKinds,
}, &kindRaw)
Expand All @@ -57,7 +57,7 @@ var (
cobra.CheckErr(err)

// Ask for wallet configuration.
afCfg, err = af.GetConfigFromSurvey(&kind)
afCfg, err = af.GetConfigFromSurvey(&kind, common.SurveyStdio)
cobra.CheckErr(err)
} else {
afCfg["algorithm"] = algorithm
Expand Down Expand Up @@ -88,7 +88,7 @@ var (
Validate: af.DataValidator(kind, afCfg),
},
}
err = survey.Ask(questions, &answers)
err = common.AskMulti(questions, &answers)
cobra.CheckErr(err)
// Ask for passphrase.
passphrase = common.AskNewPassphrase()
Expand Down
9 changes: 5 additions & 4 deletions cmd/wallet/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wallet

import (
"fmt"
"os"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -31,15 +32,15 @@ var rmCmd = &cobra.Command{
}

if !common.GetAnswerYes() {
fmt.Printf("WARNING: Removing the account will ERASE secret key material!\n")
fmt.Printf("WARNING: THIS ACTION IS IRREVERSIBLE!\n")
fmt.Fprintln(os.Stderr, "WARNING: Removing the account will ERASE secret key material!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a number of other warnings and hints throughout the code. Do you think all of those should now be redirected to stderr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update on all places yes (and introduce a helper method)

fmt.Fprintln(os.Stderr, "WARNING: THIS ACTION IS IRREVERSIBLE!")

var result string
confirmText := fmt.Sprintf("I really want to remove accounts: %s", strings.Join(uniqueArgs, ", "))
prompt := &survey.Input{
confirmPrompt := &survey.Input{
Message: fmt.Sprintf("Enter '%s' (without quotes) to confirm removal:", confirmText),
}
err := survey.AskOne(prompt, &result)
err := common.Ask(confirmPrompt, &result)
cobra.CheckErr(err)

if result != confirmText {
Expand Down
1 change: 1 addition & 0 deletions examples/account/allow-paratime.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/allow.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/amend-commission-schedule.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/burn.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/delegate-paratime.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Fee:
Network: testnet
ParaTime: sapphire
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/delegate.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/deposit-eth.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Fee:
Network: testnet
ParaTime: sapphire
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/deposit-named.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Fee:
Network: testnet
ParaTime: sapphire
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/deposit-oasis.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Fee:
Network: testnet
ParaTime: sapphire
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/deposit.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Fee:
Network: testnet
ParaTime: sapphire
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/entity-deregister.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/entity-register.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/node-unfreeze.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Fee:
Network: testnet
ParaTime: none (consensus layer)
Account: oscar
(In case you are using a hardware-based signer you may need to confirm on device.)
1 change: 1 addition & 0 deletions examples/account/transfer-eth.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Fee:
Network: testnet
ParaTime: sapphire
Account: orlando
(In case you are using a hardware-based signer you may need to confirm on device.)
Loading
Loading