Skip to content

Commit

Permalink
chore: log for init
Browse files Browse the repository at this point in the history
  • Loading branch information
seriouspoop committed Aug 30, 2024
1 parent 65e1a11 commit b546c7e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
26 changes: 17 additions & 9 deletions internal/handler/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

"github.com/MakeNowJust/heredoc/v2"
"github.com/seriouspoop/gopush/svc"
"github.com/seriouspoop/gopush/utils"
"github.com/spf13/cobra"
)

func Init(s servicer) *cobra.Command {
var verbose bool
// TODO -> verbose implementation
// var verbose bool
initCmd := &cobra.Command{
Use: "init",
Short: "initializes git repo with all the config setting",
Expand All @@ -33,14 +35,15 @@ func Init(s servicer) *cobra.Command {
if err != nil {
return err
}
//TODO - migrate to svc methods

err = s.LoadProject()
if err != nil {
if errors.Is(err, svc.ErrRepoNotFound) {
err := s.InitializeRepo()
if err != nil {
return err
}
utils.Logger(utils.LOG_SUCCESS, "repository initialized")
}

// load config for add remote
Expand All @@ -53,43 +56,48 @@ func Init(s servicer) *cobra.Command {
if err != nil {
return err
}
utils.Logger(utils.LOG_SUCCESS, "remote initialized")
}
fmt.Println("✅ Repository and Remote initialized.")

utils.Logger(utils.LOG_INFO, "Generating config file...")
err = s.SetRemoteAuth()
if err != nil {
return err
}
fmt.Println("✅ config file generated.")
utils.Logger(utils.LOG_SUCCESS, "authorization set")

err = s.LoadConfig()
if err != nil {
return err
}
utils.Logger(utils.LOG_SUCCESS, "config file generated")

// staging current files
utils.Logger(utils.LOG_INFO, "Staging changes...")
err = s.StageChanges()
if err != nil {
return err
}
fmt.Println("✅ changes staged.")
fmt.Println("Pulling commits from main...")
utils.Logger(utils.LOG_SUCCESS, "files added")

utils.Logger(utils.LOG_INFO, "Pulling commits from main...")
err = s.Pull(true)
if err != nil {
if errors.Is(err, svc.ErrPullFailed) {
fmt.Println("Remote pull failed, try pulling manually.")
utils.Logger(utils.LOG_INFO, "Remote pull failed, try pulling manually.")
}
return err
}
utils.Logger(utils.LOG_SUCCESS, "pulled changes")
fmt.Println(heredoc.Doc(`
✅ Pulled remote changes
Now you will be able to use "gopush run" command for you workflow.
See "gopush run --help" for more details.`))
return nil
},
}

initCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "detailed output for each step")
// initCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "detailed output for each step")

return initCmd
}
8 changes: 4 additions & 4 deletions repo/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ func (g *Git) Push(remote *model.Remote, branch model.Branch, authType model.Aut
Password: auth.Token,
}
} else if authType == model.AuthSSH {
Auth = &ssh.Password{
User: auth.Username,
Password: auth.Token,
}
// Auth = &ssh.Password{
// User: auth.Username,
// Password: "vmc.Singh@2004",
// }
} else {
return g.err.InvalidAuthMethod
}
Expand Down
23 changes: 8 additions & 15 deletions svc/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *Svc) LoadProject() error {
}

func (s *Svc) InitializeRepo() error {
fmt.Println("Initializing repository...")
utils.Logger(utils.LOG_INFO, "Initializing repository...")
err := s.git.CreateRepo()
if err != nil {
return err
Expand All @@ -42,12 +42,13 @@ func (s *Svc) InitializeRemote() error {
return ErrConfigNotLoaded
}
var remoteURL string
fmt.Println("Adding remote...")
fmt.Print("- Enter Remote URL: ")
remoteURL, err := s.r.ReadString('\n')
utils.Logger(utils.LOG_INFO, "Adding remote...")

remoteURL, err := utils.Prompt("remote url")
if err != nil {
return err
}

remoteURL = strings.TrimSpace(remoteURL)
remote := &model.Remote{
Name: s.cfg.DefaultRemote,
Expand All @@ -57,7 +58,8 @@ func (s *Svc) InitializeRemote() error {
if err != nil {
return err
}
fmt.Println("\U00002714 Remote added.")

utils.Logger(utils.LOG_SUCCESS, "remote added")
return nil
}

Expand Down Expand Up @@ -131,16 +133,7 @@ func generateCommitMsg() (string, error) {
commitType = shortner[commitType]
}

promptTemplate := &promptui.PromptTemplates{
Valid: "{{ . }}: ",
Success: "{{ `\U00002714` | green }} {{ . | faint}}{{ `:` | faint}} ",
}

message := promptui.Prompt{
Label: "Commit Message",
Templates: promptTemplate,
}
msg, err := message.Run()
msg, err := utils.Prompt("commit message")
if err != nil {
return "", err
}
Expand Down
19 changes: 19 additions & 0 deletions utils/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

import (
"github.com/manifoldco/promptui"
)

func Prompt(label string) (res string, err error) {
template := &promptui.PromptTemplates{
Valid: "{{ . }}: ",
Success: "{{ `\U00002714` | green }} {{ . | faint}}{{ `:` | faint}} ",
}
prompt := promptui.Prompt{
Label: label,
Templates: template,
}

res, err = prompt.Run()
return
}

0 comments on commit b546c7e

Please sign in to comment.