Skip to content
Open
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
17 changes: 16 additions & 1 deletion tools/fxconfig/internal/cli/v1/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package v1

import "github.com/spf13/cobra"
import (
"fmt"

"github.com/spf13/cobra"
)

// outputFlag represents an output file path flag.
type outputFlag string
Expand Down Expand Up @@ -48,6 +52,17 @@ func (f *namespaceDeployFlags) bind(cmd *cobra.Command) {
"Wait for transaction finalization (implies --submit)")
}

// Validate checks that the flag combinations are valid.
func (f *namespaceDeployFlags) Validate() error {
if f.submit && !f.endorse {
return fmt.Errorf("the --submit flag requires --endorse")
}
if f.wait && !f.submit {
return fmt.Errorf("the --wait flag requires --submit")
}
return nil
}

// waitFlag represents a flag to wait for transaction finalization.
type waitFlag bool

Expand Down
84 changes: 84 additions & 0 deletions tools/fxconfig/internal/cli/v1/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,87 @@ func TestWaitFlag_Bind(t *testing.T) {
require.NotNil(t, flag)
require.Equal(t, "false", flag.DefValue)
}

func TestNamespaceDeployFlags_Validate(t *testing.T) {
t.Parallel()

tests := []struct {
name string
flags namespaceDeployFlags
wantErr string
}{
{
name: "valid: none",
flags: namespaceDeployFlags{
endorse: false,
submit: false,
wait: false,
},
},
{
name: "valid: endorse only",
flags: namespaceDeployFlags{
endorse: true,
submit: false,
wait: false,
},
},
{
name: "valid: endorse and submit",
flags: namespaceDeployFlags{
endorse: true,
submit: true,
wait: false,
},
},
{
name: "valid: all flags",
flags: namespaceDeployFlags{
endorse: true,
submit: true,
wait: true,
},
},
{
name: "invalid: submit without endorse",
flags: namespaceDeployFlags{
endorse: false,
submit: true,
wait: false,
},
wantErr: "the --submit flag requires --endorse",
},
{
name: "invalid: wait without submit",
flags: namespaceDeployFlags{
endorse: true,
submit: false,
wait: true,
},
wantErr: "the --wait flag requires --submit",
},
{
name: "invalid: wait without submit and endorse",
flags: namespaceDeployFlags{
endorse: false,
submit: false,
wait: true,
},
wantErr: "the --wait flag requires --submit",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := tt.flags.Validate()
if tt.wantErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.wantErr)
} else {
require.NoError(t, err)
}
})
}
}
14 changes: 11 additions & 3 deletions tools/fxconfig/internal/cli/v1/namespace_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Policy Syntax:
• OutOf(2, 'Org1MSP.member', 'Org2MSP.member', 'Org3MSP.member') - 2 of 3 orgs

Transaction Lifecycle Flags:
--endorse Collect endorsement from local MSP
--submit Submit transaction to ordering service
--wait Wait for transaction finalization (implies --submit)
--endorse Collect endorsement from local MSP (if used without --submit, only saves the endorsed tx)
--submit Submit transaction to ordering service (requires --endorse)
--wait Wait for transaction finalization (requires --submit)

Examples:
# Create namespace with single org policy (save to file)
Expand All @@ -61,6 +61,10 @@ Examples:
--output=tx.json`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := namespace.Validate(); err != nil {
return err
}

p := app.PolicyConfig{}
p.Set(string(policy))

Expand All @@ -85,6 +89,10 @@ Examples:
return nil
}

if namespace.endorse && !namespace.submit {
ctx.Printer.Print("Transaction successfully endorsed and saved. You can submit it later using 'fxconfig tx submit'.")
}

o, err := ctx.IOTransactionCodec.Encode(res.TxID, res.Tx)
if err != nil {
return err
Expand Down
15 changes: 14 additions & 1 deletion tools/fxconfig/internal/cli/v1/namespace_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ Examples:
fxconfig namespace update payments \
--policy="OutOf(2, 'Org1MSP.member', 'Org2MSP.member', 'Org3MSP.member')" \
--version=2 \
--output=update_tx.json`,
--output=update_tx.json

Transaction Lifecycle Flags:
--endorse Collect endorsement from local MSP (if used without --submit, only saves the endorsed tx)
--submit Submit transaction to ordering service (requires --endorse)
--wait Wait for transaction finalization (requires --submit)`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := namespace.Validate(); err != nil {
return err
}

p := app.PolicyConfig{}
p.Set(string(policy))

Expand All @@ -84,6 +93,10 @@ Examples:
return nil
}

if namespace.endorse && !namespace.submit {
ctx.Printer.Print("Transaction successfully endorsed and saved. You can submit it later using 'fxconfig tx submit'.")
}

o, err := ctx.IOTransactionCodec.Encode(res.TxID, res.Tx)
if err != nil {
return err
Expand Down