Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #279 from kaduartur/feature/init
Browse files Browse the repository at this point in the history
Feature/init
  • Loading branch information
victor-schumacher authored Jul 14, 2020
2 parents cf2812e + 2e48b63 commit 9b78166
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 224 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ NEXT_VERSION=$(shell ./.circleci/scripts/next_version.sh)

build-linux:
mkdir -p $(DIST_LINUX)
#LINUX
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO_BUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_LINUX)/$(BINARY_NAME) -v $(CMD_PATH)

build-mac:
mkdir -p $(DIST_MAC)
#MAC
GOOS=darwin GOARCH=amd64 $(GO_BUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_MAC)/$(BINARY_NAME) -v $(CMD_PATH)

build-windows:
mkdir -p $(DIST_WIN)
#WINDOWS
GOOS=windows GOARCH=amd64 $(GO_BUILD) -ldflags '-X $(MODULE)/pkg/cmd.Version=$(VERSION) -X $(MODULE)/pkg/cmd.BuildDate=$(DATE)' -o ./$(DIST_WIN)/$(BINARY_NAME).exe -v $(CMD_PATH)

build: build-linux build-mac build-windows
Expand Down
21 changes: 7 additions & 14 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"os"
"time"

"github.com/ZupIT/ritchie-cli/pkg/formula/repo"
"github.com/ZupIT/ritchie-cli/pkg/formula/runner"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/github"
"github.com/ZupIT/ritchie-cli/pkg/session/validator"

"k8s.io/kubectl/pkg/util/templates"

"github.com/ZupIT/ritchie-cli/pkg/formula/builder"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator"
"github.com/ZupIT/ritchie-cli/pkg/formula/repo"
"github.com/ZupIT/ritchie-cli/pkg/formula/runner"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/github"

"github.com/ZupIT/ritchie-cli/pkg/upgrade"
"github.com/ZupIT/ritchie-cli/pkg/version"
Expand All @@ -34,10 +32,8 @@ import (
fworkspace "github.com/ZupIT/ritchie-cli/pkg/formula/workspace"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"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/stream"
"github.com/ZupIT/ritchie-cli/pkg/workspace"
)

