Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion cmd/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func TestHelloCmd(t *testing.T) {
t.Run("with no options", func(t *testing.T) {
expected := `{"hello": "world"}`
actual := new(bytes.Buffer)

rootCmd.SetOut(actual)
rootCmd.SetErr(actual)
rootCmd.SetArgs([]string{"hello"})
Expand Down
51 changes: 51 additions & 0 deletions cmd/projects/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package projects

import (
"context"
"errors"
"fmt"
"ld-cli/internal/projects"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but do we care about imports ordering?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this in another PR.


"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Return a list of projects",
Long: "Return a list of projects",
RunE: runList,
}

cmd.AddCommand()

return cmd
}

func runList(cmd *cobra.Command, args []string) error {
// TODO: handle missing flags
if viper.GetString("accessToken") == "" {
return errors.New("accessToken required")
}
if viper.GetString("baseUri") == "" {
return errors.New("baseUri required")
}

client := projects.NewClient(
viper.GetString("accessToken"),
viper.GetString("baseUri"),
)
response, err := projects.ListProjects(
context.Background(),
client,
)
if err != nil {
return err
}

// TODO: should this return response and let caller output or pass in stdout-ish interface?
fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n")

return nil
}
17 changes: 17 additions & 0 deletions cmd/projects/projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package projects

import (
"github.com/spf13/cobra"
)

func NewProjectsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "projects",
Short: "Make requests (list, create, etc.) on projects",
Long: "Make requests (list, create, etc.) on projects",
}

cmd.AddCommand(NewListCmd())

return cmd
}
30 changes: 18 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"

"ld-cli/cmd/projects"
)

var rootCmd = &cobra.Command{
Expand All @@ -21,31 +23,35 @@ func Execute() {
}

func init() {
var accessToken string
var (
accessToken string
baseURI string
)

rootCmd.PersistentFlags().StringVarP(
&accessToken,
"baseUri",
"u",
"http://localhost:3000",
"LaunchDarkly base URI.",
"accessToken",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetized these.

"t",
"",
"LaunchDarkly personal access token",
)
err := viper.BindPFlag("baseUri", rootCmd.PersistentFlags().Lookup("baseUri"))
err := viper.BindPFlag("accessToken", rootCmd.PersistentFlags().Lookup("accessToken"))
if err != nil {
os.Exit(1)
}
rootCmd.PersistentFlags().StringVarP(
&accessToken,
"accessToken",
"t",
"",
"LaunchDarkly personal access token with write-level access.",
&baseURI,
"baseUri",
"u",
"http://localhost:3000",
"LaunchDarkly base URI",
)
err = viper.BindPFlag("accessToken", rootCmd.PersistentFlags().Lookup("accessToken"))
err = viper.BindPFlag("baseUri", rootCmd.PersistentFlags().Lookup("baseUri"))
if err != nil {
os.Exit(1)
}

rootCmd.AddCommand(newHelloCmd())
rootCmd.AddCommand(projects.NewProjectsCmd())
rootCmd.AddCommand(setupCmd)
}
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.10.0
github.com/launchdarkly/api-client-go/v14 v14.0.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
Expand All @@ -17,6 +18,7 @@ require (
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -43,10 +45,14 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
375 changes: 371 additions & 4 deletions go.sum

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions internal/projects/projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package projects

import (
"context"
"encoding/json"

ldapi "github.com/launchdarkly/api-client-go/v14"
)

type Client interface {
List(context.Context) (*ldapi.Projects, error)
}

type ProjectsClient struct {
client *ldapi.APIClient
}

func NewClient(accessToken string, baseURI string) ProjectsClient {
config := ldapi.NewConfiguration()
config.AddDefaultHeader("Authorization", accessToken)
config.Servers[0].URL = baseURI
client := ldapi.NewAPIClient(config)

return ProjectsClient{
client: client,
}
}

func (c ProjectsClient) List(ctx context.Context) (*ldapi.Projects, error) {
projects, _, err := c.client.ProjectsApi.
GetProjects(ctx).
Limit(2).
Execute()
if err != nil {
// TODO: make this nicer
return nil, err
}

return projects, nil
}

func ListProjects(ctx context.Context, client2 Client) ([]byte, error) {
projects, err := client2.List(ctx)
if err != nil {
// 401 - should return unauthorized type error with body(?)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll follow up with a PR to handle errors.

// 404 - should return not found type error with body
e, ok := err.(ldapi.GenericOpenAPIError)
if ok {
return e.Body(), err
}
return nil, err
}

projectsJSON, err := json.Marshal(projects)
if err != nil {
return nil, err
}

return projectsJSON, nil
}
89 changes: 89 additions & 0 deletions internal/projects/projects_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package projects_test

import (
"context"
"ld-cli/internal/projects"
"testing"

ldapi "github.com/launchdarkly/api-client-go/v14"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func strPtr(s string) *string {
return &s
}

type GetProjectsResponse struct{}

type MockClient struct{}

func (c MockClient) List(ctx context.Context) (*ldapi.Projects, error) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change this once we have better error handling.

totalCount := int32(1)

return &ldapi.Projects{
Links: map[string]ldapi.Link{
"last": {
Href: strPtr("/api/v2/projects?expand=environments&limit=1&offset=1"),
Type: strPtr("application/json"),
},
"next": {
Href: strPtr("/api/v2/projects?expand=environments&limit=1&offset=0"),
Type: strPtr("application/json"),
},
"self": {
Href: strPtr("/api/v2/projects?expand=environments&limit=1"),
Type: strPtr("application/json"),
},
},
Items: []ldapi.Project{
{
Id: "000000000000000000000001",
Key: "test-project",
},
},
TotalCount: &totalCount,
}, nil
}

func TestListProjects(t *testing.T) {
t.Run("returns a paginated list of projects", func(t *testing.T) {
expected := `{
"_links": {
"last": {
"href": "/api/v2/projects?expand=environments&limit=1&offset=1",
"type": "application/json"
},
"next": {
"href": "/api/v2/projects?expand=environments&limit=1&offset=0",
"type": "application/json"
},
"self": {
"href": "/api/v2/projects?expand=environments&limit=1",
"type": "application/json"
}
},
"items": [
{
"_id": "000000000000000000000001",
"_links": null,
"includeInSnippetByDefault": false,
"key": "test-project",
"name": "",
"tags": null
}
],
"totalCount": 1
}`
mockClient := MockClient{}

response, err := projects.ListProjects(context.Background(), mockClient)

require.NoError(t, err)
assert.JSONEq(t, expected, string(response))
})

t.Run("with invalid accessToken is forbidden", func(t *testing.T) {
})
}
3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions vendor/github.com/golang/protobuf/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading