Skip to content

Commit 760ad05

Browse files
committed
Implement create-bookmark command
1 parent e280a3b commit 760ad05

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

cmd/createbookmark.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
"time"
12+
13+
"github.com/bufbuild/connect-go"
14+
"github.com/google/uuid"
15+
"github.com/overmindtech/ovm-cli/tracing"
16+
"github.com/overmindtech/sdp-go"
17+
log "github.com/sirupsen/logrus"
18+
"github.com/spf13/cobra"
19+
"github.com/spf13/viper"
20+
"go.opentelemetry.io/otel/attribute"
21+
"go.opentelemetry.io/otel/trace"
22+
)
23+
24+
// createBookmarkCmd represents the get-bookmark command
25+
var createBookmarkCmd = &cobra.Command{
26+
Use: "create-bookmark [--file FILE]",
27+
Short: "Creates a bookmark from JSON.",
28+
PreRun: func(cmd *cobra.Command, args []string) {
29+
// Bind these to viper
30+
err := viper.BindPFlags(cmd.Flags())
31+
if err != nil {
32+
log.WithError(err).Fatal("could not bind `create-bookmark` flags")
33+
}
34+
},
35+
Run: func(cmd *cobra.Command, args []string) {
36+
sigs := make(chan os.Signal, 1)
37+
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
38+
39+
exitcode := CreateBookmark(sigs, nil)
40+
tracing.ShutdownTracer()
41+
os.Exit(exitcode)
42+
},
43+
}
44+
45+
func CreateBookmark(signals chan os.Signal, ready chan bool) int {
46+
timeout, err := time.ParseDuration(viper.GetString("timeout"))
47+
if err != nil {
48+
log.Errorf("invalid --timeout value '%v', error: %v", viper.GetString("timeout"), err)
49+
return 1
50+
}
51+
52+
in := os.Stdin
53+
if viper.GetString("file") != "" {
54+
in, err = os.Open(viper.GetString("file"))
55+
if err != nil {
56+
log.WithError(err).WithFields(log.Fields{
57+
"file": viper.GetString("file"),
58+
}).Error("failed to open input")
59+
return 1
60+
}
61+
}
62+
63+
ctx := context.Background()
64+
ctx, span := tracing.Tracer().Start(ctx, "CLI CreateBookmark", trace.WithAttributes(
65+
attribute.String("om.config", fmt.Sprintf("%v", viper.AllSettings())),
66+
))
67+
defer span.End()
68+
69+
ctx, err = ensureToken(ctx, signals)
70+
if err != nil {
71+
log.WithContext(ctx).WithError(err).WithFields(log.Fields{
72+
"url": viper.GetString("url"),
73+
}).Error("failed to authenticate")
74+
return 1
75+
}
76+
77+
// apply a timeout to the main body of processing
78+
ctx, cancel := context.WithTimeout(ctx, timeout)
79+
defer cancel()
80+
81+
contents, err := io.ReadAll(in)
82+
if err != nil {
83+
log.WithContext(ctx).WithError(err).Error("failed to read file")
84+
return 1
85+
}
86+
msg := sdp.BookmarkProperties{}
87+
err = json.Unmarshal(contents, &msg)
88+
if err != nil {
89+
log.WithContext(ctx).WithError(err).Error("failed to parse input")
90+
return 1
91+
}
92+
client := AuthenticatedBookmarkClient(ctx)
93+
response, err := client.CreateBookmark(ctx, &connect.Request[sdp.CreateBookmarkRequest]{
94+
Msg: &sdp.CreateBookmarkRequest{
95+
Properties: &msg,
96+
},
97+
})
98+
if err != nil {
99+
log.WithContext(ctx).WithError(err).WithFields(log.Fields{
100+
"bookmark-url": viper.GetString("bookmark-url"),
101+
}).Error("failed to get bookmark")
102+
return 1
103+
}
104+
log.WithContext(ctx).WithFields(log.Fields{
105+
"bookmark-uuid": uuid.UUID(response.Msg.Bookmark.Metadata.UUID),
106+
"bookmark-created": response.Msg.Bookmark.Metadata.Created,
107+
"bookmark-name": response.Msg.Bookmark.Properties.Name,
108+
"bookmark-description": response.Msg.Bookmark.Properties.Description,
109+
}).Info("created bookmark")
110+
for _, q := range response.Msg.Bookmark.Properties.Queries {
111+
log.WithContext(ctx).WithFields(log.Fields{
112+
"bookmark-query": q,
113+
}).Info("created bookmark query")
114+
}
115+
for _, i := range response.Msg.Bookmark.Properties.ExcludedItems {
116+
log.WithContext(ctx).WithFields(log.Fields{
117+
"bookmark-excluded-item": i,
118+
}).Info("created bookmark excluded item")
119+
}
120+
121+
b, _ := json.MarshalIndent(response.Msg.Bookmark.Properties, "", " ")
122+
log.Info(string(b))
123+
124+
return 0
125+
}
126+
127+
func init() {
128+
rootCmd.AddCommand(createBookmarkCmd)
129+
130+
createBookmarkCmd.PersistentFlags().String("bookmark-url", "", "The bookmark service API endpoint (defaults to --url)")
131+
132+
createBookmarkCmd.PersistentFlags().String("file", "", "JSON formatted file to read bookmark. (defaults to stdin)")
133+
134+
createBookmarkCmd.PersistentFlags().String("timeout", "1m", "How long to wait for responses")
135+
}

examples/create-bookmark.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Changing items for 'CN=GTS Root R1,O=Google Trust Services'",
3+
"description": "This bookmark contains the items that are changing as part of the 'CN=GTS Root R1,O=Google Trust Services' change. Generated using UpdateChangingItems",
4+
"queries": [
5+
{
6+
"type": "certificate",
7+
"query": "CN=GTS Root R1,O=Google Trust Services",
8+
"scope": "global"
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)