Skip to content

Commit 2e98444

Browse files
author
Timothy MacDonald
authored
refactor(scaffolding): update CDK Go scaffolding (#1463)
* refactor(scaffolding): update CDK Go scaffolding Adhere to golang project layout standards Signed-off-by: Timothy MacDonald <[email protected]> * refactor(scaffolding): change directory structure to match best practices Signed-off-by: Timothy MacDonald <[email protected]> * refactor(scaffolding): update CDK Go scaffolding Update to match project structure best practices and add internals Signed-off-by: Timothy MacDonald <[email protected]> * refactor(typo): change comment Signed-off-by: Timothy MacDonald <[email protected]> * refactor(comments): fix comments Signed-off-by: Timothy MacDonald <[email protected]> * refactor: change case of README.md Signed-off-by: Timothy MacDonald <[email protected]> * refactor: fix case of README.md Signed-off-by: Timothy MacDonald <[email protected]> --------- Signed-off-by: Timothy MacDonald <[email protected]>
1 parent dc17dbb commit 2e98444

File tree

10 files changed

+147
-8
lines changed

10 files changed

+147
-8
lines changed

internal/databox/blob.go

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/databox/static/scaffoldings/golang/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ COVERAGEHTML?=coverage.html
77
GOJUNITOUT?=go-junit.xml
88
PACKAGENAME?=[[.Component]]
99
CLINAME?=lacework
10-
GO_LDFLAGS="-X github.com/lacework/go-sdk/cli/cmd.Version=$(shell cat VERSION) \
11-
-X github.com/lacework/go-sdk/cli/cmd.GitSHA=$(shell git rev-parse HEAD) \
12-
-X github.com/lacework/go-sdk/cli/cmd.BuildTime=$(shell date +%Y%m%d%H%M%S)"
10+
GO_LDFLAGS="-X [[.Component]]/internal/version.Version=$(shell cat VERSION) \
11+
-X [[.Component]]/internal/version.GitSHA=$(shell git rev-parse HEAD) \
12+
-X [[.Component]]/internal/version..BuildTime=$(shell date +%Y%m%d%H%M%S)"
1313
GOFLAGS=-mod=vendor
1414
CGO_ENABLED?=0
1515
export GOFLAGS GO_LDFLAGS CGO_ENABLED
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `/cmd`
2+
3+
Main applications for this project.

internal/databox/static/scaffoldings/golang/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module [[.Component]]
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/spf13/cobra v1.6.1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `/internal`
2+
3+
Private application and library code. This is the code you don't want others importing in their applications or libraries.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright:: Copyright 2023, Lacework Inc.
3+
// License:: Apache License, Version 2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
18+
package logger
19+
20+
import (
21+
"os"
22+
23+
"github.com/lacework/go-sdk/lwlogger"
24+
)
25+
26+
// Log allows this component to leverage our Go-SDK lwlogger
27+
//
28+
// Example:
29+
//
30+
// import "[[.Component]]/internal/logger"
31+
// logger.Log.Info("an informational message")
32+
// logger.Log.Debug("a debug message")
33+
// logger.Log.Infow("info message with variables", "foo", "bar")
34+
var Log = lwlogger.New(os.Getenv("LW_LOG")).Sugar()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package metric
2+
3+
import (
4+
"context"
5+
"os"
6+
7+
"[[.Component]]/internal/logger"
8+
"[[.Component]]/internal/version"
9+
10+
cdk "github.com/lacework/go-sdk/cli/cdk/go/proto/v1"
11+
"google.golang.org/grpc"
12+
"google.golang.org/grpc/credentials/insecure"
13+
)
14+
15+
var cdkClient cdk.CoreClient
16+
17+
func init() {
18+
// Set up a connection to the CDK server
19+
logger.Log.Infow("connecting to gRPC server", "address", os.Getenv("LW_CDK_TARGET"))
20+
conn, err := grpc.Dial(os.Getenv("LW_CDK_TARGET"),
21+
// we allow insecure connections since we are connecting to 'localhost'
22+
grpc.WithTransportCredentials(insecure.NewCredentials()))
23+
if err != nil {
24+
logger.Log.Warn("Cannot initialize CDK client", "error", err.Error())
25+
} else {
26+
cdkClient = cdk.NewCoreClient(conn)
27+
}
28+
}
29+
30+
func SendMetricData(feature string, data map[string]string) {
31+
if cdkClient == nil {
32+
logger.Log.Warn("unable to send telemetry",
33+
"type", "data",
34+
"error", "client not initialized",
35+
)
36+
return
37+
}
38+
39+
// add version
40+
data["version"] = version.Version
41+
42+
go func() {
43+
_, err := cdkClient.Honeyvent(context.Background(), &cdk.HoneyventRequest{
44+
Feature: feature, FeatureData: data,
45+
})
46+
if err != nil {
47+
logger.Log.Warn("unable to send telemetry",
48+
"type", "data", "error", err.Error(),
49+
)
50+
}
51+
}()
52+
}
53+
54+
func SendMetricError(e error) {
55+
if cdkClient == nil {
56+
logger.Log.Warn("unable to send telemetry",
57+
"type", "error",
58+
"error", "client not initialized",
59+
)
60+
return
61+
}
62+
63+
go func() {
64+
_, err := cdkClient.Honeyvent(context.Background(), &cdk.HoneyventRequest{
65+
Error: e.Error(),
66+
})
67+
if err != nil {
68+
logger.Log.Warn("unable to send telemetry",
69+
"type", "error", "error", err.Error(),
70+
)
71+
}
72+
}()
73+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package version
2+
3+
var (
4+
// All the following "unknown" variables are being injected at
5+
// build time via the cross-platform directive inside the Makefile
6+
//
7+
// Version is the semver coming from the VERSION file
8+
Version = "unknown"
9+
10+
// GitSHA is the git ref that the cli was built from
11+
GitSHA = "unknown"
12+
13+
// BuildTime is a human-readable time when the cli was built at
14+
BuildTime = "unknown"
15+
)

internal/databox/static/scaffoldings/golang/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package main
1919

20-
import "[[.Component]]/cmd"
20+
import (
21+
"[[.Component]]/cmd"
22+
)
2123

2224
func main() {
2325
cmd.Execute()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `/pkg`
2+
3+
Library code that's ok to use by external applications (e.g., `/pkg/mypubliclib`).

0 commit comments

Comments
 (0)