Repository navigation
Expand file tree
/
Copy pathunstack.go
More file actions
72 lines (61 loc) · 1.63 KB
/
Copy pathunstack.go
File metadata and controls
72 lines (61 loc) · 1.63 KB
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
68
69
70
71
72
package cmd
import (
"github.com/github/gh-stack/internal/config"
"github.com/github/gh-stack/internal/stack"
"github.com/spf13/cobra"
)
type unstackOptions struct {
target string
local bool
}
func UnstackCmd(cfg *config.Config) *cobra.Command {
opts := &unstackOptions{}
cmd := &cobra.Command{
Use: "unstack [branch]",
Short: "Delete a stack locally and on GitHub",
Long: "Remove a stack from local tracking and delete it on GitHub. Use --local to only remove local tracking.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
opts.target = args[0]
}
return runUnstack(cfg, opts)
},
}
cmd.Flags().BoolVar(&opts.local, "local", false, "Only delete the stack locally")
return cmd
}
func runUnstack(cfg *config.Config, opts *unstackOptions) error {
result, err := loadStack(cfg, opts.target)
if err != nil {
return ErrNotInStack
}
gitDir := result.GitDir
sf := result.StackFile
s := result.Stack
target := opts.target
if target == "" {
target = result.CurrentBranch
}
cfg.Printf("Stack branches: %v", s.BranchNames())
// Remove from local tracking
sf.RemoveStackForBranch(target)
if err := stack.Save(gitDir, sf); err != nil {
return handleSaveError(cfg, err)
}
cfg.Successf("Stack removed from local tracking")
// Delete the stack on GitHub
if !opts.local {
client, err := cfg.GitHubClient()
if err != nil {
cfg.Errorf("failed to create GitHub client: %s", err)
return ErrAPIFailure
}
if err := client.DeleteStack(); err != nil {
cfg.Warningf("%v", err)
} else {
cfg.Successf("Stack deleted on GitHub")
}
}
return nil
}