1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
package stackswitch
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/spf13/cobra"
"gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/pkg/git"
"gitlab.com/gitlab-org/cli/pkg/text"
)
func NewCmdStackSwitch(f *cmdutils.Factory) *cobra.Command {
stackSwitchCmd := &cobra.Command{
Use: "switch <stack-name>",
Short: "Switch between stacks. (EXPERIMENTAL.)",
Long: heredoc.Doc(
"Switch between stacks to work on another stack created with \"glab stack create\".\n" +
"To see the list of all stacks, check the `.git/stacked/` directory.\n" +
text.ExperimentalString,
),
Example: "glab stack switch <stack-name>",
RunE: func(cmd *cobra.Command, args []string) error {
if err := switchFunc(f, args[0]); err != nil {
return fmt.Errorf("switching stacks failed: %w", err)
}
return nil
},
Args: cobra.ExactArgs(1),
}
return stackSwitchCmd
}
func switchFunc(f *cmdutils.Factory, name string) error {
currentStackTitle, err := git.GetCurrentStackTitle()
if err != nil {
return fmt.Errorf("error getting current stack: %v", err)
}
if currentStackTitle == name {
// No need to switch, we're already on the right stack
return nil
}
stacks, err := git.GetStacks()
if err != nil {
return fmt.Errorf("getting stacks: %v", err)
}
var foundStack *git.Stack
for _, s := range stacks {
if s.Title == name {
foundStack = &s
break
}
}
if foundStack == nil {
return fmt.Errorf("no stack named %q found", name)
}
err = git.SetLocalConfig("glab.currentstack", name)
if err != nil {
return fmt.Errorf("error setting local Git config: %w", err)
}
fmt.Fprintf(f.IO.StdOut, "Switched to stack %s.\n", name)
return nil
}
|