Skip to content

Commit da6e08f

Browse files
author
gregorgololicic
committed
configuration for url and error handlers
1 parent 507f19f commit da6e08f

7 files changed

Lines changed: 127 additions & 89 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
config.yaml

cmd/account/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package account
22

33
import (
44
"github.com/sideninja/flow-cli/cmd/account/get"

cmd/config/config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package config
2+
3+
import (
4+
"github.com/sideninja/flow-cli/gateway"
5+
"github.com/sideninja/flow-cli/util"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// NewCmdConfig configuration command
10+
func NewCmdConfig(gateway gateway.IGateway, version string) *cobra.Command {
11+
12+
var configCmd = &cobra.Command{
13+
Use: "config <param>",
14+
Short: "Save persistent configuration",
15+
Long: `
16+
Save persistent configuration variables.
17+
`,
18+
Args: cobra.ExactArgs(0),
19+
RunE: func(c *cobra.Command, args []string) error {
20+
util.PromptURL()
21+
return nil
22+
},
23+
}
24+
25+
return configCmd
26+
}

cmd/helpers.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

cmd/root.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package cmd
22

33
import (
4-
"errors"
5-
64
accountCmd "github.com/sideninja/flow-cli/cmd/account"
5+
configCmd "github.com/sideninja/flow-cli/cmd/config"
76
"github.com/sideninja/flow-cli/gateway"
7+
"github.com/sideninja/flow-cli/util"
88
"github.com/spf13/cobra"
99
)
1010

11-
// SilentErr is silent error passed through
12-
var SilentErr = errors.New("SilentErr")
13-
1411
// NewCmdRoot root command factory
1512
func NewCmdRoot(gateway gateway.IGateway, version string) *cobra.Command {
1613

@@ -33,7 +30,12 @@ Flow CLI tool to interact with flow emulator.`,
3330
//viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
3431

3532
// here we add all commands:
36-
rootCmd.AddCommand(accountCmd.NewCmdAccount(gateway, version))
33+
rootCmd.AddCommand(
34+
accountCmd.NewCmdAccount(gateway, version),
35+
)
36+
rootCmd.AddCommand(
37+
configCmd.NewCmdConfig(gateway, version),
38+
)
3739

3840
// always enabled version and json flags
3941
rootCmd.Flags().BoolP("version", "v", false, "show version information")
@@ -43,7 +45,7 @@ Flow CLI tool to interact with flow emulator.`,
4345
rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
4446
cmd.Printf("\n\033[31mError: %s\033[0m\n\n", err)
4547
cmd.Println(cmd.UsageString())
46-
return SilentErr
48+
return util.SilentErr
4749
})
4850

4951
return rootCmd

main.go

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
package main
22

33
import (
4-
"errors"
5-
"fmt"
6-
"os"
4+
"github.com/sideninja/flow-cli/util"
75

8-
"github.com/onflow/flow-go-sdk/client"
96
"github.com/sideninja/flow-cli/cmd"
10-
cliCmd "github.com/sideninja/flow-cli/cmd"
117
"github.com/sideninja/flow-cli/gateway"
128
"github.com/sideninja/flow-cli/models"
139
"github.com/spf13/viper"
14-
"google.golang.org/grpc/codes"
1510
)
1611

1712
func init() {
18-
viper.SetConfigName("config")
19-
viper.AddConfigPath(".")
20-
viper.SetDefault("APIURL", "localhost:3569")
21-
22-
if err := viper.ReadInConfig(); err != nil {
23-
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
24-
25-
} else {
26-
// Config file was found but another error was produced
27-
}
28-
}
13+
util.InitConfig()
2914
}
3015

