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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/context/apply_organisation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package context

import "github.com/thalassa-cloud/cli/internal/config/contextstate"

// applyOrganisationToContext sets the organisation on ctx. When the organisation
// changes to a non-empty value, the project is cleared because projects are
// organisation-scoped. Returns the previous project when it was cleared.
func applyOrganisationToContext(ctx *contextstate.Context, organisation string) (clearedProject string) {
if ctx == nil || organisation == "" {
return ""
}
if ctx.Organisation == organisation {
return ""
}
clearedProject = ctx.Project
ctx.Organisation = organisation
ctx.Project = ""
return clearedProject
}
100 changes: 100 additions & 0 deletions cmd/context/apply_organisation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package context

import (
"testing"

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

"github.com/thalassa-cloud/cli/internal/config/contextstate"
)

func TestApplyOrganisationToContext(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctx *contextstate.Context
organisation string
wantClearedProject string
wantOrganisation string
wantProject string
}{
{
name: "same organisation keeps project",
ctx: &contextstate.Context{
Organisation: "acme",
Project: "proj-1",
},
organisation: "acme",
wantClearedProject: "",
wantOrganisation: "acme",
wantProject: "proj-1",
},
{
name: "different organisation clears project",
ctx: &contextstate.Context{
Organisation: "acme",
Project: "proj-1",
},
organisation: "other",
wantClearedProject: "proj-1",
wantOrganisation: "other",
wantProject: "",
},
{
name: "different organisation with empty project",
ctx: &contextstate.Context{
Organisation: "acme",
Project: "",
},
organisation: "other",
wantClearedProject: "",
wantOrganisation: "other",
wantProject: "",
},
{
name: "empty organisation leaves context unchanged",
ctx: &contextstate.Context{
Organisation: "acme",
Project: "proj-1",
},
organisation: "",
wantClearedProject: "",
wantOrganisation: "acme",
wantProject: "proj-1",
},
{
name: "nil context is a no-op",
ctx: nil,
organisation: "other",
wantClearedProject: "",
},
{
name: "sets organisation when previously empty",
ctx: &contextstate.Context{
Organisation: "",
Project: "proj-1",
},
organisation: "acme",
wantClearedProject: "proj-1",
wantOrganisation: "acme",
wantProject: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

cleared := applyOrganisationToContext(tt.ctx, tt.organisation)
assert.Equal(t, tt.wantClearedProject, cleared)
if tt.ctx == nil {
return
}
require.NotNil(t, tt.ctx)
assert.Equal(t, tt.wantOrganisation, tt.ctx.Organisation)
assert.Equal(t, tt.wantProject, tt.ctx.Project)
})
}
}
2 changes: 1 addition & 1 deletion cmd/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var createCmd = &cobra.Command{
if err != nil {
return err
}
currentContext.Organisation = organisation
applyOrganisationToContext(&currentContext, organisation)
err = contextstate.CombineConfigContext(currentContext)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/context/organisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var organisationCmd = &cobra.Command{
Aliases: []string{"set-organisation", "use-organisation", "organisation", "org"},
Args: cobra.RangeArgs(0, 1),
Short: "Set the organisation in the current-context",
Long: "Set the organisation in the current-context",
Long: "Set the organisation in the current-context. Changing the organisation clears the project.",
Example: "tcloud context use-organisation <organisation>",
RunE: func(cmd *cobra.Command, args []string) error {
currentContext, err := contextstate.GetContextConfiguration()
Expand All @@ -27,7 +27,7 @@ var organisationCmd = &cobra.Command{
if err != nil {
return err
}
currentContext.Organisation = selectedOrganisation
applyOrganisationToContext(&currentContext, selectedOrganisation)
err = contextstate.CombineConfigContext(currentContext)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion docs/tcloud/context/organisation/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Set the organisation in the current-context

### Synopsis

Set the organisation in the current-context
Set the organisation in the current-context.

```
tcloud context organisation <organisation> [flags]
Expand Down
Loading