Skip to content

Commit

Permalink
fix(cmd): hack env commands to be available to client (#957)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <troian.ap@gmail.com>
  • Loading branch information
troian committed Nov 29, 2020
1 parent e662958 commit d2ce9b1
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions cmd/akash/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cmd

import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -26,18 +28,36 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/ovrclk/akash/app"
ecmd "github.com/ovrclk/akash/events/cmd"
pcmd "github.com/ovrclk/akash/provider/cmd"
"github.com/ovrclk/akash/sdkutil"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

"github.com/ovrclk/akash/app"
ecmd "github.com/ovrclk/akash/events/cmd"
pcmd "github.com/ovrclk/akash/provider/cmd"
"github.com/ovrclk/akash/sdkutil"
)

func bindFlags(cmd *cobra.Command, v *viper.Viper) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
// Environment variables can't have dashes in them, so bind them to their equivalent
// keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR
_ = v.BindEnv(f.Name, fmt.Sprintf("%s_%s", "AKASH", strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))))
_ = v.BindPFlag(f.Name, f)

// Apply the viper config value to the flag when the flag is not set and viper has a value
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
_ = cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
}
})
}

// NewRootCmd creates a new root command for akash. It is called once in the
// main function.
func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
Expand All @@ -57,11 +77,15 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
Short: "Akash Blockchain Application",
Long: "Akash CLI Utility.\n\nAkash is a peer-to-peer marketplace for computing resources and \na deployment platform for heavily distributed applications. \nFind out more at https://akash.network",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
if err := server.InterceptConfigsPreRunHandler(cmd); err != nil {
return err
}

return server.InterceptConfigsPreRunHandler(cmd)
ctx := server.GetServerContextFromCmd(cmd)

bindFlags(cmd, ctx.Viper)

return client.SetCmdClientContextHandler(initClientCtx, cmd)
},
}

Expand Down

0 comments on commit d2ce9b1

Please sign in to comment.