Skip to content

Commit

Permalink
Merge pull request #39 from shipyard/look-up-group-properly
Browse files Browse the repository at this point in the history
Defer looking up an org using a function for the main client
  • Loading branch information
maxsokolovsky authored Feb 23, 2024
2 parents 16c6dd9 + 05d908e commit 9c1126d
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 35 deletions.
4 changes: 2 additions & 2 deletions commands/env/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func newCancelEnvironmentCmd(c client.Client) *cobra.Command {

func cancelEnvironmentByID(c client.Client, id string) error {
params := make(map[string]string)
if c.Org != "" {
params["org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}
_, err := c.Requester.Do(http.MethodPost, uri.CreateResourceURI("cancel", "environment", id, "", params), "application/json", nil)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions commands/env/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func handleGetAllEnvironments(c client.Client) error {
if pageSize := viper.GetInt("page-size"); pageSize != 0 {
params["page_size"] = strconv.Itoa(pageSize)
}
if org := viper.GetString("org"); org != "" {
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

Expand Down Expand Up @@ -161,8 +161,8 @@ func handleGetAllEnvironments(c client.Client) error {

func handleGetEnvironmentByID(c client.Client, id string) error {
params := make(map[string]string)
if c.Org != "" {
params["org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

body, err := c.Requester.Do(http.MethodGet, uri.CreateResourceURI("", "environment", id, "", params), "application/json", nil)
Expand Down
4 changes: 2 additions & 2 deletions commands/env/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Rebuild will automatically fetch the latest commit for the branch/PR.`,

func rebuildEnvironmentByID(c client.Client, id string) error {
params := make(map[string]string)
if c.Org != "" {
params["org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

_, err := c.Requester.Do(http.MethodPost, uri.CreateResourceURI("rebuild", "environment", id, "", params), "application/json", nil)
Expand Down
4 changes: 2 additions & 2 deletions commands/env/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func newRestartEnvironmentCmd(c client.Client) *cobra.Command {

func restartEnvironmentByID(c client.Client, id string) error {
params := make(map[string]string)
if c.Org != "" {
params["org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

_, err := c.Requester.Do(http.MethodPost, uri.CreateResourceURI("restart", "environment", id, "", params), "application/json", nil)
Expand Down
4 changes: 2 additions & 2 deletions commands/env/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func newReviveEnvironmentCmd(c client.Client) *cobra.Command {

func reviveEnvironmentByID(c client.Client, id string) error {
params := make(map[string]string)
if c.Org != "" {
params["org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

_, err := c.Requester.Do(http.MethodPost, uri.CreateResourceURI("revive", "environment", id, "", params), "application/json", nil)
Expand Down
4 changes: 2 additions & 2 deletions commands/env/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func newStopEnvironmentCmd(c client.Client) *cobra.Command {

func stopEnvironmentByID(c client.Client, id string) error {
params := make(map[string]string)
if c.Org != "" {
params["org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

_, err := c.Requester.Do(http.MethodPost, uri.CreateResourceURI("stop", "environment", id, "", params), "application/json", nil)
Expand Down
5 changes: 4 additions & 1 deletion commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func init() {

func setupCommands() {
requester := requests.New()
c := client.New(requester, viper.GetString("org"))
orgLookupFn := func() string {
return viper.GetString("org")
}
c := client.New(requester, orgLookupFn)
rootCmd.AddCommand(NewLoginCmd())
rootCmd.AddCommand(NewGetCmd(c))
rootCmd.AddCommand(NewSetCmd())
Expand Down
4 changes: 2 additions & 2 deletions commands/volumes/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func NewCreateSnapshotCmd(c client.Client) *cobra.Command {
func handleCreateSnapshotCmd(c client.Client) error {
envID := viper.GetString("env")
params := make(map[string]string)
if c.Org != "" {
params["Org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}
body := map[string]any{
"note": viper.GetString("note"),
Expand Down
4 changes: 2 additions & 2 deletions commands/volumes/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func handleResetVolumeCmd(c client.Client) error {
envID := viper.GetString("env")
volume := viper.GetString("volume")
params := make(map[string]string)
if c.Org != "" {
params["Org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

subresource := fmt.Sprintf("volume/%s/volume-reset", volume)
Expand Down
4 changes: 2 additions & 2 deletions commands/volumes/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewGetVolumeSnapshotsCmd(c client.Client) *cobra.Command {

func handleGetVolumeSnapshotsCmd(c client.Client) error {
params := make(map[string]string)
if org := viper.GetString("org"); org != "" {
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}
if page := viper.GetInt("page"); page != 0 {
Expand Down Expand Up @@ -121,7 +121,7 @@ func NewLoadVolumeSnapshotCmd(c client.Client) *cobra.Command {

func handleLoadVolumeSnapshotCmd(c client.Client) error {
params := make(map[string]string)
if org := viper.GetString("org"); org != "" {
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}
id := viper.GetString("env")
Expand Down
4 changes: 2 additions & 2 deletions commands/volumes/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func handleUploadVolumeCmd(c client.Client) error {
envID := viper.GetString("env")
volume := viper.GetString("volume")
params := make(map[string]string)
if c.Org != "" {
params["Org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

path := viper.GetString("path")
Expand Down
4 changes: 2 additions & 2 deletions commands/volumes/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func NewGetVolumesCmd(c client.Client) *cobra.Command {
func handleGetVolumesCmd(c client.Client) error {
id := viper.GetString("env")
params := make(map[string]string)
if c.Org != "" {
params["Org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

body, err := c.Requester.Do(http.MethodGet, uri.CreateResourceURI("", "environment", id, "volumes", params), "application/json", nil)
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package client
import "github.com/shipyard/shipyard-cli/pkg/requests"

type Client struct {
Requester requests.Requester
Org string
Requester requests.Requester
OrgLookupFn func() string
}

func New(r requests.Requester, org string) Client {
return Client{Requester: r, Org: org}
func New(r requests.Requester, orgLookupFn func() string) Client {
return Client{Requester: r, OrgLookupFn: orgLookupFn}
}
2 changes: 1 addition & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func setup() (client Client, cleanup func()) {
server := httptest.NewServer(handler)
_ = os.Setenv("SHIPYARD_BUILD_URL", server.URL)
viper.Set("API_TOKEN", "fake-token")
c := New(requests.New(), "")
c := New(requests.New(), func() string { return "" })
return c, func() {
defer server.Close()
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func (c Client) EnvByID(id string) (*types.Response, error) {
}

params := make(map[string]string)
if c.Org != "" {
params["Org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

body, err := c.Requester.Do(http.MethodGet, uri.CreateResourceURI("", "environment", id, "", params), "application/json", nil)
Expand All @@ -31,8 +31,8 @@ func (c Client) EnvByID(id string) (*types.Response, error) {
// AllEnvironmentUUIDs tries to fetch all environment by UUIDs in an org.
func (c Client) AllEnvironmentUUIDs() (*types.UUIDResponse, error) {
params := make(map[string]string)
if c.Org != "" {
params["Org"] = c.Org
if org := c.OrgLookupFn(); org != "" {
params["org"] = org
}

body, err := c.Requester.Do(http.MethodGet, uri.CreateResourceURI("", "environment/uuid", "", "", params), "application/json", nil)
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8s/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (c *Service) setupKubeconfig(envID string) error {
// fetchKubeconfig tries to fetch the Kubeconfig from the backend API.
func (c *Service) fetchKubeconfig(envID string) ([]byte, error) {
params := make(map[string]string)
if c.client.Org != "" {
params["org"] = c.client.Org
if org := c.client.OrgLookupFn(); org != "" {
params["org"] = org
}

requestURI := uri.CreateResourceURI("", "environment", envID, "kubeconfig", params)
Expand Down

0 comments on commit 9c1126d

Please sign in to comment.