-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sdk instructions step to quickstart (#91)
Create choose SDK setup step
- Loading branch information
Showing
7 changed files
with
270 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package quickstart | ||
|
||
import tea "github.com/charmbracelet/bubbletea" | ||
|
||
type fetchSDKInstructionsMsg struct { | ||
canonicalName string | ||
flagKey string | ||
name string | ||
} | ||
|
||
// errMsg is sent when there is an error in one of the steps that the container model needs to | ||
// know about. | ||
type errMsg struct { | ||
err error | ||
} | ||
|
||
func sendErr(err error) tea.Cmd { | ||
return func() tea.Msg { | ||
return errMsg{err: err} | ||
} | ||
} | ||
|
||
// noInstructionsMsg is sent when we can't find the SDK instructions repository for the given SDK. | ||
type noInstructionsMsg struct{} | ||
|
||
func sendNoInstructions() tea.Cmd { | ||
return func() tea.Msg { | ||
return noInstructionsMsg{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package quickstart | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"ldcli/internal/sdks" | ||
"net/http" | ||
"time" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/glamour" | ||
"github.com/charmbracelet/lipgloss" | ||
"github.com/muesli/reflow/wordwrap" | ||
) | ||
|
||
const instructionsURL = "https://raw.githubusercontent.com/launchdarkly/hello-%s/main/README.md" | ||
|
||
type showSDKInstructionsModel struct { | ||
instructions string | ||
sdk string | ||
} | ||
|
||
func NewShowSDKInstructionsModel() tea.Model { | ||
return showSDKInstructionsModel{} | ||
} | ||
|
||
func (m showSDKInstructionsModel) Init() tea.Cmd { | ||
// send command to make request? | ||
return nil | ||
} | ||
|
||
func (m showSDKInstructionsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case fetchSDKInstructionsMsg: | ||
url := fmt.Sprintf(instructionsURL, msg.canonicalName) | ||
c := &http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return m, sendErr(err) | ||
} | ||
resp, err := c.Do(req) | ||
if err != nil { | ||
return m, sendErr(err) | ||
} | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return m, sendErr(err) | ||
} | ||
|
||
if resp.StatusCode == 404 { | ||
m.sdk = msg.name | ||
|
||
return m, sendNoInstructions() | ||
} | ||
|
||
m.sdk = msg.name | ||
m.instructions = sdks.ReplaceFlagKey(string(body), msg.flagKey) | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (m showSDKInstructionsModel) View() string { | ||
style := lipgloss.NewStyle().Border(lipgloss.NormalBorder(), true, false) | ||
md, err := m.renderMarkdown() | ||
if err != nil { | ||
return fmt.Sprintf("error rendering instructions: %s", err) | ||
} | ||
|
||
return wordwrap.String( | ||
fmt.Sprintf( | ||
"Set up your application. Here are the steps to incorporate the LaunchDarkly %s SDK into your code.\n%s", | ||
m.sdk, | ||
style.Render(md), | ||
), | ||
0, | ||
) | ||
} | ||
|
||
func (m showSDKInstructionsModel) renderMarkdown() (string, error) { | ||
out, err := glamour.Render(m.instructions, "auto") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package sdks | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// ReplaceFlagKey changes the placeholder flag key in the SDK instructions to the flag key from | ||
// the user. | ||
func ReplaceFlagKey(instructions string, key string) string { | ||
r := strings.NewReplacer( | ||
"my-flag-key", | ||
key, | ||
"my-flag", | ||
key, | ||
"my-boolean-flag", | ||
key, | ||
"FLAG_KEY", | ||
key, | ||
"<flag key>", | ||
key, | ||
) | ||
|
||
return r.Replace(instructions) | ||
} |
Oops, something went wrong.