-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathdelete-context.go
More file actions
92 lines (76 loc) · 2.72 KB
/
delete-context.go
File metadata and controls
92 lines (76 loc) · 2.72 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package config
import (
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
"github.com/microsoft/go-sqlcmd/internal/config"
"github.com/microsoft/go-sqlcmd/internal/localizer"
)
// DeleteContext implements the `sqlcmd config delete-context` command
type DeleteContext struct {
cmdparser.Cmd
name string
cascade bool
}
func (c *DeleteContext) DefineCommand(...cmdparser.CommandOptions) {
options := cmdparser.CommandOptions{
Use: "delete-context",
Short: localizer.Sprintf("Delete a context"),
Examples: []cmdparser.ExampleOptions{
{
Description: localizer.Sprintf("Delete a context (including its endpoint and user)"),
Steps: []string{
"sqlcmd config delete-context --name my-context --cascade",
"sqlcmd config delete-context my-context --cascade"},
},
{
Description: localizer.Sprintf("Delete a context (excluding its endpoint and user)"),
Steps: []string{
"sqlcmd config delete-context --name my-context --cascade=false",
"sqlcmd config delete-context my-context --cascade=false"},
},
},
Run: c.run,
FirstArgAlternativeForFlag: &cmdparser.AlternativeForFlagOptions{Flag: "name", Value: &c.name},
}
c.Cmd.DefineCommand(options)
c.AddFlag(cmdparser.FlagOptions{
String: &c.name,
Name: "name",
Usage: localizer.Sprintf("Name of context to delete")})
c.AddFlag(cmdparser.FlagOptions{
Bool: &c.cascade,
Name: "cascade",
DefaultBool: true,
Usage: localizer.Sprintf("Delete the context's endpoint and user as well")})
}
// run is responsible for deleting a context in a configuration. It first checks if
// a name is provided and if the context exists. If the cascade flag is set, it will
// also delete the associated endpoint and user. It then deletes the context
// and prints a message to the output indicating the context has been deleted.
func (c *DeleteContext) run() {
output := c.Output()
if c.name == "" {
output.FatalWithHints([]string{localizer.Sprintf("Use the %s flag to pass in a context name to delete", localizer.NameFlag)},
"A 'name' is required")
}
if config.ContextExists(c.name) {
context := config.GetContext(c.name)
if c.cascade {
config.DeleteEndpoint(context.Endpoint)
if config.UserExists(context) {
config.DeleteUser(*context.ContextDetails.User)
}
for _, c := range context.AddOns {
config.DeleteEndpoint(c.Endpoint)
}
}
config.DeleteContext(c.name)
output.Info(localizer.Sprintf("Context '%v' deleted", c.name))
} else {
output.FatalWithHintExamples([][]string{
{localizer.Sprintf("View available contexts"), "sqlcmd config get-contexts"},
},
localizer.Sprintf("Context '%v' does not exist", c.name))
}
}