-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add flags create command (#56)
Add flags create command
- Loading branch information
Showing
9 changed files
with
172 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package flags | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"ld-cli/internal/errors" | ||
"ld-cli/internal/flags" | ||
) | ||
|
||
func NewCreateCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a new flag", | ||
Long: "Create a new flag", | ||
PreRunE: validate, | ||
RunE: runCreate, | ||
} | ||
|
||
cmd.Flags().StringP("data", "d", "", "Input data in JSON") | ||
err := cmd.MarkFlagRequired("data") | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = viper.BindPFlag("data", cmd.Flags().Lookup("data")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cmd.Flags().String("projKey", "", "Project key") | ||
err = cmd.MarkFlagRequired("projKey") | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = viper.BindPFlag("projKey", cmd.Flags().Lookup("projKey")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
type inputData struct { | ||
Name string `json:"name"` | ||
Key string `json:"key"` | ||
} | ||
|
||
func runCreate(cmd *cobra.Command, args []string) error { | ||
client := flags.NewClient( | ||
viper.GetString("accessToken"), | ||
viper.GetString("baseUri"), | ||
) | ||
|
||
var data inputData | ||
err := json.Unmarshal([]byte(cmd.Flags().Lookup("data").Value.String()), &data) | ||
// err := json.Unmarshal([]byte(viper.GetString("data")), &data) | ||
if err != nil { | ||
return err | ||
} | ||
projKey := viper.GetString("projKey") | ||
|
||
response, err := client.Create( | ||
context.Background(), | ||
data.Name, | ||
data.Key, | ||
projKey, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n") | ||
|
||
return nil | ||
} | ||
|
||
// validate ensures the flags are valid before using them. | ||
// TODO: refactor with projects validate(). | ||
func validate(cmd *cobra.Command, args []string) error { | ||
_, err := url.ParseRequestURI(viper.GetString("baseUri")) | ||
if err != nil { | ||
return errors.ErrInvalidBaseURI | ||
} | ||
|
||
return 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,15 @@ | ||
package flags | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func NewFlagsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "flags", | ||
Short: "Make requests (list, create, etc.) on flags", | ||
Long: "Make requests (list, create, etc.) on flags", | ||
} | ||
|
||
cmd.AddCommand(NewCreateCmd()) | ||
|
||
return cmd | ||
} |
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
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,55 @@ | ||
package flags | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
ldapi "github.com/launchdarkly/api-client-go/v14" | ||
|
||
"ld-cli/internal/errors" | ||
) | ||
|
||
type Client interface { | ||
Create(ctx context.Context, name string, key string, projectKey string) ([]byte, error) | ||
} | ||
|
||
type FlagsClient struct { | ||
client *ldapi.APIClient | ||
} | ||
|
||
func NewClient(accessToken string, baseURI string) FlagsClient { | ||
config := ldapi.NewConfiguration() | ||
config.AddDefaultHeader("Authorization", accessToken) | ||
config.Servers[0].URL = baseURI | ||
client := ldapi.NewAPIClient(config) | ||
|
||
return FlagsClient{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (c FlagsClient) Create( | ||
ctx context.Context, | ||
name string, | ||
key string, | ||
projectKey string, | ||
) ([]byte, error) { | ||
post := ldapi.NewFeatureFlagBody(name, key) | ||
flag, _, err := c.client.FeatureFlagsApi.PostFeatureFlag(ctx, projectKey).FeatureFlagBody(*post).Execute() | ||
if err != nil { | ||
switch err.Error() { | ||
case "401 Unauthorized": | ||
return nil, errors.ErrUnauthorized | ||
case "403 Forbidden": | ||
return nil, errors.ErrForbidden | ||
default: | ||
return nil, err | ||
} | ||
} | ||
responseJSON, err := json.Marshal(flag) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return responseJSON, 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
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