Skip to content

Commit 192ff12

Browse files
authored
New CLI commands (directory* + notification* schemas) (#2274)
* add new CLI commands * new go client
1 parent c4acabb commit 192ff12

39 files changed

Lines changed: 2689 additions & 18 deletions

cli/cmd/directoryaccount/create.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//go:build cli
2+
3+
package directoryaccount
4+
5+
import (
6+
"context"
7+
8+
"github.com/spf13/cobra"
9+
10+
"github.com/theopenlane/core/cli/cmd"
11+
"github.com/theopenlane/go-client/graphclient"
12+
)
13+
14+
var createCmd = &cobra.Command{
15+
Use: "create",
16+
Short: "create a new directory account",
17+
Run: func(cmd *cobra.Command, args []string) {
18+
err := create(cmd.Context())
19+
cobra.CheckErr(err)
20+
},
21+
}
22+
23+
func init() {
24+
command.AddCommand(createCmd)
25+
26+
createCmd.Flags().StringP("external-id", "e", "", "stable identifier from the directory system")
27+
createCmd.Flags().String("canonical-email", "", "primary email address of the account")
28+
createCmd.Flags().String("display-name", "", "display name of the account")
29+
createCmd.Flags().String("given-name", "", "first name reported by the provider")
30+
createCmd.Flags().String("family-name", "", "last name reported by the provider")
31+
createCmd.Flags().String("job-title", "", "title captured at sync time")
32+
createCmd.Flags().String("department", "", "department captured at sync time")
33+
createCmd.Flags().String("directory-name", "", "directory source label (e.g. googleworkspace, github, slack)")
34+
createCmd.Flags().String("directory-instance-id", "", "stable external workspace or tenant identifier")
35+
createCmd.Flags().String("integration-id", "", "integration that owns this directory account")
36+
createCmd.Flags().String("directory-sync-run-id", "", "sync run that produced this snapshot")
37+
createCmd.Flags().String("platform-id", "", "platform associated with this directory account")
38+
createCmd.Flags().StringSlice("tags", []string{}, "tags associated with the account")
39+
}
40+
41+
// createValidation validates the required fields for the command
42+
func createValidation() (input graphclient.CreateDirectoryAccountInput, err error) {
43+
externalID := cmd.Config.String("external-id")
44+
if externalID == "" {
45+
return input, cmd.NewRequiredFieldMissingError("external id")
46+
}
47+
48+
input.ExternalID = externalID
49+
50+
canonicalEmail := cmd.Config.String("canonical-email")
51+
if canonicalEmail != "" {
52+
input.CanonicalEmail = &canonicalEmail
53+
}
54+
55+
displayName := cmd.Config.String("display-name")
56+
if displayName != "" {
57+
input.DisplayName = &displayName
58+
}
59+
60+
givenName := cmd.Config.String("given-name")
61+
if givenName != "" {
62+
input.GivenName = &givenName
63+
}
64+
65+
familyName := cmd.Config.String("family-name")
66+
if familyName != "" {
67+
input.FamilyName = &familyName
68+
}
69+
70+
jobTitle := cmd.Config.String("job-title")
71+
if jobTitle != "" {
72+
input.JobTitle = &jobTitle
73+
}
74+
75+
department := cmd.Config.String("department")
76+
if department != "" {
77+
input.Department = &department
78+
}
79+
80+
directoryName := cmd.Config.String("directory-name")
81+
if directoryName != "" {
82+
input.DirectoryName = &directoryName
83+
}
84+
85+
directoryInstanceID := cmd.Config.String("directory-instance-id")
86+
if directoryInstanceID != "" {
87+
input.DirectoryInstanceID = &directoryInstanceID
88+
}
89+
90+
integrationID := cmd.Config.String("integration-id")
91+
if integrationID != "" {
92+
input.IntegrationID = &integrationID
93+
}
94+
95+
directorySyncRunID := cmd.Config.String("directory-sync-run-id")
96+
if directorySyncRunID != "" {
97+
input.DirectorySyncRunID = &directorySyncRunID
98+
}
99+
100+
platformID := cmd.Config.String("platform-id")
101+
if platformID != "" {
102+
input.PlatformID = &platformID
103+
}
104+
105+
tags := cmd.Config.Strings("tags")
106+
if len(tags) > 0 {
107+
input.Tags = tags
108+
}
109+
110+
return input, nil
111+
}
112+
113+
// create a new directory account
114+
func create(ctx context.Context) error {
115+
client, err := cmd.TokenAuth(ctx, cmd.Config)
116+
if err != nil || client == nil {
117+
client, err = cmd.SetupClientWithAuth(ctx)
118+
cobra.CheckErr(err)
119+
defer cmd.StoreSessionCookies(client)
120+
}
121+
122+
input, err := createValidation()
123+
cobra.CheckErr(err)
124+
125+
o, err := client.CreateDirectoryAccount(ctx, input)
126+
cobra.CheckErr(err)
127+
128+
return consoleOutput(o)
129+
}

cli/cmd/directoryaccount/delete.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//go:build cli
2+
3+
package directoryaccount
4+
5+
import (
6+
"context"
7+
8+
"github.com/spf13/cobra"
9+
10+
"github.com/theopenlane/core/cli/cmd"
11+
)
12+
13+
var deleteCmd = &cobra.Command{
14+
Use: "delete",
15+
Short: "delete an existing directory account",
16+
Run: func(cmd *cobra.Command, args []string) {
17+
err := delete(cmd.Context())
18+
cobra.CheckErr(err)
19+
},
20+
}
21+
22+
func init() {
23+
command.AddCommand(deleteCmd)
24+
25+
deleteCmd.Flags().StringP("id", "i", "", "directory account id to delete")
26+
}
27+
28+
// deleteValidation validates the required fields for the command
29+
func deleteValidation() (string, error) {
30+
id := cmd.Config.String("id")
31+
if id == "" {
32+
return "", cmd.NewRequiredFieldMissingError("directory account id")
33+
}
34+
35+
return id, nil
36+
}
37+
38+
// delete an existing directory account
39+
func delete(ctx context.Context) error {
40+
client, err := cmd.TokenAuth(ctx, cmd.Config)
41+
if err != nil || client == nil {
42+
client, err = cmd.SetupClientWithAuth(ctx)
43+
cobra.CheckErr(err)
44+
defer cmd.StoreSessionCookies(client)
45+
}
46+
47+
id, err := deleteValidation()
48+
cobra.CheckErr(err)
49+
50+
o, err := client.DeleteDirectoryAccount(ctx, id)
51+
cobra.CheckErr(err)
52+
53+
return consoleOutput(o)
54+
}

cli/cmd/directoryaccount/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//go:build cli
2+
3+
// Package directoryaccount is our cobra cli for directory account endpoints
4+
package directoryaccount

cli/cmd/directoryaccount/get.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//go:build cli
2+
3+
package directoryaccount
4+
5+
import (
6+
"context"
7+
"strings"
8+
9+
"github.com/spf13/cobra"
10+
11+
"github.com/theopenlane/core/cli/cmd"
12+
"github.com/theopenlane/go-client/graphclient"
13+
)
14+
15+
var getCmd = &cobra.Command{
16+
Use: "get",
17+
Short: "get an existing directory account",
18+
Run: func(cmd *cobra.Command, args []string) {
19+
err := get(cmd.Context())
20+
cobra.CheckErr(err)
21+
},
22+
}
23+
24+
func init() {
25+
command.AddCommand(getCmd)
26+
27+
getCmd.Flags().StringP("id", "i", "", "directory account id to query")
28+
}
29+
30+
// get an existing directory account
31+
func get(ctx context.Context) error {
32+
client, err := cmd.TokenAuth(ctx, cmd.Config)
33+
if err != nil || client == nil {
34+
client, err = cmd.SetupClientWithAuth(ctx)
35+
cobra.CheckErr(err)
36+
defer cmd.StoreSessionCookies(client)
37+
}
38+
39+
id := cmd.Config.String("id")
40+
41+
if id != "" {
42+
o, err := client.GetDirectoryAccountByID(ctx, id)
43+
cobra.CheckErr(err)
44+
45+
return consoleOutput(o)
46+
}
47+
48+
var order *graphclient.DirectoryAccountOrder
49+
if cmd.OrderBy != nil && cmd.OrderDirection != nil {
50+
order = &graphclient.DirectoryAccountOrder{
51+
Direction: graphclient.OrderDirection(strings.ToUpper(*cmd.OrderDirection)),
52+
Field: graphclient.DirectoryAccountOrderField(*cmd.OrderBy),
53+
}
54+
}
55+
56+
o, err := client.GetAllDirectoryAccounts(ctx, cmd.First, cmd.Last, cmd.After, cmd.Before, []*graphclient.DirectoryAccountOrder{order})
57+
cobra.CheckErr(err)
58+
59+
return consoleOutput(o)
60+
}

cli/cmd/directoryaccount/root.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//go:build cli
2+
3+
package directoryaccount
4+
5+
import (
6+
"encoding/json"
7+
"strings"
8+
9+
"github.com/spf13/cobra"
10+
11+
"github.com/theopenlane/core/cli/cmd"
12+
"github.com/theopenlane/utils/cli/tables"
13+
14+
"github.com/theopenlane/go-client/graphclient"
15+
)
16+
17+
// command represents the base directory-account command when called without any subcommands
18+
var command = &cobra.Command{
19+
Use: "directory-account",
20+
Short: "the subcommands for working with directory accounts",
21+
}
22+
23+
func init() {
24+
cmd.RootCmd.AddCommand(command)
25+
}
26+
27+
// consoleOutput prints the output in the console
28+
func consoleOutput(e any) error {
29+
if strings.EqualFold(cmd.OutputFormat, cmd.JSONOutput) {
30+
return jsonOutput(e)
31+
}
32+
33+
switch v := e.(type) {
34+
case *graphclient.GetAllDirectoryAccounts:
35+
var nodes []*graphclient.GetAllDirectoryAccounts_DirectoryAccounts_Edges_Node
36+
37+
for _, i := range v.DirectoryAccounts.Edges {
38+
nodes = append(nodes, i.Node)
39+
}
40+
41+
e = nodes
42+
case *graphclient.GetDirectoryAccountByID:
43+
e = v.DirectoryAccount
44+
case *graphclient.CreateDirectoryAccount:
45+
e = v.CreateDirectoryAccount.DirectoryAccount
46+
case *graphclient.UpdateDirectoryAccount:
47+
e = v.UpdateDirectoryAccount.DirectoryAccount
48+
case *graphclient.DeleteDirectoryAccount:
49+
deletedTableOutput(v)
50+
return nil
51+
}
52+
53+
s, err := json.Marshal(e)
54+
cobra.CheckErr(err)
55+
56+
var list []graphclient.DirectoryAccount
57+
58+
err = json.Unmarshal(s, &list)
59+
if err != nil {
60+
var in graphclient.DirectoryAccount
61+
err = json.Unmarshal(s, &in)
62+
cobra.CheckErr(err)
63+
64+
list = append(list, in)
65+
}
66+
67+
tableOutput(list)
68+
69+
return nil
70+
}
71+
72+
// jsonOutput prints the output in a JSON format
73+
func jsonOutput(out any) error {
74+
s, err := json.Marshal(out)
75+
cobra.CheckErr(err)
76+
77+
return cmd.JSONPrint(s)
78+
}
79+
80+
// tableOutput prints the output in a table format
81+
func tableOutput(out []graphclient.DirectoryAccount) {
82+
writer := tables.NewTableWriter(command.OutOrStdout(), "ID", "DisplayID", "ExternalID", "DisplayName", "CanonicalEmail", "GivenName", "FamilyName", "Status", "Department")
83+
for _, i := range out {
84+
writer.AddRow(i.ID, i.DisplayID, i.ExternalID, derefStr(i.DisplayName), derefStr(i.CanonicalEmail), derefStr(i.GivenName), derefStr(i.FamilyName), i.Status, derefStr(i.Department))
85+
}
86+
87+
writer.Render()
88+
}
89+
90+
// deletedTableOutput prints the deleted id in a table format
91+
func deletedTableOutput(e *graphclient.DeleteDirectoryAccount) {
92+
writer := tables.NewTableWriter(command.OutOrStdout(), "DeletedID")
93+
94+
writer.AddRow(e.DeleteDirectoryAccount.DeletedID)
95+
96+
writer.Render()
97+
}
98+
99+
// derefStr safely dereferences a string pointer
100+
func derefStr(s *string) string {
101+
if s == nil {
102+
return ""
103+
}
104+
105+
return *s
106+
}

0 commit comments

Comments
 (0)