Skip to content

Commit

Permalink
Merge pull request #577 from sttts/sttts-ctx-unify-path-string
Browse files Browse the repository at this point in the history
ctx: unify path string
  • Loading branch information
sttts authored Jul 11, 2024
2 parents fd87b65 + 6faaf8e commit ebbe7c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
6 changes: 3 additions & 3 deletions cmd/up/ctx/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *Space) Accept(upCtx *upbound.Context, navCtx *navContext) (msg string,
return "", err
}

return fmt.Sprintf(contextSwitchedFmt, s.Breadcrumbs()), nil
return fmt.Sprintf(contextSwitchedFmt, withUpboundPrefix(s.Breadcrumbs())), nil
}

// Accept upserts the "upbound" kubeconfig context and cluster to the chosen
Expand All @@ -59,7 +59,7 @@ func (g *Group) Accept(upCtx *upbound.Context, navCtx *navContext) (msg string,
return "", err
}

return fmt.Sprintf(contextSwitchedFmt, g.Breadcrumbs()), nil
return fmt.Sprintf(contextSwitchedFmt, withUpboundPrefix(g.Breadcrumbs())), nil
}

// Accept upserts a controlplane context and cluster to the chosen kubeconfig.
Expand All @@ -76,5 +76,5 @@ func (ctp *ControlPlane) Accept(upCtx *upbound.Context, navCtx *navContext) (msg
return "", err
}

return fmt.Sprintf(contextSwitchedFmt, ctp.Breadcrumbs()), nil
return fmt.Sprintf(contextSwitchedFmt, withUpboundPrefix(ctp.Breadcrumbs())), nil
}
33 changes: 16 additions & 17 deletions cmd/up/ctx/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *Cmd) Run(ctx context.Context, kongCtx *kong.Context, upCtx *upbound.Con
case "":
return c.RunInteractive(ctx, kongCtx, upCtx, navCtx, initialState)
default:
return c.RunRelative(ctx, upCtx, navCtx, initialState)
return c.RunNonInteractive(ctx, upCtx, navCtx, initialState)
}
}

Expand Down Expand Up @@ -168,10 +168,18 @@ func (c *Cmd) RunSwap(ctx context.Context, upCtx *upbound.Context, navCtx *navCo
if err := writeLastContext(oldContext); err != nil {
return err
}
fmt.Printf(contextSwitchedFmt, state.Breadcrumbs())
if c.Short {
fmt.Println(state.Breadcrumbs())
} else {
fmt.Printf(contextSwitchedFmt, withUpboundPrefix(state.Breadcrumbs()))
}
return nil
}

func withUpboundPrefix(s string) string {
return fmt.Sprintf("%s %s", upboundRootStyle.Render("Upbound"), s)
}

func activateContext(conf *clientcmdapi.Config, sourceContext, preferredContext string) (newConf *clientcmdapi.Config, newLastContext string, err error) { // nolint:gocyclo // little long, but well tested
// switch to non-upbound last context trivially via CurrentContext e.g.
// - upbound <-> other
Expand Down Expand Up @@ -258,7 +266,7 @@ func activateContext(conf *clientcmdapi.Config, sourceContext, preferredContext
return conf, newLastContext, nil
}

func (c *Cmd) RunRelative(ctx context.Context, upCtx *upbound.Context, navCtx *navContext, initialState NavigationState) error { // nolint:gocyclo // a bit long but ¯\_(ツ)_/¯
func (c *Cmd) RunNonInteractive(ctx context.Context, upCtx *upbound.Context, navCtx *navContext, initialState NavigationState) error { // nolint:gocyclo // a bit long but ¯\_(ツ)_/¯
// begin from root unless we're starting from a relative . or ..
state := initialState
if !strings.HasPrefix(c.Argument, ".") {
Expand Down Expand Up @@ -310,7 +318,7 @@ func (c *Cmd) RunRelative(ctx context.Context, upCtx *upbound.Context, navCtx *n
}

// final step if we moved: accept the state
msg := fmt.Sprintf("Kubeconfig context %q: %s\n", c.KubeContext, m.state.Breadcrumbs())
msg := fmt.Sprintf("Kubeconfig context %q: %s\n", c.KubeContext, withUpboundPrefix(m.state.Breadcrumbs()))
if m.state.Breadcrumbs() != initialState.Breadcrumbs() {
accepting, ok := m.state.(Accepting)
if !ok {
Expand All @@ -324,19 +332,10 @@ func (c *Cmd) RunRelative(ctx context.Context, upCtx *upbound.Context, navCtx *n
}

// don't print anything else or we are going to pollute stdout
if c.File != "-" {
if c.Short {
switch state := m.state.(type) {
case *Space:
fmt.Printf("%s/%s\n", state.Org.Name, state.Name)
case *Group:
fmt.Printf("%s/%s/%s\n", state.Space.Org.Name, state.Space.Name, state.Name)
case *ControlPlane:
fmt.Printf("%s/%s/%s/%s\n", state.Group.Space.Org.Name, state.Group.Space.Name, state.NamespacedName().Namespace, state.NamespacedName().Name)
}
} else {
fmt.Print(msg)
}
if c.Short {
fmt.Println(state.Breadcrumbs())
} else {
fmt.Print(msg)
}

return nil
Expand Down
10 changes: 3 additions & 7 deletions cmd/up/ctx/navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,8 @@ func (r *Root) Items(ctx context.Context, upCtx *upbound.Context, navCtx *navCon
}), nil
}

func (r *Root) breadcrumbs() string {
return upboundRootStyle.Render("Upbound")
}

func (r *Root) Breadcrumbs() string {
return r.breadcrumbs()
return ""
}

type Disconnected struct{}
Expand Down Expand Up @@ -241,7 +237,7 @@ func spaceItemFromKubeContext(ctx context.Context, kubeconfig clientcmdapi.Confi
}

func (d *Disconnected) breadcrumbs(styles breadcrumbStyle) string {
return upboundRootStyle.Render("Upbound ") + styles.currentLevel.Render("disconnected/")
return styles.currentLevel.Render("disconnected/")
}

func (d *Disconnected) Breadcrumbs() string {
Expand Down Expand Up @@ -374,7 +370,7 @@ func (o *Organization) BackLabel() string {
}

func (o *Organization) breadcrumbs(styles breadcrumbStyle) string {
return upboundRootStyle.Render("Upbound ") + styles.currentLevel.Render(fmt.Sprintf("%s/", o.Name))
return styles.currentLevel.Render(fmt.Sprintf("%s/", o.Name))
}

func (o *Organization) Breadcrumbs() string {
Expand Down

0 comments on commit ebbe7c2

Please sign in to comment.