From 29ea164539dd9f4af70db3907010b4c1d792fe26 Mon Sep 17 00:00:00 2001 From: Sandokan Date: Wed, 8 Apr 2020 20:09:47 -0300 Subject: [PATCH 1/3] fix home dir, repo list --- cmd/main.go | 12 ++++++++---- pkg/formula/default_repo.go | 6 +++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8f9fe103e..c669af75f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -34,7 +34,7 @@ const ( ) func main() { - homePath := ritchieHomePath() + homePath := ritchieHomeDir() rootCmd := buildCommands(homePath) if err := rootCmd.Execute(); err != nil { @@ -43,12 +43,16 @@ func main() { } } -func ritchieHomePath() string { +func homeDir() string { usr, err := user.Current() if err != nil { panic(err) } - return fmt.Sprintf(ritchieHomePattern, usr.HomeDir) + return usr.HomeDir +} + +func ritchieHomeDir() string { + return fmt.Sprintf(ritchieHomePattern, homeDir()) } func buildCommands(ritchieHomePath string) *cobra.Command { @@ -98,7 +102,7 @@ func buildCommands(ritchieHomePath string) *cobra.Command { envResolvers[env.Credential] = credResolver formulaRunner := formula.NewRunner(ritchieHomePath, envResolvers, http.DefaultClient, treeManager) - formulaCreator := formula.NewCreator(ritchieHomePath, treeManager) + formulaCreator := formula.NewCreator(homeDir(), treeManager) rootCmd := cmd.NewRootCmd(workspaceManager, loginManager, repoManager, sessionValidator) diff --git a/pkg/formula/default_repo.go b/pkg/formula/default_repo.go index bb4078f6a..eb3777620 100644 --- a/pkg/formula/default_repo.go +++ b/pkg/formula/default_repo.go @@ -183,9 +183,13 @@ func (dm RepoManager) Delete(name string) error { func (dm RepoManager) List() ([]Repository, error) { f, err := dm.loadReposFromDisk() - if fileutil.IsNotExistErr(err) || len(f.Values) == 0 { + + if fileutil.IsNotExistErr(err) { return nil, ErrNoRepoToShow } + if len(f.Values) == 0 { + return []Repository{}, nil + } sort.Sort(ByPriority(f.Values)) From fffa827c038e6c085f23a1a0e093ff7283d4e5d7 Mon Sep 17 00:00:00 2001 From: Sandokan Date: Wed, 8 Apr 2020 22:31:29 -0300 Subject: [PATCH 2/3] separate binaries into different folders --- Makefile | 37 +++---- cmd/main.go | 171 ------------------------------- cmd/single/main.go | 104 +++++++++++++++++++ cmd/team/main.go | 125 ++++++++++++++++++++++ pkg/api/api.go | 29 ++++++ pkg/cmd/root.go | 35 ++++--- pkg/cmd/set_credential.go | 27 +++-- pkg/cmd/team.go | 5 + pkg/env/env.go | 11 -- pkg/formula/default_repo.go | 17 ++- pkg/security/oauth/login.go | 6 +- pkg/security/oauth/logout.go | 6 +- pkg/security/oauth/oauth.go | 5 +- pkg/security/secteam/login.go | 2 +- pkg/security/secteam/logout.go | 7 +- pkg/security/secteam/provider.go | 8 +- pkg/validator/validator.go | 5 +- 17 files changed, 360 insertions(+), 240 deletions(-) delete mode 100644 cmd/main.go create mode 100644 cmd/single/main.go create mode 100644 cmd/team/main.go create mode 100644 pkg/cmd/team.go diff --git a/Makefile b/Makefile index 5986beb40..bda21d7c9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ # Go parameters +TEAM=team +SINGLE=single GOCMD=go GOBUILD=$(GOCMD) build GOCLEAN=$(GOCMD) clean @@ -6,10 +8,9 @@ GOTEST=$(GOCMD) test GOTOOLCOVER=$(GOCMD) tool cover GOGET=$(GOCMD) get BINARY_NAME=rit -CMD_PATH=./cmd/main.go +SINGLE_CMD_PATH=./cmd/$(SINGLE)/main.go +TEAM_CMD_PATH=./cmd/$(TEAM)/main.go BIN=bin -TEAM=team -SINGLE=single DIST=dist DIST_MAC=$(DIST)/mac DIST_MAC_TEAM=$(DIST_MAC)/$(TEAM) @@ -31,17 +32,17 @@ RITCHIE_ENV=$(shell VERSION=$(VERSION) ./ritchie_env.sh) build: mkdir -p $(DIST_MAC_TEAM) $(DIST_MAC_SINGLE) $(DIST_LINUX_TEAM) $(DIST_LINUX_SINGLE) $(DIST_WIN_TEAM) $(DIST_WIN_SINGLE) #LINUX - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=$(RITCHIE_SERVER) ' -o ./$(DIST_LINUX_TEAM)/$(BINARY_NAME) -v $(CMD_PATH) + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_LINUX_TEAM)/$(BINARY_NAME) -v $(TEAM_CMD_PATH) #MAC - GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_MAC_TEAM)/$(BINARY_NAME) -v $(CMD_PATH) + GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_MAC_TEAM)/$(BINARY_NAME) -v $(TEAM_CMD_PATH) #WINDOWS 64 - GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_WIN_TEAM)/$(BINARY_NAME).exe -v $(CMD_PATH) + GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_WIN_TEAM)/$(BINARY_NAME).exe -v $(TEAM_CMD_PATH) #LINUX SINGLE - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.Edition=$(SINGLE) -X $(MODULE)/pkg/env.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_LINUX_SINGLE)/$(BINARY_NAME) -v $(CMD_PATH) + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_LINUX_SINGLE)/$(BINARY_NAME) -v $(SINGLE_CMD_PATH) #MAC SINGLE - GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.Edition=$(SINGLE) -X $(MODULE)/pkg/env.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_MAC_SINGLE)/$(BINARY_NAME) -v $(CMD_PATH) + GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_MAC_SINGLE)/$(BINARY_NAME) -v $(SINGLE_CMD_PATH) #WINDOWS 64 SINGLE - GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.Edition=$(SINGLE) -X $(MODULE)/pkg/env.ServerURL=$(RITCHIE_SERVER)' -o ./$(DIST_WIN_SINGLE)/$(BINARY_NAME).exe -v $(CMD_PATH) + GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_WIN_SINGLE)/$(BINARY_NAME).exe -v $(SINGLE_CMD_PATH) ifneq "$(BUCKET)" "" echo $(BUCKET) aws s3 sync dist s3://$(BUCKET)/$(RELEASE_VERSION) --include "*" @@ -54,26 +55,26 @@ endif build-qa: mkdir -p $(DIST_MAC_TEAM) $(DIST_MAC_SINGLE) $(DIST_LINUX_TEAM) $(DIST_LINUX_SINGLE) $(DIST_WIN_TEAM) $(DIST_WIN_SINGLE) #LINUX - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=https://ritchie-server.itiaws.dev' -o ./$(DIST_LINUX_TEAM)/$(BINARY_NAME) -v $(CMD_PATH) + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=https://ritchie-server.itiaws.dev' -o ./$(DIST_LINUX_TEAM)/$(BINARY_NAME) -v $(TEAM_CMD_PATH) #MAC - GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=https://ritchie-server.itiaws.dev' -o ./$(DIST_MAC_TEAM)/$(BINARY_NAME) -v $(CMD_PATH) + GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=https://ritchie-server.itiaws.dev' -o ./$(DIST_MAC_TEAM)/$(BINARY_NAME) -v $(TEAM_CMD_PATH) #WINDOWS 64 - GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=https://ritchie-server.itiaws.dev' -o ./$(DIST_WIN_TEAM)/$(BINARY_NAME).exe -v $(CMD_PATH) + GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=https://ritchie-server.itiaws.dev' -o ./$(DIST_WIN_TEAM)/$(BINARY_NAME).exe -v $(TEAM_CMD_PATH) #LINUX SINGLE - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=https://ritchie-server.itiaws.dev -X $(MODULE)/pkg/env.Edition=$(SINGLE)' -o ./$(DIST_LINUX_SINGLE)/$(BINARY_NAME) -v $(CMD_PATH) + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_LINUX_SINGLE)/$(BINARY_NAME) -v $(SINGLE_CMD_PATH) #MAC SINGLE - GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=https://ritchie-server.itiaws.dev -X $(MODULE)/pkg/env.Edition=$(SINGLE)' -o ./$(DIST_MAC_SINGLE)/$(BINARY_NAME) -v $(CMD_PATH) + GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_MAC_SINGLE)/$(BINARY_NAME) -v $(SINGLE_CMD_PATH) #WINDOWS 64 SINGLE - GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=https://ritchie-server.itiaws.dev -X $(MODULE)/pkg/env.Edition=$(SINGLE)' -o ./$(DIST_WIN_SINGLE)/$(BINARY_NAME).exe -v $(CMD_PATH) + GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=qa -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_WIN_SINGLE)/$(BINARY_NAME).exe -v $(SINGLE_CMD_PATH) build-team-local: mkdir -p $(DIST_MAC_TEAM) $(DIST_MAC_SINGLE) $(DIST_LINUX_TEAM) #LINUX - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=dev -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=http://localhost:8080' -o ./$(DIST_LINUX_TEAM)/$(BINARY_NAME) -v $(CMD_PATH) + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=dev -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=http://localhost:8080' -o ./$(DIST_LINUX_TEAM)/$(BINARY_NAME) -v $(TEAM_CMD_PATH) #MAC - GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=dev -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=http://localhost:8080' -o ./$(DIST_MAC_TEAM)/$(BINARY_NAME) -v $(CMD_PATH) + GOOS=darwin GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=dev -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=http://localhost:8080' -o ./$(DIST_MAC_TEAM)/$(BINARY_NAME) -v $(TEAM_CMD_PATH) #WINDOWS 64 - GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=dev -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/env.ServerURL=http://localhost:8080' -o ./$(DIST_WIN_TEAM)/$(BINARY_NAME).exe -v $(CMD_PATH) + GOOS=windows GOARCH=amd64 $(GOBUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=dev -X $(MODULE)/pkg/cmd.BuildDate=$(DATE) -X $(MODULE)/pkg/cmd.ServerURL=http://localhost:8080' -o ./$(DIST_WIN_TEAM)/$(BINARY_NAME).exe -v $(TEAM_CMD_PATH) test: mkdir -p $(BIN) diff --git a/cmd/main.go b/cmd/main.go deleted file mode 100644 index c669af75f..000000000 --- a/cmd/main.go +++ /dev/null @@ -1,171 +0,0 @@ -package main - -import ( - "fmt" - "github.com/ZupIT/ritchie-cli/pkg/credential" - "net/http" - "os" - "os/user" - "time" - - "github.com/spf13/cobra" - - "github.com/ZupIT/ritchie-cli/pkg/api" - "github.com/ZupIT/ritchie-cli/pkg/autocomplete" - "github.com/ZupIT/ritchie-cli/pkg/cmd" - "github.com/ZupIT/ritchie-cli/pkg/credential/credsingle" - "github.com/ZupIT/ritchie-cli/pkg/credential/credteam" - "github.com/ZupIT/ritchie-cli/pkg/env" - "github.com/ZupIT/ritchie-cli/pkg/env/envcredential" - "github.com/ZupIT/ritchie-cli/pkg/formula" - "github.com/ZupIT/ritchie-cli/pkg/metrics" - "github.com/ZupIT/ritchie-cli/pkg/rcontext" - "github.com/ZupIT/ritchie-cli/pkg/security" - "github.com/ZupIT/ritchie-cli/pkg/security/secsingle" - "github.com/ZupIT/ritchie-cli/pkg/security/secteam" - "github.com/ZupIT/ritchie-cli/pkg/session" - "github.com/ZupIT/ritchie-cli/pkg/session/sesssingle" - "github.com/ZupIT/ritchie-cli/pkg/session/sessteam" - "github.com/ZupIT/ritchie-cli/pkg/workspace" -) - -const ( - ritchieHomePattern = "%s/.rit" -) - -func main() { - homePath := ritchieHomeDir() - rootCmd := buildCommands(homePath) - - if err := rootCmd.Execute(); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "Error: %+v\n", err) - os.Exit(1) - } -} - -func homeDir() string { - usr, err := user.Current() - if err != nil { - panic(err) - } - return usr.HomeDir -} - -func ritchieHomeDir() string { - return fmt.Sprintf(ritchieHomePattern, homeDir()) -} - -func buildCommands(ritchieHomePath string) *cobra.Command { - sessionManager := session.NewManager(ritchieHomePath) - workspaceManager := workspace.NewChecker(ritchieHomePath) - ctxFinder := rcontext.NewFinder(ritchieHomePath) - ctxSetter := rcontext.NewSetter(ritchieHomePath, ctxFinder) - ctxRemover := rcontext.NewRemover(ritchieHomePath, ctxFinder) - ctxFindSetter := rcontext.NewFindSetter(ritchieHomePath, ctxFinder, ctxSetter) - ctxFindRemover := rcontext.NewFindRemover(ritchieHomePath, ctxFinder, ctxRemover) - repoManager := formula.NewRepoManager(ritchieHomePath, http.DefaultClient, sessionManager) - - var sessionValidator session.Validator - var loginManager security.LoginManager - var logoutManager security.LogoutManager - var userManager security.UserManager - var credSetter credential.Setter - var credFinder credential.Finder - var credSettings credential.Settings - var coreCmds []api.Command - - switch env.Edition { - case env.Single: - coreCmds = api.SingleCoreCmds - sessionValidator = sesssingle.NewValidator(sessionManager) - loginManager = secsingle.NewLoginManager(sessionManager) - credSetter = credsingle.NewSetter(ritchieHomePath, ctxFinder, sessionManager) - credFinder = credsingle.NewFinder(ritchieHomePath, ctxFinder, sessionManager) - case env.Team: - coreCmds = api.TeamCoreCmds - sessionValidator = sessteam.NewValidator(sessionManager) - loginManager = secteam.NewLoginManager(ritchieHomePath, env.ServerURL, security.OAuthProvider, http.DefaultClient, sessionManager) - logoutManager = secteam.NewLogoutManager(security.OAuthProvider, sessionManager) - userManager = secteam.NewUserManager(env.ServerURL, http.DefaultClient, sessionManager) - credSetter = credteam.NewSetter(env.ServerURL, http.DefaultClient, sessionManager, ctxFinder) - credFinder = credteam.NewFinder(env.ServerURL, http.DefaultClient, sessionManager, ctxFinder) - credSettings = credteam.NewSettings(env.ServerURL, http.DefaultClient, sessionManager, ctxFinder) - default: - panic("The env.Edition is required on build") - } - - treeManager := formula.NewTreeManager(ritchieHomePath, repoManager, coreCmds) - autocompleteGen := autocomplete.NewGenerator(treeManager) - - credResolver := envcredential.NewResolver(credFinder) - envResolvers := make(env.Resolvers) - envResolvers[env.Credential] = credResolver - - formulaRunner := formula.NewRunner(ritchieHomePath, envResolvers, http.DefaultClient, treeManager) - formulaCreator := formula.NewCreator(homeDir(), treeManager) - - rootCmd := cmd.NewRootCmd(workspaceManager, loginManager, repoManager, sessionValidator) - - // level 1 - autocompleteCmd := cmd.NewAutocompleteCmd() - addCmd := cmd.NewAddCmd() - cleanCmd := cmd.NewCleanCmd() - createCmd := cmd.NewCreateCmd() - deleteCmd := cmd.NewDeleteCmd() - listCmd := cmd.NewListCmd() - loginCmd := cmd.NewLoginCmd(loginManager, repoManager) - logoutCmd := cmd.NewLogoutCmd(logoutManager) - setCmd := cmd.NewSetCmd() - showCmd := cmd.NewShowCmd() - updateCmd := cmd.NewUpdateCmd() - - // level 2 - setCredentialCmd := cmd.NewSetCredentialCmd(credSetter, credSettings) - createUserCmd := cmd.NewCreateUserCmd(userManager) - deleteUserCmd := cmd.NewDeleteUserCmd(userManager) - deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover) - setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter) - showCtxCmd := cmd.NewShowContextCmd(ctxFinder) - addRepoCmd := cmd.NewAddRepoCmd(repoManager) - cleanRepoCmd := cmd.NewCleanRepoCmd(repoManager) - deleteRepoCmd := cmd.NewDeleteRepoCmd(repoManager) - listRepoCmd := cmd.NewListRepoCmd(repoManager) - updateRepoCmd := cmd.NewUpdateRepoCmd(repoManager) - autocompleteZsh := cmd.NewAutocompleteZsh(autocompleteGen) - autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen) - createFormulaCmd := cmd.NewCreateFormulaCmd(formulaCreator) - - autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash) - addCmd.AddCommand(addRepoCmd) - cleanCmd.AddCommand(cleanRepoCmd) - createCmd.AddCommand(createUserCmd, createFormulaCmd) - deleteCmd.AddCommand(deleteUserCmd, deleteRepoCmd, deleteCtxCmd) - listCmd.AddCommand(listRepoCmd) - setCmd.AddCommand(setCredentialCmd, setCtxCmd) - showCmd.AddCommand(showCtxCmd) - updateCmd.AddCommand(updateRepoCmd) - - rootCmd.AddCommand(addCmd, autocompleteCmd, cleanCmd, createCmd, deleteCmd, listCmd, loginCmd, logoutCmd, setCmd, showCmd, updateCmd) - if env.Edition == env.Single { - createCmd.RemoveCommand(createUserCmd) - deleteCmd.RemoveCommand(deleteUserCmd) - rootCmd.RemoveCommand(loginCmd, logoutCmd) - } - - formulaCmd := cmd.NewFormulaCommand(coreCmds, treeManager, formulaRunner) - err := formulaCmd.Add(rootCmd) - if err != nil { - panic(err) - } - - sendMetrics(sessionManager) - - return rootCmd -} - -func sendMetrics(sessionManager session.DefaultManager) { - if env.Edition == env.Team { - metricsManager := metrics.NewSender(env.ServerURL, &http.Client{Timeout: 2 * time.Second}, sessionManager) - go metricsManager.SendCommand() - } -} diff --git a/cmd/single/main.go b/cmd/single/main.go new file mode 100644 index 000000000..0c66254e1 --- /dev/null +++ b/cmd/single/main.go @@ -0,0 +1,104 @@ +package main + +import ( + "fmt" + "github.com/spf13/cobra" + "net/http" + "os" + + "github.com/ZupIT/ritchie-cli/pkg/api" + "github.com/ZupIT/ritchie-cli/pkg/autocomplete" + "github.com/ZupIT/ritchie-cli/pkg/cmd" + "github.com/ZupIT/ritchie-cli/pkg/credential/credsingle" + "github.com/ZupIT/ritchie-cli/pkg/env" + "github.com/ZupIT/ritchie-cli/pkg/env/envcredential" + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/rcontext" + "github.com/ZupIT/ritchie-cli/pkg/security/secsingle" + "github.com/ZupIT/ritchie-cli/pkg/session" + "github.com/ZupIT/ritchie-cli/pkg/session/sesssingle" + "github.com/ZupIT/ritchie-cli/pkg/workspace" +) + + + +func main() { + rootCmd := buildCommands() + if err := rootCmd.Execute(); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Error: %+v\n", err) + os.Exit(1) + } +} + +func buildCommands() *cobra.Command { + userHomeDir := api.UserHomeDir() + ritchieHomeDir := api.RitchieHomeDir() + + // deps + sessionManager := session.NewManager(ritchieHomeDir) + workspaceManager := workspace.NewChecker(ritchieHomeDir) + ctxFinder := rcontext.NewFinder(ritchieHomeDir) + ctxSetter := rcontext.NewSetter(ritchieHomeDir, ctxFinder) + ctxRemover := rcontext.NewRemover(ritchieHomeDir, ctxFinder) + ctxFindSetter := rcontext.NewFindSetter(ritchieHomeDir, ctxFinder, ctxSetter) + ctxFindRemover := rcontext.NewFindRemover(ritchieHomeDir, ctxFinder, ctxRemover) + repoManager := formula.NewSingleRepoManager(ritchieHomeDir, http.DefaultClient, sessionManager) + sessionValidator := sesssingle.NewValidator(sessionManager) + loginManager := secsingle.NewLoginManager(sessionManager) + credSetter := credsingle.NewSetter(ritchieHomeDir, ctxFinder, sessionManager) + credFinder := credsingle.NewFinder(ritchieHomeDir, ctxFinder, sessionManager) + treeManager := formula.NewTreeManager(ritchieHomeDir, repoManager, api.SingleCoreCmds) + autocompleteGen := autocomplete.NewGenerator(treeManager) + credResolver := envcredential.NewResolver(credFinder) + envResolvers := make(env.Resolvers) + envResolvers[env.Credential] = credResolver + formulaRunner := formula.NewRunner(ritchieHomeDir, envResolvers, http.DefaultClient, treeManager) + formulaCreator := formula.NewCreator(userHomeDir, treeManager) + + //commands + rootCmd := cmd.NewRootCmd(workspaceManager, loginManager, repoManager, sessionValidator, api.Single) + + // level 1 + autocompleteCmd := cmd.NewAutocompleteCmd() + addCmd := cmd.NewAddCmd() + cleanCmd := cmd.NewCleanCmd() + createCmd := cmd.NewCreateCmd() + deleteCmd := cmd.NewDeleteCmd() + listCmd := cmd.NewListCmd() + setCmd := cmd.NewSetCmd() + showCmd := cmd.NewShowCmd() + updateCmd := cmd.NewUpdateCmd() + + // level 2 + setCredentialCmd := cmd.NewSingleSetCredentialCmd(credSetter) + deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover) + setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter) + showCtxCmd := cmd.NewShowContextCmd(ctxFinder) + addRepoCmd := cmd.NewAddRepoCmd(repoManager) + cleanRepoCmd := cmd.NewCleanRepoCmd(repoManager) + deleteRepoCmd := cmd.NewDeleteRepoCmd(repoManager) + listRepoCmd := cmd.NewListRepoCmd(repoManager) + updateRepoCmd := cmd.NewUpdateRepoCmd(repoManager) + autocompleteZsh := cmd.NewAutocompleteZsh(autocompleteGen) + autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen) + createFormulaCmd := cmd.NewCreateFormulaCmd(formulaCreator) + + autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash) + addCmd.AddCommand(addRepoCmd) + cleanCmd.AddCommand(cleanRepoCmd) + createCmd.AddCommand(createFormulaCmd) + deleteCmd.AddCommand(deleteRepoCmd, deleteCtxCmd) + listCmd.AddCommand(listRepoCmd) + setCmd.AddCommand(setCredentialCmd, setCtxCmd) + showCmd.AddCommand(showCtxCmd) + updateCmd.AddCommand(updateRepoCmd) + + rootCmd.AddCommand(addCmd, autocompleteCmd, cleanCmd, createCmd, deleteCmd, listCmd, setCmd, showCmd, updateCmd) + + formulaCmd := cmd.NewFormulaCommand(api.SingleCoreCmds, treeManager, formulaRunner) + if err := formulaCmd.Add(rootCmd); err != nil { + panic(err) + } + + return rootCmd +} diff --git a/cmd/team/main.go b/cmd/team/main.go new file mode 100644 index 000000000..3e4d904fc --- /dev/null +++ b/cmd/team/main.go @@ -0,0 +1,125 @@ +package main + +import ( + "fmt" + "net/http" + "os" + "time" + + "github.com/ZupIT/ritchie-cli/pkg/api" + "github.com/ZupIT/ritchie-cli/pkg/autocomplete" + "github.com/ZupIT/ritchie-cli/pkg/cmd" + "github.com/ZupIT/ritchie-cli/pkg/credential/credteam" + "github.com/ZupIT/ritchie-cli/pkg/env" + "github.com/ZupIT/ritchie-cli/pkg/env/envcredential" + "github.com/ZupIT/ritchie-cli/pkg/formula" + "github.com/ZupIT/ritchie-cli/pkg/metrics" + "github.com/ZupIT/ritchie-cli/pkg/rcontext" + "github.com/ZupIT/ritchie-cli/pkg/security" + "github.com/ZupIT/ritchie-cli/pkg/security/secteam" + "github.com/ZupIT/ritchie-cli/pkg/session" + "github.com/ZupIT/ritchie-cli/pkg/session/sessteam" + "github.com/ZupIT/ritchie-cli/pkg/workspace" + "github.com/spf13/cobra" +) + +func main() { + println("ServerURL: ", cmd.ServerURL) + if cmd.ServerURL == "" { + panic("The env cmd.ServerURL is required") + } + + rootCmd := buildCommands() + if err := rootCmd.Execute(); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Error: %+v\n", err) + os.Exit(1) + } +} + +func buildCommands() *cobra.Command { + userHomeDir := api.UserHomeDir() + ritchieHomeDir := api.RitchieHomeDir() + + //deps + sessionManager := session.NewManager(ritchieHomeDir) + workspaceManager := workspace.NewChecker(ritchieHomeDir) + ctxFinder := rcontext.NewFinder(ritchieHomeDir) + ctxSetter := rcontext.NewSetter(ritchieHomeDir, ctxFinder) + ctxRemover := rcontext.NewRemover(ritchieHomeDir, ctxFinder) + ctxFindSetter := rcontext.NewFindSetter(ritchieHomeDir, ctxFinder, ctxSetter) + ctxFindRemover := rcontext.NewFindRemover(ritchieHomeDir, ctxFinder, ctxRemover) + repoManager := formula.NewTeamRepoManager(ritchieHomeDir, cmd.ServerURL, http.DefaultClient, sessionManager) + sessionValidator := sessteam.NewValidator(sessionManager) + loginManager := secteam.NewLoginManager(ritchieHomeDir, cmd.ServerURL, security.OAuthProvider, http.DefaultClient, sessionManager) + logoutManager := secteam.NewLogoutManager(security.OAuthProvider, sessionManager, cmd.ServerURL) + userManager := secteam.NewUserManager(cmd.ServerURL, http.DefaultClient, sessionManager) + credSetter := credteam.NewSetter(cmd.ServerURL, http.DefaultClient, sessionManager, ctxFinder) + credFinder := credteam.NewFinder(cmd.ServerURL, http.DefaultClient, sessionManager, ctxFinder) + credSettings := credteam.NewSettings(cmd.ServerURL, http.DefaultClient, sessionManager, ctxFinder) + treeManager := formula.NewTreeManager(ritchieHomeDir, repoManager, api.TeamCoreCmds) + autocompleteGen := autocomplete.NewGenerator(treeManager) + credResolver := envcredential.NewResolver(credFinder) + envResolvers := make(env.Resolvers) + envResolvers[env.Credential] = credResolver + + formulaRunner := formula.NewRunner(ritchieHomeDir, envResolvers, http.DefaultClient, treeManager) + formulaCreator := formula.NewCreator(userHomeDir, treeManager) + + //commands + rootCmd := cmd.NewRootCmd(workspaceManager, loginManager, repoManager, sessionValidator, api.Team) + + // level 1 + autocompleteCmd := cmd.NewAutocompleteCmd() + addCmd := cmd.NewAddCmd() + cleanCmd := cmd.NewCleanCmd() + createCmd := cmd.NewCreateCmd() + deleteCmd := cmd.NewDeleteCmd() + listCmd := cmd.NewListCmd() + loginCmd := cmd.NewLoginCmd(loginManager, repoManager) + logoutCmd := cmd.NewLogoutCmd(logoutManager) + setCmd := cmd.NewSetCmd() + showCmd := cmd.NewShowCmd() + updateCmd := cmd.NewUpdateCmd() + + // level 2 + setCredentialCmd := cmd.NewTeamSetCredentialCmd(credSetter, credSettings) + createUserCmd := cmd.NewCreateUserCmd(userManager) + deleteUserCmd := cmd.NewDeleteUserCmd(userManager) + deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover) + setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter) + showCtxCmd := cmd.NewShowContextCmd(ctxFinder) + addRepoCmd := cmd.NewAddRepoCmd(repoManager) + cleanRepoCmd := cmd.NewCleanRepoCmd(repoManager) + deleteRepoCmd := cmd.NewDeleteRepoCmd(repoManager) + listRepoCmd := cmd.NewListRepoCmd(repoManager) + updateRepoCmd := cmd.NewUpdateRepoCmd(repoManager) + autocompleteZsh := cmd.NewAutocompleteZsh(autocompleteGen) + autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen) + createFormulaCmd := cmd.NewCreateFormulaCmd(formulaCreator) + + autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash) + addCmd.AddCommand(addRepoCmd) + cleanCmd.AddCommand(cleanRepoCmd) + createCmd.AddCommand(createUserCmd, createFormulaCmd) + deleteCmd.AddCommand(deleteUserCmd, deleteRepoCmd, deleteCtxCmd) + listCmd.AddCommand(listRepoCmd) + setCmd.AddCommand(setCredentialCmd, setCtxCmd) + showCmd.AddCommand(showCtxCmd) + updateCmd.AddCommand(updateRepoCmd) + + rootCmd.AddCommand(addCmd, autocompleteCmd, cleanCmd, createCmd, deleteCmd, listCmd, loginCmd, logoutCmd, setCmd, showCmd, updateCmd) + + formulaCmd := cmd.NewFormulaCommand(api.TeamCoreCmds, treeManager, formulaRunner) + if err := formulaCmd.Add(rootCmd); err != nil { + panic(err) + } + + sendMetrics(sessionManager) + + return rootCmd +} + +func sendMetrics(sm session.DefaultManager) { + metricsManager := metrics.NewSender(cmd.ServerURL, &http.Client{Timeout: 2 * time.Second}, sm) + go metricsManager.SendCommand() +} diff --git a/pkg/api/api.go b/pkg/api/api.go index c05751f4d..280b04761 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -1,5 +1,18 @@ package api +import ( + "fmt" + "os/user" +) + +const ( + ritchieHomePattern = "%s/.rit" + // Team version + Team = Edition("team") + // Single version + Single = Edition("single") +) + var ( CoreCmds = []Command{ {Parent: "root", Usage: "add"}, @@ -54,3 +67,19 @@ type Formula struct { Config string `json:"config"` RepoURL string `json:"repoUrl"` } + +// Edition type that represents Single or Team. +type Edition string + +func UserHomeDir() string { + usr, err := user.Current() + if err != nil { + panic(err) + } + return usr.HomeDir +} + +func RitchieHomeDir() string { + return fmt.Sprintf(ritchieHomePattern, UserHomeDir()) +} + diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 615e1b2a7..3e99b45d6 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/formula" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/security" @@ -12,7 +13,6 @@ import ( "github.com/spf13/cobra" - "github.com/ZupIT/ritchie-cli/pkg/env" "github.com/ZupIT/ritchie-cli/pkg/slice/sliceutil" "github.com/ZupIT/ritchie-cli/pkg/workspace" ) @@ -45,15 +45,26 @@ type rootCmd struct { loginManager security.LoginManager repoLoader formula.RepoLoader sessionValidator session.Validator + edition api.Edition } // NewRootCmd creates the root for all ritchie commands. -func NewRootCmd(wm workspace.Checker, l security.LoginManager, r formula.RepoLoader, sv session.Validator) *cobra.Command { - o := &rootCmd{wm, l, r, sv} +func NewRootCmd(wm workspace.Checker, + l security.LoginManager, + r formula.RepoLoader, + sv session.Validator, + e api.Edition) *cobra.Command { + o := &rootCmd{ + wm, + l, + r, + sv, + e, + } return &cobra.Command{ Use: cmdUse, - Version: version(), + Version: o.version(), Short: cmdShortDescription, Long: cmdDescription, PersistentPreRunE: o.PreRunFunc(), @@ -83,7 +94,7 @@ func (o *rootCmd) checkSession(commandPath string) error { err := o.sessionValidator.Validate() if err != nil { fmt.Print("To use this command, you need to start a session on Ritchie\n\n") - secret, err := sessionPrompt() + secret, err := o.sessionPrompt() if err != nil { return err } @@ -92,7 +103,7 @@ func (o *rootCmd) checkSession(commandPath string) error { return err } - if env.Edition == env.Team { + if o.edition == api.Team { if err := o.repoLoader.Load(); err != nil { return err } @@ -105,14 +116,14 @@ func (o *rootCmd) checkSession(commandPath string) error { return nil } -func sessionPrompt() (security.Passcode, error) { +func (o *rootCmd) sessionPrompt() (security.Passcode, error) { var passcode string var err error - switch env.Edition { - case env.Single: + switch o.edition { + case api.Single: passcode, err = prompt.Password("Define a passphrase for the session: ") - case env.Team: + case api.Team: passcode, err = prompt.String("Enter your organization: ", true) default: err = errors.New("invalid Ritchie build, no edition defined") @@ -125,6 +136,6 @@ func sessionPrompt() (security.Passcode, error) { return security.Passcode(passcode), nil } -func version() string { - return fmt.Sprintf(versionMsg, Version, env.Edition, BuildDate, runtime.Version()) +func (o *rootCmd) version() string { + return fmt.Sprintf(versionMsg, Version, o.edition, BuildDate, runtime.Version()) } diff --git a/pkg/cmd/set_credential.go b/pkg/cmd/set_credential.go index b3b3181fa..d01017d2b 100644 --- a/pkg/cmd/set_credential.go +++ b/pkg/cmd/set_credential.go @@ -3,13 +3,13 @@ package cmd import ( "errors" "fmt" + "github.com/ZupIT/ritchie-cli/pkg/api" "log" "strings" "github.com/spf13/cobra" "github.com/ZupIT/ritchie-cli/pkg/credential" - "github.com/ZupIT/ritchie-cli/pkg/env" "github.com/ZupIT/ritchie-cli/pkg/prompt" ) @@ -17,11 +17,24 @@ import ( type setCredentialCmd struct { credential.Setter credential.Settings + edition api.Edition } -// NewSetCredentialCmd creates a new cmd instance -func NewSetCredentialCmd(setter credential.Setter, settings credential.Settings) *cobra.Command { - s := &setCredentialCmd{setter, settings} +// NewSingleSetCredentialCmd creates a new cmd instance +func NewSingleSetCredentialCmd(st credential.Setter) *cobra.Command { + s := &setCredentialCmd{Setter: st, edition: api.Single} + + return &cobra.Command{ + Use: "credential", + Short: "Set credential", + Long: `Set credentials for Github, Gitlab, AWS, UserPass, etc.`, + RunE: s.RunFunc(), + } +} + +// NewTeamSetCredentialCmd creates a new cmd instance +func NewTeamSetCredentialCmd(st credential.Setter, si credential.Settings) *cobra.Command { + s := &setCredentialCmd{Setter: st, Settings: si, edition: api.Team} return &cobra.Command{ Use: "credential", @@ -48,10 +61,10 @@ func (s setCredentialCmd) RunFunc() CommandRunnerFunc { } func (s setCredentialCmd) PromptResolver() (credential.Detail, error) { - switch env.Edition { - case env.Single: + switch s.edition { + case api.Single: return s.singlePrompt() - case env.Team: + case api.Team: return s.teamPrompt() default: return credential.Detail{}, errors.New("invalid CLI build, no edition defined") diff --git a/pkg/cmd/team.go b/pkg/cmd/team.go new file mode 100644 index 000000000..2ef1ed9f7 --- /dev/null +++ b/pkg/cmd/team.go @@ -0,0 +1,5 @@ +package cmd + +var ( + ServerURL = "" +) diff --git a/pkg/env/env.go b/pkg/env/env.go index fc5bd9b0d..9f52caea3 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -3,17 +3,6 @@ package env const ( // Credential resolver Credential = "CREDENTIAL" - // Team version - Team = "team" - // Single version - Single = "single" -) - -var ( - //Url Server - ServerURL = "https://ritchie-server.zup.io" - // Edition option - Edition = Team ) type Resolvers map[string]Resolver diff --git a/pkg/formula/default_repo.go b/pkg/formula/default_repo.go index eb3777620..caf8e81c7 100644 --- a/pkg/formula/default_repo.go +++ b/pkg/formula/default_repo.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/ZupIT/ritchie-cli/pkg/env" "github.com/ZupIT/ritchie-cli/pkg/file/fileutil" "github.com/ZupIT/ritchie-cli/pkg/session" "github.com/gofrs/flock" @@ -38,6 +37,7 @@ type RepoManager struct { homePath string httpClient *http.Client sessionManager session.Manager + serverURL string } // ByPriority implements sort.Interface for []Repository based on @@ -48,7 +48,7 @@ func (a ByPriority) Len() int { return len(a) } func (a ByPriority) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByPriority) Less(i, j int) bool { return a[i].Priority < a[j].Priority } -func NewRepoManager(homePath string, hc *http.Client, sm session.Manager) RepoManager { +func NewSingleRepoManager(homePath string, hc *http.Client, sm session.Manager) RepoManager { return RepoManager{ repoFile: fmt.Sprintf(repositoryConfFilePattern, homePath), cacheFile: fmt.Sprintf(repositoryCacheFolderPattern, homePath), @@ -58,6 +58,17 @@ func NewRepoManager(homePath string, hc *http.Client, sm session.Manager) RepoMa } } +func NewTeamRepoManager(homePath string, serverURL string, hc *http.Client, sm session.Manager) RepoManager { + return RepoManager{ + repoFile: fmt.Sprintf(repositoryConfFilePattern, homePath), + cacheFile: fmt.Sprintf(repositoryCacheFolderPattern, homePath), + homePath: homePath, + serverURL : serverURL, + httpClient: hc, + sessionManager: sm, + } +} + func (dm RepoManager) Add(r Repository) error { err := os.MkdirAll(filepath.Dir(dm.cacheFile), os.ModePerm) if err != nil && !os.IsExist(err) { @@ -202,7 +213,7 @@ func (dm RepoManager) Load() error { return err } - url := fmt.Sprintf(providerPath, env.ServerURL) + url := fmt.Sprintf(providerPath, dm.serverURL) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return err diff --git a/pkg/security/oauth/login.go b/pkg/security/oauth/login.go index 021d6c78c..9c41f0479 100644 --- a/pkg/security/oauth/login.go +++ b/pkg/security/oauth/login.go @@ -66,17 +66,19 @@ const ( type LoginManager struct { Resp chan security.ChanResponse + serverURL string } -func NewLoginManager(resp chan security.ChanResponse) *LoginManager { +func NewLoginManager(resp chan security.ChanResponse, serverURL string) *LoginManager { return &LoginManager{ Resp: resp, + serverURL: serverURL, } } // Process login oauth func (l *LoginManager) Login(org string) error { - providerConfig, err := providerConfig(org) + providerConfig, err := providerConfig(org, l.serverURL) if err != nil { l.Resp <- security.ChanResponse{Error: err} return nil diff --git a/pkg/security/oauth/logout.go b/pkg/security/oauth/logout.go index 624b6bcb2..ecdfaa38b 100644 --- a/pkg/security/oauth/logout.go +++ b/pkg/security/oauth/logout.go @@ -63,17 +63,19 @@ const ( type LogoutManager struct { Resp chan security.ChanResponse Org string + serverURL string } -func NewLogoutManager(org string, resp chan security.ChanResponse) *LogoutManager { +func NewLogoutManager(org string, resp chan security.ChanResponse, serverURL string) *LogoutManager { return &LogoutManager{ Resp: resp, Org: org, + serverURL: serverURL, } } func (l *LogoutManager) Logout() error { - providerConfig, err := providerConfig(l.Org) + providerConfig, err := providerConfig(l.Org, l.serverURL) if err != nil { l.Resp <- security.ChanResponse{Error: err} return nil diff --git a/pkg/security/oauth/oauth.go b/pkg/security/oauth/oauth.go index 4301887f1..6e746063d 100644 --- a/pkg/security/oauth/oauth.go +++ b/pkg/security/oauth/oauth.go @@ -3,7 +3,6 @@ package oauth import ( "encoding/json" "fmt" - "github.com/ZupIT/ritchie-cli/pkg/env" "io/ioutil" "net/http" "os/exec" @@ -27,9 +26,9 @@ func callbackURL() string { return fmt.Sprintf(callbackURLPattern, callbackPath) } -func providerConfig(organization string) (ProviderConfig, error) { +func providerConfig(organization, serverURL string) (ProviderConfig, error) { var provideConfig ProviderConfig - url := fmt.Sprintf(providerURLPattern, env.ServerURL) + url := fmt.Sprintf(providerURLPattern, serverURL) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return provideConfig, fmt.Errorf("Failed to providerConfig for org %s. \n%v", organization, err) diff --git a/pkg/security/secteam/login.go b/pkg/security/secteam/login.go index a62869a49..5470d291f 100644 --- a/pkg/security/secteam/login.go +++ b/pkg/security/secteam/login.go @@ -30,7 +30,7 @@ func NewLoginManager( func (l LoginManager) Login(p security.Passcode) error { org := p.String() - cr, err := loginChannelProvider(l.provider, org) + cr, err := loginChannelProvider(l.provider, org, l.serverURL) if err != nil { return err } diff --git a/pkg/security/secteam/logout.go b/pkg/security/secteam/logout.go index e4b21bbcb..0ba798cda 100644 --- a/pkg/security/secteam/logout.go +++ b/pkg/security/secteam/logout.go @@ -8,10 +8,11 @@ import ( type LogoutManager struct { provider security.AuthProvider sessionManager session.Manager + serverURL string } -func NewLogoutManager(p security.AuthProvider, sm session.Manager) LogoutManager { - return LogoutManager{provider: p, sessionManager: sm} +func NewLogoutManager(p security.AuthProvider, sm session.Manager, serverURL string) LogoutManager { + return LogoutManager{provider: p, sessionManager: sm, serverURL: serverURL} } func (l LogoutManager) Logout() error { @@ -20,7 +21,7 @@ func (l LogoutManager) Logout() error { return err } - cr, err := logoutChannelProvider(l.provider, session.Organization) + cr, err := logoutChannelProvider(l.provider, session.Organization, l.serverURL) if err != nil { return err } diff --git a/pkg/security/secteam/provider.go b/pkg/security/secteam/provider.go index 9e5693f50..4b8628528 100644 --- a/pkg/security/secteam/provider.go +++ b/pkg/security/secteam/provider.go @@ -5,11 +5,11 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/security/oauth" ) -func loginChannelProvider(p security.AuthProvider, org string) (chan security.ChanResponse, error) { +func loginChannelProvider(p security.AuthProvider, org, serverURL string) (chan security.ChanResponse, error) { cr := make(chan security.ChanResponse) switch p { case security.OAuthProvider: - oauthCli := oauth.NewLoginManager(cr) + oauthCli := oauth.NewLoginManager(cr, serverURL) go oauthCli.Login(org) default: return nil, security.ErrUnknownProvider @@ -17,11 +17,11 @@ func loginChannelProvider(p security.AuthProvider, org string) (chan security.Ch return cr, nil } -func logoutChannelProvider(p security.AuthProvider, org string) (chan security.ChanResponse, error) { +func logoutChannelProvider(p security.AuthProvider, org, serverURL string) (chan security.ChanResponse, error) { cr := make(chan security.ChanResponse) switch p { case security.OAuthProvider: - oauthCli := oauth.NewLogoutManager(org, cr) + oauthCli := oauth.NewLogoutManager(org, cr, serverURL) go oauthCli.Logout() default: return nil, security.ErrUnknownProvider diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 7732b9dfc..93cb68ba4 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/ZupIT/ritchie-cli/pkg/env" "github.com/ZupIT/ritchie-cli/pkg/file/fileutil" "log" "net/http" @@ -69,8 +68,8 @@ func IsValidEmail(email string) error { } //IsValidVersion Validate version with server -func IsValidVersion(version, org string) { - url := fmt.Sprintf(urlPatternVersion, env.ServerURL) +func IsValidVersion(version, org, serverURL string) { + url := fmt.Sprintf(urlPatternVersion, serverURL) client := &http.Client{Timeout: 2 * time.Second} req, _ := http.NewRequest("GET", url, nil) req.Header.Set("x-org", org) From fca6b016f44d067d61f30c1bfa4c8c99ab31b5f3 Mon Sep 17 00:00:00 2001 From: Sandokan Date: Wed, 8 Apr 2020 22:45:09 -0300 Subject: [PATCH 3/3] remove global var --- pkg/workspace/default_workspace_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/workspace/default_workspace_test.go b/pkg/workspace/default_workspace_test.go index 7554ff23d..c45257196 100644 --- a/pkg/workspace/default_workspace_test.go +++ b/pkg/workspace/default_workspace_test.go @@ -4,8 +4,6 @@ import ( "fmt" "os" "testing" - - "github.com/ZupIT/ritchie-cli/pkg/env" ) var ( @@ -13,7 +11,6 @@ var ( ) func TestMain(m *testing.M) { - env.Edition = env.Single home = fmt.Sprintf("%s/.rit", os.TempDir()) os.Exit(m.Run()) }