-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathflag_toggle.go
More file actions
92 lines (75 loc) · 1.89 KB
/
Copy pathflag_toggle.go
File metadata and controls
92 lines (75 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package setup
import (
"bytes"
"fmt"
"net/http"
"time"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type flagToggleModel struct {
enabled bool
flagKey string
logType string
//err error
}
func NewFlagToggle() flagToggleModel {
return flagToggleModel{}
}
func (m flagToggleModel) Init() tea.Cmd {
return nil
}
func (m flagToggleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Toggle):
m.enabled = !m.enabled
// uncomment to send PATCH
//m.err = m.toggleFlag()
}
}
return m, nil
}
func (m flagToggleModel) View() string {
var furtherInstructions string
title := "Toggle your feature flag (press tab)"
toggle := "OFF"
bgColor := "#646a73"
margin := 1
if m.enabled {
bgColor = "#3d9c51"
furtherInstructions = fmt.Sprintf("\n\nCheck your %s to see the change!", m.logType)
margin = 2
toggle = "ON"
}
toggleStyle := lipgloss.NewStyle().
Background(lipgloss.Color(bgColor)).
Padding(0, 1).
MarginRight(margin)
return title + "\n\n" + toggleStyle.Render(toggle) + m.flagKey + furtherInstructions
}
func (m flagToggleModel) toggleFlag() error { //nolint:unused
url := fmt.Sprintf("http://localhost/api/v2/flags/default/%s", m.flagKey)
c := &http.Client{
Timeout: 10 * time.Second,
}
toggleInstruction := "turnFlagOn"
if !m.enabled {
toggleInstruction = "turnFlagOff"
}
body := fmt.Sprintf(`{
"environmentKey": "test",
"instructions": [ { "kind": %q } ]
}`, toggleInstruction)
req, _ := http.NewRequest("PATCH", url, bytes.NewBufferString(body))
req.Header.Add("Authorization", apiToken) // add token here
req.Header.Add("Content-type", "application/json; domain-model=launchdarkly.semanticpatch")
res, err := c.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}