Skip to content

Commit

Permalink
wip on --assign
Browse files Browse the repository at this point in the history
  • Loading branch information
Taimoor Ahmad committed Sep 13, 2023
1 parent 1bbc50a commit 85e444f
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions src/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
var resourceType string
var assignTag bool

// TODO: support --assign
var createTagCmd = &cobra.Command{
Use: "tag --type=RESOURCE_TYPE RESOURCE_ID KEY VALUE",
Short: "Create a tag",
Long: "Create a tag",
Short: "Create/assign a tag",
Long: "Create/assign a tag",
Example: `
opslevel create tag --type=Team --assign TEAM_ID KEY VALUE
opslevel create tag --type=Team ID|ALIAS KEY VALUE
opslevel create tag --type=Repository --assign ID|ALIAS KEY VALUE
`,
Args: cobra.ExactArgs(3),
ArgAliases: []string{"RESOURCE_ID", "KEY", "VALUE"},
Expand All @@ -37,29 +37,55 @@ opslevel create tag --type=Team --assign TEAM_ID KEY VALUE
key := args[1]
value := args[2]

var input opslevel.TagCreateInput
if opslevel.IsID(resource) {
input = opslevel.TagCreateInput{
Id: opslevel.ID(resource),
if assignTag {
var input opslevel.TagAssignInput
tagInput := opslevel.TagInput{
Key: key,
Value: value,
}

if opslevel.IsID(resource) {
input = opslevel.TagAssignInput{
Id: opslevel.ID(resource),
Tags: []opslevel.TagInput{tagInput},
}
} else {
input = opslevel.TagAssignInput{
Alias: resource,
Type: opslevel.TaggableResource(resourceType),
Tags: []opslevel.TagInput{tagInput},
}
}

_, err := getClientGQL().AssignTag(input)
cobra.CheckErr(err)
fmt.Printf("updated new tag on %s", resource)
} else {
input = opslevel.TagCreateInput{
Alias: resource,
Type: opslevel.TaggableResourceTeam,
Key: key,
Value: value,
var input opslevel.TagCreateInput
if opslevel.IsID(resource) {
input = opslevel.TagCreateInput{
Id: opslevel.ID(resource),
Key: key,
Value: value,
}
} else {
input = opslevel.TagCreateInput{
Alias: resource,
Type: opslevel.TaggableResource(resourceType),
Key: key,
Value: value,
}
}
}

result, err := getClientGQL().CreateTag(input)
cobra.CheckErr(err)
fmt.Printf("added new tag on %s: %s\n", resource, result.Id)
result, err := getClientGQL().CreateTag(input)
cobra.CheckErr(err)
fmt.Printf("added new tag on %s: %s\n", resource, result.Id)
}
},
}

func init() {
createCmd.AddCommand(createTagCmd)
createTagCmd.Flags().StringVar(&resourceType, "type", "", "resource type")
createTagCmd.Flags().Bool("assign", false, "assign a tag instead of creating it")
}

0 comments on commit 85e444f

Please sign in to comment.