Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit cc3cd88

Browse files
author
Sam McGeown
committed
Delete endpoint by name
1 parent 95307ba commit cc3cd88

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

cmd/cli-helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func getYamlFilePaths(importPath string) []string {
5050
// Read importPath
5151
stat, err := os.Stat(importPath)
5252
if err == nil && stat.IsDir() {
53-
log.Debugln("importPath is a directory")
53+
// log.Debugln("importPath is a directory")
5454
files, err := ioutil.ReadDir(importPath)
5555
if err != nil {
5656
log.Fatal(err)
@@ -61,7 +61,7 @@ func getYamlFilePaths(importPath string) []string {
6161
}
6262
}
6363
} else {
64-
log.Debugln("importPath is a file")
64+
// log.Debugln("importPath is a file")
6565
yamlFiles = append(yamlFiles, importPath)
6666
}
6767
return yamlFiles

cmd/endpoint.go

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ SPDX-License-Identifier: BSD-2-Clause
55
package cmd
66

77
import (
8+
"errors"
9+
"fmt"
810
"os"
11+
"path/filepath"
912

1013
log "github.com/sirupsen/logrus"
1114

@@ -64,11 +67,19 @@ var createEndpointCmd = &cobra.Command{
6467
}
6568

6669
if importPath != "" {
67-
err := importYaml(importPath, "create")
68-
if err != nil {
69-
log.Fatalln("Failed to import Endpoint", err)
70+
yamlFilePaths := getYamlFilePaths(importPath)
71+
if len(yamlFilePaths) == 0 {
72+
log.Warnln("No YAML files were found in", importPath)
73+
}
74+
for _, yamlFilePath := range yamlFilePaths {
75+
yamlFileName := filepath.Base(yamlFilePath)
76+
err := importYaml(yamlFilePath, "create")
77+
if err != nil {
78+
log.Warnln("Failed to import", yamlFilePath, "as Endpoint", err)
79+
} else {
80+
fmt.Println("Imported", yamlFileName, "successfully - Endpoint created.")
81+
}
7082
}
71-
log.Println("Imported successfully, Endpoint created.")
7283
}
7384
},
7485
}
@@ -79,20 +90,30 @@ var updateEndpointCmd = &cobra.Command{
7990
Short: "Update an Endpoint",
8091
Long: `Update an Endpoint by importing the YAML specification
8192
82-
Update from YAML
83-
cs-cli update endpoint --importPath "/Users/sammcgeown/Desktop/updated-endpoint.yaml"
93+
Update from a YAML file
94+
cs-cli update endpoint --importPath "/Users/sammcgeown/cs-cli/endpoints/updated-endpoint.yaml"
95+
Update from a folder of YAML files
96+
cs-cli update endpoint --importPath "/Users/sammcgeown/cs-cli/endpoints"
8497
`,
8598
Run: func(cmd *cobra.Command, args []string) {
8699
if err := ensureTargetConnection(); err != nil {
87100
log.Fatalln(err)
88101
}
89102

90103
if importPath != "" {
91-
err := importYaml(importPath, "update")
92-
if err != nil {
93-
log.Fatalln("Failed to import Endpoint", err)
104+
yamlFilePaths := getYamlFilePaths(importPath)
105+
if len(yamlFilePaths) == 0 {
106+
log.Warnln("No YAML files were found in", importPath)
107+
}
108+
for _, yamlFilePath := range yamlFilePaths {
109+
yamlFileName := filepath.Base(yamlFilePath)
110+
err := importYaml(yamlFilePath, "apply")
111+
if err != nil {
112+
log.Warnln("Failed to import", yamlFilePath, "as Endpoint", err)
113+
} else {
114+
fmt.Println("Imported", yamlFileName, "successfully - Endpoint updated.")
115+
}
94116
}
95-
log.Println("Imported successfully, Endpoint updated.")
96117
}
97118
},
98119
}
@@ -101,19 +122,36 @@ var updateEndpointCmd = &cobra.Command{
101122
var deleteEndpointCmd = &cobra.Command{
102123
Use: "endpoint",
103124
Short: "Delete an Endpoint",
104-
Long: `Delete an Endpoint with a specific Endpoint ID
125+
Long: `Delete an Endpoint with a specific Endpoint ID or Name
105126
106127
`,
128+
Args: func(cmd *cobra.Command, args []string) error {
129+
if id != "" && name != "" {
130+
return errors.New("please specify either endpoint name or endpoint id")
131+
}
132+
if id == "" && name == "" {
133+
return errors.New("please specify endpoint name or endpoint id")
134+
}
135+
136+
return nil
137+
},
107138
Run: func(cmd *cobra.Command, args []string) {
108139
if err := ensureTargetConnection(); err != nil {
109140
log.Fatalln(err)
110141
}
142+
if name != "" {
143+
response, err := getEndpoint(id, name, project, typename, export, exportPath)
144+
if err != nil {
145+
log.Fatalln(err)
146+
}
147+
id = response[0].ID
148+
}
111149

112150
response, err := deleteEndpoint(id)
113151
if err != nil {
114152
log.Println("Unable to delete Endpoint: ", err)
115153
}
116-
log.Println("Endpoint with id " + response.ID + " deleted")
154+
fmt.Println("Endpoint with id " + response.ID + " deleted")
117155
},
118156
}
119157

@@ -136,6 +174,6 @@ func init() {
136174
// Delete
137175
deleteCmd.AddCommand(deleteEndpointCmd)
138176
deleteEndpointCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the Endpoint to delete")
139-
deleteEndpointCmd.MarkFlagRequired("id")
177+
deleteEndpointCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the Endpoint to delete")
140178

141179
}

0 commit comments

Comments
 (0)