Skip to content

Commit

Permalink
Add change ctx namespace command (#1924)
Browse files Browse the repository at this point in the history
* Add change ctx namespace command

Signed-off-by: Javier López Barba <javier@okteto.com>

* Track context use-namespace

Signed-off-by: Javier López Barba <javier@okteto.com>

* Remove get method

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Nov 3, 2021
1 parent 7f763e2 commit ec977dd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ This will prompt you to select one of your existing contexts or to create a new
cmd.AddCommand(CreateCMD())
cmd.AddCommand(UpdateKubeconfigCMD())
cmd.AddCommand(List())
cmd.AddCommand(UseNamespace())
cmd.Flags().StringVarP(&ctxOptions.Token, "token", "t", "", "API token for authentication")
cmd.Flags().StringVarP(&ctxOptions.Namespace, "namespace", "n", "", "namespace of your okteto context")
cmd.Flags().StringVarP(&ctxOptions.Builder, "builder", "b", "", "url of the builder service")
Expand Down
48 changes: 48 additions & 0 deletions cmd/context/use-namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package context

import (
"context"

"github.com/okteto/okteto/cmd/utils"
"github.com/okteto/okteto/pkg/analytics"
"github.com/okteto/okteto/pkg/okteto"
"github.com/spf13/cobra"
)

// Namespace changes your current context namespace.
func UseNamespace() *cobra.Command {
ctxOptions := &ContextOptions{}
cmd := &cobra.Command{
Use: "use-namespace [name]",
Args: utils.ExactArgsAccepted(1, "https://okteto.com/docs/reference/cli/#context"),
Short: "Set the namespace of the current context",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
ctxOptions.Namespace = args[0]
ctxOptions.Context = okteto.Context().Name
ctxOptions.Show = true
err := Run(ctx, ctxOptions)
analytics.TrackContextUseNamespace(err == nil)
if err != nil {
return err
}

return nil
},
}

return cmd
}
13 changes: 6 additions & 7 deletions pkg/analytics/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
execEvent = "Exec"
signupEvent = "Signup"
contextEvent = "Context"
contextUseNamespaceEvent = "Context Use-namespace"
disableEvent = "Disable Analytics"
stackNotSupportedField = "Stack Field Not Supported"
buildPullErrorEvent = "BuildPullError"
Expand Down Expand Up @@ -248,10 +249,6 @@ func TrackDestroyStack(success bool) {

// TrackLogin sends a tracking event to mixpanel when the user logs in
func TrackLogin(success bool) {
if !get().Enabled {
return
}

track(loginEvent, success, nil)
}

Expand All @@ -266,12 +263,14 @@ func TrackSignup(success bool, userID string) {

// TrackContext sends a tracking event to mixpanel when the user use context in
func TrackContext(success bool) {
if !get().Enabled {
return
}
track(contextEvent, success, nil)
}

// TrackContextUseNamespace sends a tracking event to mixpanel when the user use context in
func TrackContextUseNamespace(success bool) {
track(contextUseNamespaceEvent, success, nil)
}

func TrackStackWarnings(warnings []string) {
re := regexp.MustCompile(`\[(.*?)\]`)
for _, warning := range warnings {
Expand Down

0 comments on commit ec977dd

Please sign in to comment.