3116
func main() {
@@ -34,35 +19,9 @@ func main() {
3419
version := "1.0"
3520

3621
gateway := gateway.CreateGateway(models.GRPC, APIURL)
37-
38-
rootCmd := cliCmd.NewCmdRoot(gateway, version)
22+
rootCmd := cmd.NewCmdRoot(gateway, version)
3923

4024
if err := rootCmd.Execute(); err != nil {
41-
handleError(err)
42-
os.Exit(1)
43-
}
44-
}
45-
46-
func handleError(err error) {
47-
if err != cmd.SilentErr {
48-
// check if error is grpc error
49-
rpcError := client.RPCError{}
50-
if errors.As(err, &rpcError) {
51-
handleGrpcError(rpcError)
52-
} else { // unhandeled error - can be improved
53-
fmt.Printf("\n\033[31mUnhandeled Error: %s\033[0m\n\n", err)
54-
}
55-
}
56-
}
57-
58-
func handleGrpcError(rpcError client.RPCError) {
59-
switch rpcError.GRPCStatus().Code() {
60-
case codes.InvalidArgument:
61-
fmt.Printf("\n\033[31mInvalid Arguments: %s\033[0m\n\n", rpcError.GRPCStatus().Message())
62-
case codes.Unavailable:
63-
fmt.Printf("\nConnection to Flow Emulator was not successful, please make sure your api url is correct. You can set it by: flow [COMMAND] --api URL \n")
64-
fmt.Printf("\n\033[31mConnection Error: %s\033[0m\n\n", rpcError.GRPCStatus().Message())
65-
default:
66-
fmt.Printf("\n\033[31mUnhandeled Error: %s\033[0m\n\n", rpcError.GRPCStatus().Err())
25+
util.HandleError(err)
6726
}
6827
}

util/util.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package util
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
8+
"github.com/manifoldco/promptui"
9+
"github.com/onflow/flow-go-sdk/client"
10+
"github.com/spf13/viper"
11+
"google.golang.org/grpc/codes"
12+
)
13+
14+
// SilentErr is silent error passed through
15+
var SilentErr = errors.New("SilentErr")
16+
17+
// PromptURL enters discovery api url if missing
18+
func PromptURL() {
19+
20+
validate := func(input string) error {
21+
/*_, err := strconv.ParseFloat(input, 64)
22+
if err != nil {
23+
return errors.New("Invalid url")
24+
}*/
25+
return nil
26+
}
27+
28+
prompt := promptui.Prompt{
29+
Label: "Enter Flow emulator discovery API URL",
30+
Validate: validate,
31+
}
32+
33+
result, err := prompt.Run()
34+
35+
if err != nil {
36+
fmt.Printf("Prompt failed %v\n", err)
37+
return
38+
}
39+
40+
viper.Set("APIURL", result)
41+
viper.WriteConfigAs("./config.yaml")
42+
}
43+
44+
// HandleError main error handler for commands
45+
func HandleError(err error) {
46+
if err != SilentErr {
47+
// check if error is grpc error
48+
rpcError := client.RPCError{}
49+
if errors.As(err, &rpcError) {
50+
handleGrpcError(rpcError)
51+
} else { // unhandeled error - can be improved
52+
fmt.Printf("\n\033[31mUnhandeled Error: %s\033[0m\n\n", err)
53+
}
54+
}
55+
56+
os.Exit(1)
57+
}
58+
59+
func handleGrpcError(rpcError client.RPCError) {
60+
switch rpcError.GRPCStatus().Code() {
61+
case codes.InvalidArgument:
62+
fmt.Printf("\n\033[31mInvalid Arguments: %s\033[0m\n\n", rpcError.GRPCStatus().Message())
63+
case codes.Unavailable:
64+
fmt.Printf("\nConnection to Flow Emulator was not successful, please make sure your api url is correct.\n\nSet it by using: flow config \n")
65+
fmt.Printf("\n\033[31mConnection Error: %s\033[0m\n\n", rpcError.GRPCStatus().Message())
66+
PromptURL()
67+
default:
68+
fmt.Printf("\n\033[31mUnhandeled Error: %s\033[0m\n\n", rpcError.GRPCStatus().Err())
69+
}
70+
}
71+
72+
// InitConfig initialize config for viper
73+
func InitConfig() {
74+
viper.SetConfigName("config")
75+
viper.SetConfigType("yaml")
76+
viper.AddConfigPath(".")
77+
viper.SetDefault("APIURL", "localhost:3569")
78+
79+
if err := viper.ReadInConfig(); err != nil {
80+
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
81+
fmt.Println("ERROR not found")
82+
} else {
83+
// Config file was found but another error was produced
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)