-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added management server and integrated CLI with it
- Loading branch information
Showing
51 changed files
with
3,320 additions
and
827 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/sentinel-official/cli-client/services/wireguard" | ||
wireguardtypes "github.com/sentinel-official/cli-client/services/wireguard/types" | ||
clienttypes "github.com/sentinel-official/cli-client/types" | ||
"github.com/sentinel-official/cli-client/context" | ||
clitypes "github.com/sentinel-official/cli-client/types" | ||
) | ||
|
||
func DisconnectCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "disconnect", | ||
Short: "Disconnect from a node", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx, err := client.GetClientTxContext(cmd) | ||
cc, err := context.NewClientContextFromCmd(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
status = clienttypes.NewStatus() | ||
statusFilePath = filepath.Join(ctx.HomeDir, "status.json") | ||
) | ||
|
||
if err := status.LoadFromPath(statusFilePath); err != nil { | ||
status, err := cc.GetStatus() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if status.IFace != "" { | ||
var ( | ||
service = wireguard.NewWireGuard(). | ||
WithConfig( | ||
&wireguardtypes.Config{ | ||
Name: status.IFace, | ||
}, | ||
) | ||
) | ||
|
||
if service.IsUp() { | ||
if err := service.PreDown(); err != nil { | ||
return err | ||
} | ||
if err := service.Down(); err != nil { | ||
return err | ||
} | ||
if err := service.PostDown(); err != nil { | ||
return err | ||
} | ||
if err := cc.Disconnect(); err != nil { | ||
return err | ||
} | ||
|
||
return os.Remove(statusFilePath) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
clitypes.AddFlagsToCmd(cmd) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagBroadcastMode) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagChainID) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagFrom) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagGas) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagGasPrices) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagKeyringBackend) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagKeyringHome) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagMemo) | ||
_ = cmd.Flags().MarkHidden(clitypes.FlagRPCAddress) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/input" | ||
"github.com/cosmos/go-bip39" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/sentinel-official/cli-client/context" | ||
clitypes "github.com/sentinel-official/cli-client/types" | ||
keyringutils "github.com/sentinel-official/cli-client/utils/keyring" | ||
) | ||
|
||
func KeysCmd() *cobra.Command { | ||
var ( | ||
cmd = &cobra.Command{ | ||
Use: "keys", | ||
Short: "Keys subcommands", | ||
DisableFlagParsing: true, | ||
SuggestionsMinimumDistance: 2, | ||
RunE: client.ValidateCmd, | ||
} | ||
) | ||
|
||
cmd.AddCommand( | ||
addCmd(), | ||
deleteCmd(), | ||
listCmd(), | ||
showCmd(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func addCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add [name]", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cc, err := context.NewClientContextFromCmd(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
coinType, err := cmd.Flags().GetUint32(clitypes.FlagCoinType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
account, err := cmd.Flags().GetUint32(clitypes.FlagAccount) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
index, err := cmd.Flags().GetUint32(clitypes.FlagIndex) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
recoverKey, err := cmd.Flags().GetBool(clitypes.FlagRecover) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
reader = bufio.NewReader(cmd.InOrStdin()) | ||
) | ||
|
||
password, err := keyringutils.ReadPassword(cc.KeyringBackend, reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entropy, err := bip39.NewEntropy(256) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mnemonic, err := bip39.NewMnemonic(entropy) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if recoverKey { | ||
mnemonic, err = input.GetString("Enter your bip39 mnemonic", reader) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
result, err := cc.AddKey(password, args[0], mnemonic, "", coinType, account, index) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "**Important** write this mnemonic phrase in a safe place\n") | ||
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s\n", mnemonic) | ||
|
||
fmt.Println(result) | ||
return nil | ||
}, | ||
} | ||
|
||
clitypes.AddKeyringFlagsToCmd(cmd) | ||
cmd.Flags().Bool(clitypes.FlagRecover, false, "provide mnemonic phrase to recover an existing key") | ||
cmd.Flags().Uint32(clitypes.FlagCoinType, 118, "coin type number for HD derivation") | ||
cmd.Flags().Uint32(clitypes.FlagAccount, 0, "account number for HD derivation") | ||
cmd.Flags().Uint32(clitypes.FlagIndex, 0, "address index number for HD derivation") | ||
|
||
return cmd | ||
} | ||
|
||
func deleteCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "delete [name]", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
cc, err := context.NewClientContextFromCmd(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
reader = bufio.NewReader(cmd.InOrStdin()) | ||
) | ||
|
||
password, err := keyringutils.ReadPassword(cc.KeyringBackend, reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cc.DeleteKey(password, args[0]) | ||
}, | ||
} | ||
|
||
clitypes.AddKeyringFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func listCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
cc, err := context.NewClientContextFromCmd(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
reader = bufio.NewReader(cmd.InOrStdin()) | ||
) | ||
|
||
password, err := keyringutils.ReadPassword(cc.KeyringBackend, reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result, err := cc.GetKeys(password) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(result) | ||
return nil | ||
}, | ||
} | ||
|
||
clitypes.AddKeyringFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func showCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "show [name]", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
cc, err := context.NewClientContextFromCmd(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
reader = bufio.NewReader(cmd.InOrStdin()) | ||
) | ||
|
||
password, err := keyringutils.ReadPassword(cc.KeyringBackend, reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result, err := cc.GetKey(password, args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(result) | ||
return nil | ||
}, | ||
} | ||
|
||
clitypes.AddKeyringFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
Oops, something went wrong.