-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdocument.go
More file actions
89 lines (79 loc) · 2.41 KB
/
document.go
File metadata and controls
89 lines (79 loc) · 2.41 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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/opslevel/opslevel-go/v2025"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var createDocumentCmd = &cobra.Command{
Use: "document",
Aliases: []string{"doc"},
Short: "Upload Swagger API documents via a file",
Long: `Upload Swagger API documents via a file:
opslevel create document my-service -i xxxxx -f swagger.json
opslevel create document my-service -r services -t openapi -i xxxxx -f swagger.json
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
serviceAlias := args[0]
flags := cmd.Flags()
resourceType := getResourceType(flags)
documentType := getDocumentType(flags)
integrationID, err := flags.GetString("integration-id")
cobra.CheckErr(err)
integrationURL := fmt.Sprintf("integrations/document/%s/%s/%s/%s", integrationID, resourceType, serviceAlias, documentType)
fileContents, err := os.ReadFile(dataFile)
cobra.CheckErr(err)
var result opslevel.RestResponse
response, err := getClientRest().R().
SetResult(&result).
SetBody(fileContents).
SetHeader("Content-Type", "application/octet-stream").
Post(integrationURL)
cobra.CheckErr(err)
if result.Result == "invalid_format" {
log.Warn().Msgf("%s", result.Message)
} else if response.IsSuccess() {
log.Info().Msgf("Successfully registered api-doc for '%s'", serviceAlias)
} else {
log.Error().Msgf("%s", response)
}
},
}
func init() {
createCmd.AddCommand(createDocumentCmd)
createDocumentCmd.Flags().StringP("integration-id", "i", "", "OpsLevel integration ID")
createDocumentCmd.Flags().StringP("resource-type", "r", "services", "OpsLevel Resource Type (options [\\\"services\\\"])")
createDocumentCmd.Flags().StringP("document-type", "t", "openapi", "API Document Type (options [\\\"openapi\\\", \\\"swagger\\\"])")
}
func getResourceType(flags *pflag.FlagSet) string {
resourceType, err := flags.GetString("resource-type")
if err != nil {
return "services"
}
switch strings.ToLower(resourceType) {
case "services":
return "services"
case "service":
return "services"
default:
return "services"
}
}
func getDocumentType(flags *pflag.FlagSet) string {
documentType, err := flags.GetString("document-type")
if err != nil {
return "openapi"
}
switch strings.ToLower(documentType) {
case "openapi":
return "openapi"
case "swagger":
return "openapi"
default:
return "openapi"
}
}