-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: use SDK metadata #378
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8073ac3
fix(deps): use consistent Go version in builds and CI
cwaldren-ld c5e03a2
change go version in go.mod to go 1.21
cwaldren-ld 42cb755
feat: use SDK metadata
cwaldren-ld f0c3007
Merge branch 'main' into cw/sc-242846/sdk-metadata
cwaldren-ld 1ad1fd3
update sdk meta module
cwaldren-ld c18a0a6
update to use new sdkmeta package
cwaldren-ld 4287c9e
update examples to use correct SDK IDs
cwaldren-ld 89020fd
go mod tidy
cwaldren-ld 86fb3c5
Use struct field instead of global var to hold SDK details
dbolson c6a883f
Maintainer popularity order of SDKs
dbolson e81f0b8
Check SDK index
dbolson cf51d35
Run go mod tidy
dbolson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,14 @@ package quickstart | |
import ( | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/launchdarkly/ldcli/internal/sdks" | ||
"github.com/launchdarkly/sdk-meta/api/sdkmeta" | ||
"golang.org/x/exp/slices" | ||
|
||
"github.com/charmbracelet/bubbles/help" | ||
"github.com/charmbracelet/bubbles/key" | ||
"github.com/charmbracelet/bubbles/list" | ||
|
@@ -18,23 +24,18 @@ var ( | |
titleBarStyle = lipgloss.NewStyle().MarginBottom(1) | ||
) | ||
|
||
const ( | ||
clientSideSDK = "client" | ||
mobileSDK = "mobile" | ||
serverSideSDK = "server" | ||
) | ||
|
||
type chooseSDKModel struct { | ||
help help.Model | ||
helpKeys keyMap | ||
list list.Model | ||
sdkDetails []sdkDetail | ||
selectedIndex int | ||
selectedSDK sdkDetail | ||
} | ||
|
||
func NewChooseSDKModel(selectedIndex int) tea.Model { | ||
initSDKs() | ||
l := list.New(sdksToItems(), sdkDelegate{}, 30, 9) | ||
sdkDetails := initSDKs() | ||
l := list.New(sdksToItems(sdkDetails), sdkDelegate{}, 30, 9) | ||
l.FilterInput.PromptStyle = lipgloss.NewStyle() | ||
|
||
l.Title = "Select your SDK:" | ||
|
@@ -61,15 +62,90 @@ func NewChooseSDKModel(selectedIndex int) tea.Model { | |
Quit: BindingQuit, | ||
}, | ||
list: l, | ||
sdkDetails: sdkDetails, | ||
selectedIndex: selectedIndex, | ||
} | ||
} | ||
|
||
// initSDKs sets the index of each SDK based on place in list. | ||
func initSDKs() { | ||
for i := range SDKs { | ||
SDKs[i].index = i | ||
// The CLI uses the sdkmeta project to obtain metadata about each SDK, including the display names | ||
// and types (client, server, etc.) | ||
// Currently, there is no sdkmeta for code examples associated with each SDK, so we hard-code the examples here. | ||
// Once they are part of sdkmeta we can remove this list. | ||
var sdkExamples = map[string]string{ | ||
"react-client-sdk": "https://github.com/launchdarkly/react-client-sdk/tree/main/examples/typescript", | ||
"vue": "https://github.com/launchdarkly/vue-client-sdk/tree/main/example", | ||
"react-native": "https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react-native/example", | ||
"cpp-client-sdk": "https://github.com/launchdarkly/cpp-sdks/tree/main/examples/hello-cpp-client", | ||
"cpp-server-sdk": "https://github.com/launchdarkly/cpp-sdks/tree/main/examples/hello-cpp-server", | ||
"lua-server-server": "https://github.com/launchdarkly/lua-server-sdk/tree/main/examples/hello-lua-server", | ||
} | ||
|
||
// sdkOrder is a list of IDs the SDKs in order that they should be rendered based on popularity. | ||
var sdkOrder = []string{ | ||
cwaldren-ld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"react-client-sdk", | ||
"node-server", | ||
"python-server-sdk", | ||
"java-server-sdk", | ||
"dotnet-server-sdk", | ||
"js-client-sdk", | ||
"vue", | ||
"swift-client-sdk", | ||
"go-server-sdk", | ||
"android", | ||
"react-native", | ||
"ruby-server-sdk", | ||
"flutter-client-sdk", | ||
"dotnet-client-sdk", | ||
"erlang-server-sdk", | ||
"rust-server-sdk", | ||
"cpp-client-sdk", | ||
"roku", | ||
"node-client-sdk", | ||
"cpp-server-sdk", | ||
"lua-server-sdk", | ||
"haskell-server-sdk", | ||
"php-server-sdk", | ||
} | ||
|
||
// initSDKs is responsible for loading SDK quickstart instructions from the embedded filesystem. | ||
// | ||
// The names of the files are special: they are the ID of the SDK (e.g. react-native), and are used as an index or | ||
// key to lookup associated sdk metadata (display name, SDK type, etc.) | ||
// | ||
// Therefore, take care when naming the files. A list of valid SDK IDs can be found here: | ||
// https://github.com/launchdarkly/sdk-meta/blob/main/products/names.json | ||
func initSDKs() []sdkDetail { | ||
items, err := sdks.InstructionFiles.ReadDir("sdk_instructions") | ||
if err != nil { | ||
panic("failed to load embedded SDK quickstart instructions: " + err.Error()) | ||
} | ||
|
||
slices.SortFunc(items, func(a fs.DirEntry, b fs.DirEntry) int { | ||
return strings.Compare(a.Name(), b.Name()) | ||
}) | ||
|
||
details := make([]sdkDetail, 0, len(items)) | ||
for _, item := range items { | ||
id, _, _ := strings.Cut(filepath.Base(item.Name()), ".") | ||
if _, ok := sdkmeta.Names[id]; !ok { | ||
continue | ||
} | ||
index := slices.Index(sdkOrder, id) | ||
if index == -1 { | ||
// if we missed an SDK don't add it with an invalid index | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this, there's a panic since it would later try to reference a -1 index. It shouldn't happen unless we change the SDK ID and forget to update the order list. |
||
continue | ||
} | ||
|
||
details = append(details, sdkDetail{ | ||
id: id, | ||
index: index, | ||
displayName: sdkmeta.Names[id], | ||
sdkType: sdkmeta.Types[id], | ||
url: sdkExamples[id], | ||
}) | ||
} | ||
|
||
return details | ||
} | ||
|
||
// Init sends commands when the model is created that will: | ||
|
@@ -116,145 +192,20 @@ func (m chooseSDKModel) View() string { | |
} | ||
|
||
type sdkDetail struct { | ||
canonicalName string | ||
displayName string | ||
index int | ||
kind string | ||
url string // custom URL if it differs from the other SDKs | ||
id string | ||
displayName string | ||
index int | ||
sdkType sdkmeta.Type | ||
url string // custom URL if it differs from the other SDKs | ||
} | ||
|
||
func (s sdkDetail) FilterValue() string { return s.displayName } | ||
|
||
var SDKs = []sdkDetail{ | ||
{ | ||
canonicalName: "react", | ||
displayName: "React", | ||
kind: clientSideSDK, | ||
url: "https://github.com/launchdarkly/react-client-sdk/tree/main/examples/typescript", | ||
}, | ||
{ | ||
canonicalName: "node-server", | ||
displayName: "Node.js (server-side)", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "python", | ||
displayName: "Python", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "java", | ||
displayName: "Java", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "dotnet-server", | ||
displayName: ".NET (server-side)", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "js", | ||
displayName: "JavaScript", | ||
kind: clientSideSDK, | ||
}, | ||
{ | ||
canonicalName: "vue", | ||
displayName: "Vue", | ||
kind: clientSideSDK, | ||
url: "https://github.com/launchdarkly/vue-client-sdk/tree/main/example", | ||
}, | ||
{ | ||
canonicalName: "ios-swift", | ||
displayName: "iOS", | ||
kind: mobileSDK, | ||
}, | ||
{ | ||
canonicalName: "go", | ||
displayName: "Go", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "android", | ||
displayName: "Android", | ||
kind: mobileSDK, | ||
}, | ||
{ | ||
canonicalName: "react-native", | ||
displayName: "React Native", | ||
kind: mobileSDK, | ||
url: "https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react-native/example", | ||
}, | ||
{ | ||
canonicalName: "ruby", | ||
displayName: "Ruby", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "flutter", | ||
displayName: "Flutter", | ||
kind: mobileSDK, | ||
}, | ||
{ | ||
canonicalName: "dotnet-client", | ||
displayName: ".NET (client-side)", | ||
kind: clientSideSDK, | ||
}, | ||
{ | ||
canonicalName: "erlang", | ||
displayName: "Erlang", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "rust", | ||
displayName: "Rust", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "c-client", | ||
displayName: "C/C++ (client-side)", | ||
kind: clientSideSDK, | ||
url: "https://github.com/launchdarkly/cpp-sdks/tree/main/examples/hello-cpp-client", | ||
}, | ||
{ | ||
canonicalName: "roku", | ||
displayName: "Roku", | ||
kind: clientSideSDK, | ||
}, | ||
{ | ||
canonicalName: "node-client", | ||
displayName: "Node.js (client-side)", | ||
kind: clientSideSDK, | ||
}, | ||
{ | ||
canonicalName: "c-server", | ||
displayName: "C/C++ (server-side)", | ||
kind: serverSideSDK, | ||
url: "https://github.com/launchdarkly/cpp-sdks/tree/main/examples/hello-cpp-server", | ||
}, | ||
{ | ||
canonicalName: "lua-server", | ||
displayName: "Lua", | ||
kind: serverSideSDK, | ||
url: "https://github.com/launchdarkly/lua-server-sdk/tree/main/examples/hello-lua-server", | ||
}, | ||
{ | ||
canonicalName: "haskell-server", | ||
displayName: "Haskell", | ||
kind: serverSideSDK, | ||
}, | ||
{ | ||
canonicalName: "php", | ||
displayName: "PHP", | ||
kind: serverSideSDK, | ||
}, | ||
} | ||
|
||
func sdksToItems() []list.Item { | ||
items := make([]list.Item, len(SDKs)) | ||
for i, sdk := range SDKs { | ||
items[i] = list.Item(sdk) | ||
func sdksToItems(sdkDetails []sdkDetail) []list.Item { | ||
items := make([]list.Item, len(sdkDetails)) | ||
for _, info := range sdkDetails { | ||
items[info.index] = list.Item(info) | ||
} | ||
|
||
return items | ||
} | ||
|
||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the PR to not use the global var but pass this list around instead. It felt weird to modify it when we could encapsulate it in the model.