| 1 | package gui |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | |
| 7 | "github.com/isacikgoz/gitbatch/internal/command" |
| 8 | "github.com/isacikgoz/gitbatch/internal/git" |
| 9 | "github.com/isacikgoz/gitbatch/internal/job" |
| 10 | "github.com/jroimartin/gocui" |
| 11 | ) |
| 12 | |
| 13 | var ( |
| 14 | // this is required so we can know where we can return |
| 15 | authenticationReturnView string |
| 16 | |
| 17 | // these views used as a label for git repository address and credential views |
| 18 | authenticationViewFeature = viewFeature{Name: "authentication", Title: " Authentication "} |
| 19 | authUserLabelFeature = viewFeature{Name: "authuserlabel", Title: " User: "} |
| 20 | authPswdLabelViewFeature = viewFeature{Name: "authpasswdlabel", Title: " Password: "} |
| 21 | |
| 22 | // these views used as a input for the credentials |
| 23 | authUserFeature = viewFeature{Name: "authuser", Title: " User "} |
| 24 | authPasswordViewFeature = viewFeature{Name: "authpasswd", Title: " Password "} |
| 25 | |
| 26 | // these are the view groups, so that we can assign common keybindings |
| 27 | authViews = []viewFeature{authUserFeature, authPasswordViewFeature} |
| 28 | authLabels = []viewFeature{authenticationViewFeature, authUserLabelFeature, authPswdLabelViewFeature} |
| 29 | |
| 30 | // we can hold the job that is required to authenticate |
| 31 | jobRequiresAuth *job.Job |
| 32 | ) |
| 33 | |
| 34 | // open an auth view to get user credentials |
| 35 | func (gui *Gui) openAuthenticationView(g *gocui.Gui, jobQueue *job.Queue, job *job.Job, returnViewName string) error { |
| 36 | maxX, maxY := g.Size() |
| 37 | // lets add this job since it is removed from the queue |
| 38 | // also it is already unsuccessfully finished |
| 39 | if err := jobQueue.AddJob(job); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | jobRequiresAuth = job |
| 43 | if job.Repository.WorkStatus() != git.Fail { |
| 44 | if err := jobQueue.RemoveFromQueue(job.Repository); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | } |
| 48 | authenticationReturnView = returnViewName |
| 49 | v, err := g.SetView(authenticationViewFeature.Name, maxX/2-30, maxY/2-2, maxX/2+30, maxY/2+2) |
| 50 | if err != nil { |
| 51 | if err != gocui.ErrUnknownView { |
| 52 | return err |
| 53 | } |
| 54 | fmt.Fprintln(v, keySymbol+selectionIndicator+red.Sprint(jobRequiresAuth.Repository.State.Remote.URL[0])) |
| 55 | } |
| 56 | g.Cursor = true |
| 57 | if err := gui.openUserView(g); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | return gui.openPasswordView(g) |
| 61 | } |
| 62 | |
| 63 | // close the opened auth views |
| 64 | func (gui *Gui) closeAuthenticationView(g *gocui.Gui, v *gocui.View) error { |
| 65 | g.Cursor = false |
| 66 | for _, vf := range authLabels { |
| 67 | if err := g.DeleteView(vf.Name); err != nil { |
| 68 | return nil |
| 69 | } |
| 70 | } |
| 71 | for _, vf := range authViews { |
| 72 | if err := g.DeleteView(vf.Name); err != nil { |
| 73 | return nil |
| 74 | } |
| 75 | } |
| 76 | return gui.closeViewCleanup(authenticationReturnView) |
| 77 | } |
| 78 | |
| 79 | // close the opened auth views and submit the credentials |
| 80 | func (gui *Gui) submitAuthenticationView(g *gocui.Gui, v *gocui.View) error { |
| 81 | g.Cursor = false |
| 82 | |
| 83 | // in order to read buffer of the views, first we need to find them |
| 84 | vUser, err := g.View(authUserFeature.Name) |
| 85 | if err != nil { |
| 86 | return err // should return?? |
| 87 | } |
| 88 | |
| 89 | vPswd, err := g.View(authPasswordViewFeature.Name) |
| 90 | if err != nil { |
| 91 | return err // should return?? |
| 92 | } |
| 93 | |
| 94 | // the return string of the views contain trailing new lines |
| 95 | re := regexp.MustCompile(`\r?\n`) |
| 96 | creduser := re.ReplaceAllString(vUser.ViewBuffer(), "") |
| 97 | credpswd := re.ReplaceAllString(vPswd.ViewBuffer(), "") |
| 98 | |
| 99 | // since the git ops require different types of options we better switch |
| 100 | switch mode := jobRequiresAuth.JobType; mode { |
| 101 | case job.FetchJob: |
| 102 | jobRequiresAuth.Options = &command.FetchOptions{ |
| 103 | RemoteName: jobRequiresAuth.Repository.State.Remote.Name, |
| 104 | Credentials: &git.Credentials{ |
| 105 | User: creduser, |
| 106 | Password: credpswd, |
| 107 | }, |
| 108 | } |
| 109 | case job.PullJob: |
| 110 | // we handle pull as fetch&merge so same rule applies |
| 111 | jobRequiresAuth.Options = &command.PullOptions{ |
| 112 | RemoteName: jobRequiresAuth.Repository.State.Remote.Name, |
| 113 | Credentials: &git.Credentials{ |
| 114 | User: creduser, |
| 115 | Password: credpswd, |
| 116 | }, |
| 117 | } |
| 118 | } |
| 119 | jobRequiresAuth.Repository.SetWorkStatus(git.Queued) |
| 120 | |
| 121 | // add this job to the last of the queue |
| 122 | if err := gui.State.Queue.AddJob(jobRequiresAuth); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | |
| 126 | return gui.closeAuthenticationView(g, v) |
| 127 | } |
| 128 | |
| 129 | // open an error view to inform user with a message and a useful note |
| 130 | func (gui *Gui) openUserView(g *gocui.Gui) error { |
| 131 | maxX, maxY := g.Size() |
| 132 | // first, create the label for user |
| 133 | vlabel, err := g.SetView(authUserLabelFeature.Name, maxX/2-30, maxY/2-1, maxX/2-19, maxY/2+1) |
| 134 | if err != nil { |
| 135 | if err != gocui.ErrUnknownView { |
| 136 | return err |
| 137 | } |
| 138 | fmt.Fprintln(vlabel, authUserLabelFeature.Title) |
| 139 | vlabel.Frame = false |
| 140 | } |
| 141 | // second, crete the user input |
| 142 | v, err := g.SetView(authUserFeature.Name, maxX/2-18, maxY/2-1, maxX/2+29, maxY/2+1) |
| 143 | if err != nil { |
| 144 | if err != gocui.ErrUnknownView { |
| 145 | return err |
| 146 | } |
| 147 | v.Title = authUserFeature.Title |
| 148 | v.Editable = true |
| 149 | v.Frame = false |
| 150 | } |
| 151 | return gui.focusToView(authUserFeature.Name) |
| 152 | } |
| 153 | |
| 154 | // open an error view to inform user with a message and a useful note |
| 155 | func (gui *Gui) openPasswordView(g *gocui.Gui) error { |
| 156 | maxX, maxY := g.Size() |
| 157 | // first, create the label for password |
| 158 | vlabel, err := g.SetView(authPswdLabelViewFeature.Name, maxX/2-30, maxY/2, maxX/2-19, maxY/2+2) |
| 159 | if err != nil { |
| 160 | if err != gocui.ErrUnknownView { |
| 161 | return err |
| 162 | } |
| 163 | fmt.Fprintln(vlabel, authPswdLabelViewFeature.Title) |
| 164 | vlabel.Frame = false |
| 165 | } |
| 166 | // second, crete the masked password input |
| 167 | v, err := g.SetView(authPasswordViewFeature.Name, maxX/2-18, maxY/2, maxX/2+29, maxY/2+2) |
| 168 | if err != nil { |
| 169 | if err != gocui.ErrUnknownView { |
| 170 | return err |
| 171 | } |
| 172 | v.Title = authPasswordViewFeature.Title |
| 173 | v.Editable = true |
| 174 | v.Mask ^= '*' |
| 175 | v.Frame = false |
| 176 | } |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | // focus to next view |
| 181 | func (gui *Gui) nextAuthView(g *gocui.Gui, v *gocui.View) error { |
| 182 | err := gui.nextViewOfGroup(g, v, authViews) |
| 183 | return err |
| 184 | } |