-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathalias.go
More file actions
85 lines (78 loc) · 2.27 KB
/
alias.go
File metadata and controls
85 lines (78 loc) · 2.27 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
package cmd
import (
"fmt"
"os"
"slices"
"strings"
"github.com/opslevel/opslevel-go/v2025"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var exampleAliasCmd = &cobra.Command{
Use: "alias",
Short: "Example alias",
Long: `Example alias`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getExample(opslevel.AliasCreateInput{
OwnerId: opslevel.ID("Z2lkOi8vc2VydmljZS8xMjM0NTY3ODk"),
Alias: "example_alias",
}))
},
}
var createAliasCommand = &cobra.Command{
Use: "alias ID ALIAS",
Short: "Create an alias",
Long: "Create an alias",
Args: cobra.MinimumNArgs(2),
Example: `opslevel create alias XXXXX my-alias`,
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
alias := args[1]
client := getClientGQL()
_, err := client.CreateAlias(opslevel.AliasCreateInput{
OwnerId: opslevel.ID(id),
Alias: alias,
})
if err != nil {
log.Error().Err(err).Msg("failed to create alias")
} else {
log.Info().Msg("alias created")
}
},
}
var deleteAliasCommand = &cobra.Command{
Use: "alias ALIAS",
Short: "Delete an alias",
Long: "Delete an alias",
Args: cobra.MinimumNArgs(1),
Example: `
# Delete alias on a service
opslevel delete alias my-service-alias
# Or specify -t to target a different resource type
opslevel delete alias -t group my-group-alias
opslevel delete alias -t infrastructure-resource my-infra-alias`,
Run: func(cmd *cobra.Command, args []string) {
alias := args[0]
aliasType := cmd.Flags().Lookup("type").Value.String()
if !slices.Contains(opslevel.AllAliasOwnerTypeEnum, aliasType) {
log.Error().Msgf("invalid alias type '%s'", aliasType)
os.Exit(1)
}
client := getClientGQL()
err := client.DeleteAlias(opslevel.AliasDeleteInput{
Alias: alias,
OwnerType: opslevel.AliasOwnerTypeEnum(aliasType),
})
if err != nil {
log.Error().Err(err).Msgf("failed to delete alias '%s'", alias)
} else {
log.Info().Msgf("alias '%s' deleted", alias)
}
},
}
func init() {
exampleCmd.AddCommand(exampleAliasCmd)
createCmd.AddCommand(createAliasCommand)
deleteCmd.AddCommand(deleteAliasCommand)
deleteAliasCommand.Flags().StringP("type", "t", "service", fmt.Sprintf("the resource type that the alias is on. Can be one of [%s]", strings.Join(opslevel.AllAliasOwnerTypeEnum, ", ")))
}