-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
405 additions
and
206 deletions.
There are no files selected for viewing
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,96 @@ | ||
package config | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
proctor_config "github.com/gojektech/proctor/config" | ||
"github.com/gojektech/proctor/io" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CreateDirIfNotExist(dir string) { | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
err = os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
func NewCmd(printer io.Printer) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "config", | ||
Short: "Configure proctor client", | ||
Long: "This command helps configure client with proctord host, email id and access token", | ||
Example: fmt.Sprintf("proctor config set %s=example.proctor.com %s=example@proctor.com %s=XXXXX", proctor_config.ProctorHost, proctor_config.EmailId, proctor_config.AccessToken), | ||
Args: cobra.MinimumNArgs(1), | ||
|
||
Run: func(cmd *cobra.Command, args []string) { | ||
configFile := filepath.Join(proctor_config.ConfigFileDir(), "proctor.yaml") | ||
if _, err := os.Stat(configFile); err == nil { | ||
printer.Println("[Warning] This will overwrite current config:", color.FgYellow) | ||
existingProctorConfig, err := ioutil.ReadFile(configFile) | ||
if err != nil { | ||
printer.Println(fmt.Sprintf("Error reading config file: %s", configFile), color.FgRed) | ||
return | ||
} | ||
|
||
printer.Println(string(existingProctorConfig)) | ||
printer.Println("\nDo you want to continue (Y/n)?\t", color.FgYellow) | ||
|
||
in := bufio.NewReader(os.Stdin) | ||
userPermission, err := in.ReadString('\n') | ||
|
||
if err != nil { | ||
printer.Println("Error getting user permission for overwriting config", color.FgRed) | ||
return | ||
} | ||
|
||
if userPermission != "y\n" && userPermission != "Y\n" { | ||
printer.Println("Skipping configuring proctor client", color.FgYellow) | ||
return | ||
} | ||
} | ||
|
||
CreateDirIfNotExist(proctor_config.ConfigFileDir()) | ||
var proctorHost, emailID, accessToken string | ||
for _, v := range args { | ||
arg := strings.Split(v, "=") | ||
|
||
if len(arg) != 2 { | ||
printer.Println(fmt.Sprintf("%-40s %-100s", "\nIncorrect config format: %s. Correct format: CONFIG_KEY=VALUE\n", v), color.FgRed) | ||
return | ||
} | ||
|
||
switch arg[0] { | ||
case proctor_config.ProctorHost: | ||
proctorHost = arg[1] | ||
case "EMAIL_ID": | ||
emailID = arg[1] | ||
case "ACCESS_TOKEN": | ||
accessToken = arg[1] | ||
default: | ||
printer.Println(fmt.Sprintf("Proctor doesn't support config key: %s", arg[0]), color.FgYellow) | ||
} | ||
} | ||
|
||
configFileContent := []byte(fmt.Sprintf("%s: "+proctorHost+"\n"+"%s: "+emailID+"\n"+"%s: "+accessToken, proctor_config.ProctorHost, proctor_config.EmailId, proctor_config.AccessToken)) | ||
f, err := os.Create(configFile) | ||
if err != nil { | ||
printer.Println(fmt.Sprintf("Error creating config file %s: %s", configFile, err.Error()), color.FgRed) | ||
} | ||
_, err = f.Write(configFileContent) | ||
if err != nil { | ||
printer.Println(fmt.Sprintf("Error writing content %v \n to config file %s: %s", configFileContent, configFile, err.Error()), color.FgRed) | ||
} | ||
defer f.Close() | ||
printer.Println("Proctor client configured successfully", color.FgGreen) | ||
}, | ||
} | ||
} |
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,48 @@ | ||
package view | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/fatih/color" | ||
proctor_config "github.com/gojektech/proctor/config" | ||
"github.com/gojektech/proctor/io" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CreateDirIfNotExist(dir string) { | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
err = os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
func NewCmd(printer io.Printer) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "show", | ||
Short: "Show proctor client config", | ||
Long: "This command helps view proctor client config", | ||
Example: fmt.Sprintf("proctor config show"), | ||
|
||
Run: func(cmd *cobra.Command, args []string) { | ||
configFile := filepath.Join(proctor_config.ConfigFileDir(), "proctor.yaml") | ||
if _, err := os.Stat(configFile); os.IsNotExist(err) { | ||
printer.Println(fmt.Sprintf("Client Config is absent: %s", configFile), color.FgRed) | ||
printer.Println(fmt.Sprintf("Setup config using `proctor config PROCTOR_HOST=some.host ...`"), color.FgRed) | ||
return | ||
} | ||
|
||
existingProctorConfig, err := ioutil.ReadFile(configFile) | ||
if err != nil { | ||
printer.Println(fmt.Sprintf("Error reading config file: %s", configFile), color.FgRed) | ||
return | ||
} | ||
|
||
printer.Println(string(existingProctorConfig)) | ||
}, | ||
} | ||
} |
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
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
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
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
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
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,12 @@ | ||
package config | ||
|
||
import "github.com/stretchr/testify/mock" | ||
|
||
type MockLoader struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockLoader) Load() (ProctorConfig, ConfigError) { | ||
args := m.Called() | ||
return args.Get(0).(ProctorConfig), args.Get(1).(ConfigError) | ||
} |
Oops, something went wrong.