oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package setup
2
3import (
4 "context"
5 "crypto/rand"
6 "net/http"
7 "strconv"
8
9 "amadan.net/oficina/equipo/internal/equipo/library"
10 "amadan.net/oficina/equipo/internal/release"
11 "amadan.net/oficina/equipo/internal/taskplan"
12 "github.com/go-chi/chi/v5"
13)
14
15type OnboardingOperations interface {
16 ListSpecialists(context.Context, Actor) ([]library.Specialist, error)
17 RequestAccountSetup(context.Context, Actor, string, string, string, Mutation) (AccountSetup, error)
18 AccountSetups(context.Context, Actor) ([]AccountSetup, error)
19 AccountRunners(context.Context, Actor) ([]Runner, error)
20 CancelAccountSetup(context.Context, Actor, string, Mutation) error
21 StartOnboardingTask(context.Context, Actor, int64, Mutation) (taskplan.Plan, error)
22 OnboardingProjects(context.Context, Actor) ([]OnboardingProject, error)
23 Onboarding(context.Context, Actor) (OnboardingState, error)
24 AdvanceOnboarding(context.Context, Actor, string, Mutation) (OnboardingState, error)
25 ChooseOnboardingProvider(context.Context, Actor, string, Mutation) (OnboardingState, error)
26}
27type OnboardingView struct {
28 Specialists []library.Specialist
29 Accounts []AccountSetup
30 Runners []Runner
31 Projects []OnboardingProject
32 State OnboardingState
33 RequestID, InstallCommand string
34}
35type OnboardingHandler struct {
36 InstallAvailable func() bool
37 Store OnboardingOperations
38 Actor func(*http.Request) (Actor, error)
39 Render func(http.ResponseWriter, *http.Request, OnboardingView)
40 Failure func(http.ResponseWriter, *http.Request, error)
41 Invalid error
42 Origin, Version, InstanceID string
43 Manifest release.Manifest
44}
45
46func (h *OnboardingHandler) Routes(r chi.Router) {
47 r.Get("/onboarding", h.Page)
48 r.Post("/onboarding/accounts", h.Account)
49 r.Post("/onboarding/accounts/cancel", h.CancelAccount)
50 r.Post("/onboarding/first-task", h.FirstTask)
51 r.Post("/onboarding/advance", h.Advance)
52 r.Post("/onboarding/provider", h.Provider)
53}
54func (h *OnboardingHandler) Page(w http.ResponseWriter, r *http.Request) {
55 privacy(w)
56 a, e := h.Actor(r)
57 if e != nil {
58 h.Failure(w, r, e)
59 return
60 }
61 state, e := h.Store.Onboarding(r.Context(), a)
62 if e != nil {
63 h.Failure(w, r, e)
64 return
65 }
66 v := OnboardingView{State: state, RequestID: rand.Text()}
67 if state.CurrentStep == "specialists" {
68 v.Specialists, e = h.Store.ListSpecialists(r.Context(), a)
69 if e != nil {
70 h.Failure(w, r, e)
71 return
72 }
73 }
74 v.Accounts, e = h.Store.AccountSetups(r.Context(), a)
75 if e != nil {
76 h.Failure(w, r, e)
77 return
78 }
79 v.Runners, e = h.Store.AccountRunners(r.Context(), a)
80 if e != nil {
81 h.Failure(w, r, e)
82 return
83 }
84 v.Projects, e = h.Store.OnboardingProjects(r.Context(), a)
85 if e != nil {
86 h.Failure(w, r, e)
87 return
88 }
89 if h.Manifest.Installable() && h.InstallAvailable != nil && h.InstallAvailable() {
90 v.InstallCommand, _ = release.InstallCommand(h.Origin, h.Version, h.InstanceID)
91 }
92 h.Render(w, r, v)
93}
94func (h *OnboardingHandler) change(w http.ResponseWriter, r *http.Request, provider bool) {
95 privacy(w)
96 a, e := h.Actor(r)
97 if e != nil {
98 h.Failure(w, r, e)
99 return
100 }
101 r.Body = http.MaxBytesReader(w, r.Body, 4096)
102 if r.ParseForm() != nil {
103 h.Failure(w, r, h.Invalid)
104 return
105 }
106 for k, v := range r.PostForm {
107 if len(v) != 1 || (k != "revision" && k != "request_id" && k != "step" && k != "provider") {
108 h.Failure(w, r, h.Invalid)
109 return
110 }
111 }
112 rev, e := strconv.ParseInt(r.PostForm.Get("revision"), 10, 64)
113 if e != nil {
114 h.Failure(w, r, h.Invalid)
115 return
116 }
117 m := Mutation{Revision: rev, RequestID: r.PostForm.Get("request_id")}
118 if provider {
119 _, e = h.Store.ChooseOnboardingProvider(r.Context(), a, r.PostForm.Get("provider"), m)
120 } else {
121 _, e = h.Store.AdvanceOnboarding(r.Context(), a, r.PostForm.Get("step"), m)
122 }
123 if e != nil {
124 h.Failure(w, r, e)
125 return
126 }
127 http.Redirect(w, r, "/onboarding", http.StatusSeeOther)
128}
129func (h *OnboardingHandler) Advance(w http.ResponseWriter, r *http.Request) { h.change(w, r, false) }
130func (h *OnboardingHandler) Provider(w http.ResponseWriter, r *http.Request) { h.change(w, r, true) }
131