func main() {
Expand Down Expand Up @@ -79,14 +75,11 @@ func buildCommands() *cobra.Command {
repoPrioritySetter := repo.NewPrioritySetter(ritchieHomeDir, fileManager, dirManager)

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)
sessionValidator := validator.NewValidator(sessionManager)
passphraseManager := secsingle.NewPassphraseManager(sessionManager)
credSetter := credsingle.NewSetter(ritchieHomeDir, ctxFinder, sessionManager)
credFinder := credsingle.NewFinder(ritchieHomeDir, ctxFinder, sessionManager)
treeManager := tree.NewTreeManager(ritchieHomeDir, repoLister, api.CoreCmds)
Expand Down Expand Up @@ -120,14 +113,14 @@ func buildCommands() *cobra.Command {
HttpClient: &http.Client{Timeout: 1 * time.Second},
}
defaultUrlFinder := upgrade.DefaultUrlFinder{}
rootCmd := cmd.NewSingleRootCmd(workspaceManager, sessionValidator)
rootCmd := cmd.NewRootCmd(ritchieHomeDir, dirManager)

// level 1
autocompleteCmd := cmd.NewAutocompleteCmd()
addCmd := cmd.NewAddCmd()
createCmd := cmd.NewCreateCmd()
deleteCmd := cmd.NewDeleteCmd()
initCmd := cmd.NewSingleInitCmd(inputPassword, passphraseManager)
initCmd := cmd.NewInitCmd(repoAdder, gitRepo)
listCmd := cmd.NewListCmd()
setCmd := cmd.NewSetCmd()
showCmd := cmd.NewShowCmd()
Expand All @@ -146,7 +139,7 @@ func buildCommands() *cobra.Command {
deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList)
setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList)
showCtxCmd := cmd.NewShowContextCmd(ctxFinder)
addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, gitRepo, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, gitRepo, inputTextValidator, inputPassword, inputURL, inputList, inputBool, inputInt)
updateRepoCmd := cmd.NewUpdateRepoCmd(http.DefaultClient, repoListUpdater, gitRepo, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
listRepoCmd := cmd.NewListRepoCmd(repoLister)
deleteRepoCmd := cmd.NewDeleteRepoCmd(repoLister, inputList, repoDeleter)
Expand Down
41 changes: 30 additions & 11 deletions pkg/cmd/add_repo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -13,11 +14,16 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

var (
ErrRepoNameNotEmpty = errors.New("the field repository name must not be empty")
ErrCommonsRepoName = errors.New("the name \"commons\" is not valid for the repository name, try to enter another name")
)

type AddRepoCmd struct {
client *http.Client
repo formula.RepositoryAddLister
github github.Repositories
prompt.InputText
prompt.InputTextValidator
prompt.InputPassword
prompt.InputURL
prompt.InputList
Expand All @@ -28,22 +34,22 @@ type AddRepoCmd struct {
func NewAddRepoCmd(
repo formula.RepositoryAddLister,
github github.Repositories,
inText prompt.InputText,
inText prompt.InputTextValidator,
inPass prompt.InputPassword,
inUrl prompt.InputURL,
inList prompt.InputList,
inBool prompt.InputBool,
inInt prompt.InputInt,
) *cobra.Command {
addRepo := AddRepoCmd{
repo: repo,
github: github,
InputText: inText,
InputURL: inUrl,
InputList: inList,
InputBool: inBool,
InputInt: inInt,
InputPassword: inPass,
repo: repo,
github: github,
InputTextValidator: inText,
InputURL: inUrl,
InputList: inList,
InputBool: inBool,
InputInt: inInt,
InputPassword: inPass,
}
cmd := &cobra.Command{
Use: "repo",
Expand All @@ -58,7 +64,7 @@ func NewAddRepoCmd(

func (ad AddRepoCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
name, err := ad.Text("Repository name: ", true)
name, err := ad.Text("Repository name: ", ad.repoNameValidator)
if err != nil {
return err
}
Expand Down Expand Up @@ -157,3 +163,16 @@ func (ad AddRepoCmd) runStdin() CommandRunnerFunc {
return nil
}
}

func (ad AddRepoCmd) repoNameValidator(text interface{}) error {
in := text.(string)
if in == "" {
return ErrRepoNameNotEmpty
}

if in == "commons" {
return ErrCommonsRepoName
}

return nil
}
10 changes: 5 additions & 5 deletions pkg/cmd/formulas.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
subCommand = " SUBCOMMAND"
Group = "group"
dockerFlag = "docker"
RootCmd = "root"
rootCmd = "root"
)

type FormulaCommand struct {
Expand All @@ -40,10 +40,10 @@ func NewFormulaCommand(
}
}

func (f FormulaCommand) Add(rootCmd *cobra.Command) error {
func (f FormulaCommand) Add(root *cobra.Command) error {
treeRep := f.treeManager.MergedTree(false)
commands := make(map[string]*cobra.Command)
commands[RootCmd] = rootCmd
commands[rootCmd] = root

for _, cmd := range treeRep.Commands {
cmdPath := api.Command{Parent: cmd.Parent, Usage: cmd.Usage}
Expand All @@ -67,8 +67,8 @@ func (f FormulaCommand) Add(rootCmd *cobra.Command) error {

func newSubCmd(cmd api.Command) *cobra.Command {
var group string
if cmd.Parent == RootCmd {
group = fmt.Sprintf("%s commands:", cmd.Repo)
if cmd.Parent == rootCmd {
group = fmt.Sprintf("%s repo commands:", cmd.Repo)
}

c := &cobra.Command{
Expand Down
87 changes: 39 additions & 48 deletions pkg/cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,74 @@
package cmd

import (
"fmt"
"os"
"time"

"github.com/kaduartur/go-cli-spinner/pkg/spinner"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/github"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/security"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

const (
MsgPhrase = "Define a passphrase for your machine: "
MsgOrganization = "Enter your organization: "
msgOrganizationAlreadyExists = "The organization (%s) already exists. Do you like to override?"
MsgServerURL = "URL of the server [http(s)://host]: "
msgServerURLAlreadyExists = "The server URL(%s) already exists. Do you like to override?"
MsgLogin = "You can perform login to your organization now, or later using [rit login] command. Perform now?"
)

type initSingleCmd struct {
prompt.InputPassword
security.PassphraseManager
const UsageMsg = ` How to contribute new formulas to the Ritchie community?
You must fork the Github repository "https://github.com/ZupIT/ritchie-formulas"
and then follow the step by step below:
∙ git clone https://github.com/{{your_github_user}}/ritchie-formulas
∙ Run the command "rit create formula" and add the location where you cloned your
repository. Rit will create a formula template that you can already test.
∙ Open the project with your favorite text editor.
∙ In order to test your new formula, you can run the command "rit build formula" or
"rit build formula --watch" to have automatic updates when editing your formula.`

var CommonsRepoURL = "https://github.com/kaduartur/ritchie-formulas"

type InitCmd struct {
repo formula.RepositoryAdder
git github.Repositories
}

func NewInitCmd(repo formula.RepositoryAdder, git github.Repositories) *cobra.Command {
o := InitCmd{repo: repo, git: git}

// NewSingleInitCmd creates init command for single edition
func NewSingleInitCmd(ip prompt.InputPassword, pm security.PassphraseManager) *cobra.Command {
o := initSingleCmd{ip, pm}

return newInitCmd(o.runStdin(), o.runPrompt())
}

func newInitCmd(stdinFunc, promptFunc CommandRunnerFunc) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Init rit",
Long: "Initialize rit configuration",
RunE: RunFuncE(stdinFunc, promptFunc),
RunE: o.runPrompt(),
}
cmd.LocalFlags()

return cmd
}

func (o initSingleCmd) runPrompt() CommandRunnerFunc {
func (in InitCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
pass, err := o.Password(MsgPhrase)
if err != nil {
return err
repo := formula.Repo{
Name: "commons",
Url: CommonsRepoURL,
Priority: 0,
}

p := security.Passphrase(pass)
if err := o.Save(p); err != nil {
return err
}
s := spinner.StartNew("Adding the commons repository...")
time.Sleep(time.Second * 2)

return nil
}
}
repoInfo := github.NewRepoInfo(repo.Url, repo.Token)

func (o initSingleCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {

obj := struct {
Passphrase string `json:"passphrase"`
}{}

err := stdin.ReadJson(os.Stdin, &obj)
tag, err := in.git.LatestTag(repoInfo)
if err != nil {
fmt.Println(stdin.MsgInvalidInput)
return err
}

p := security.Passphrase(obj.Passphrase)
if err := o.Save(p); err != nil {
repo.Version = formula.RepoVersion(tag.Name)

if err := in.repo.Add(repo); err != nil {
return err
}

s.Success(prompt.Green("Okay, now you can use rit.\n"))
prompt.Info(UsageMsg)

return nil
}
}
4 changes: 2 additions & 2 deletions pkg/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
)

func TestNewSingleInitCmd(t *testing.T) {
cmd := NewSingleInitCmd(inputPasswordMock{}, passphraseManagerMock{}, repoLoaderMock{})
cmd := NewInitCmd(inputPasswordMock{}, passphraseManagerMock{}, repoLoaderMock{})
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")

if cmd == nil {
t.Errorf("NewSingleInitCmd got %v", cmd)
t.Errorf("NewInitCmd got %v", cmd)
}

if err := cmd.Execute(); err != nil {
Expand Down
Loading

0 comments on commit 9b78166

Please sign in to comment.