-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdelete.go
More file actions
108 lines (84 loc) · 2.46 KB
/
delete.go
File metadata and controls
108 lines (84 loc) · 2.46 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cmd
import (
"fmt"
"os"
"strings"
"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/spf13/cobra"
)
func NewDeleteCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
var deleteCmd = &cobra.Command{
Use: "delete <serviceName:version>",
Short: "Delete an API from Microcks server",
Long: "Delete an API (service + version) from Microcks server",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
input := args[0]
// Validate input format
if !strings.Contains(input, ":") {
fmt.Println("delete requires <serviceName:version>")
os.Exit(1)
}
parts := strings.SplitN(input, ":", 2)
service := parts[0]
version := parts[1]
if service == "" || version == "" {
fmt.Println("delete requires both serviceName and version (neither can be empty)")
os.Exit(1)
}
// Load config (same as import)
config.InsecureTLS = globalClientOpts.InsecureTLS
config.CaCertPaths = globalClientOpts.CaCertPaths
config.Verbose = globalClientOpts.Verbose
localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
if err != nil {
fmt.Println(err)
return
}
var mc connectors.MicrocksClient
// Same auth logic as import (DO NOT DUPLICATE BADLY → reuse later)
if globalClientOpts.ServerAddr != "" &&
globalClientOpts.ClientId != "" &&
globalClientOpts.ClientSecret != "" {
mc = connectors.NewMicrocksClient(globalClientOpts.ServerAddr)
keycloakURL, err := mc.GetKeycloakURL()
if err != nil {
fmt.Printf("Error retrieving config: %s", err)
os.Exit(1)
}
token := "unauthenticated-token"
if keycloakURL != "null" {
kc := connectors.NewKeycloakClient(
keycloakURL,
globalClientOpts.ClientId,
globalClientOpts.ClientSecret,
)
token, err = kc.ConnectAndGetToken()
if err != nil {
fmt.Printf("Auth error: %s", err)
os.Exit(1)
}
}
mc.SetOAuthToken(token)
} else {
if localConfig == nil {
fmt.Println("Please login to perform operation...")
return
}
mc, err = connectors.NewClient(*globalClientOpts)
if err != nil {
fmt.Printf("error %v", err)
return
}
}
err = mc.DeleteService(service, version)
if err != nil {
fmt.Printf("Delete failed: %s\n", err)
os.Exit(1)
}
fmt.Printf("Deleted service '%s:%s'\n", service, version)
},
}
return deleteCmd
}