Skip to content

Commit 086d556

Browse files
authored
Merge pull request #64 from replicatedhq/ffi
Support CGO for calling analysis from other languages
2 parents 8125d55 + 996a55b commit 086d556

File tree

6 files changed

+318
-23
lines changed

6 files changed

+318
-23
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export GO111MODULE=on
55

66
all: test manager
77

8+
.PHONY: ffi
9+
ffi: fmt vet
10+
go build -o bin/troubleshoot.so -buildmode=c-shared ffi/main.go
11+
812
# Run tests
913
test: generate fmt vet manifests
1014
go test ./pkg/... ./cmd/... -coverprofile cover.out

deploy/.goreleaser.snapshot.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ release:
44
owner: replicatedhq
55
name: troubleshoot
66
builds:
7+
- id: so
8+
goos:
9+
- linux
10+
goarch:
11+
- amd64
12+
env:
13+
- CGO_ENABLED=1
14+
- GO111MODULE=on
15+
main: ffi/main.go
16+
flags: -buildmode=c-shared
17+
binary: troubleshoot.so
18+
hooks: {}
719
- id: collector
820
goos:
921
- linux
@@ -73,6 +85,10 @@ builds:
7385
binary: manager
7486
hooks: {}
7587
archives:
88+
- id: so
89+
builds:
90+
- so
91+
format: tar.gz
7692
- id: tar
7793
format: tar.gz
7894
name_template: '{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{.Arm }}{{ end }}-alpha'

deploy/.goreleaser.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ release:
44
owner: replicatedhq
55
name: troubleshoot
66
builds:
7+
- id: so
8+
goos:
9+
- linux
10+
goarch:
11+
- amd64
12+
env:
13+
- CGO_ENABLED=1
14+
- GO111MODULE=on
15+
main: ffi/main.go
16+
flags: -buildmode=c-shared
17+
binary: troubleshoot.so
18+
hooks: {}
719
- id: collector
820
goos:
921
- linux
@@ -73,6 +85,10 @@ builds:
7385
binary: manager
7486
hooks: {}
7587
archives:
88+
- id: so
89+
builds:
90+
- so
91+
format: tar.gz
7692
- id: preflight
7793
builds:
7894
- preflight

ffi/main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import "C"
4+
5+
import (
6+
"fmt"
7+
"context"
8+
"encoding/json"
9+
10+
"gopkg.in/yaml.v2"
11+
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
12+
"github.com/replicatedhq/troubleshoot/pkg/logger"
13+
"github.com/replicatedhq/troubleshoot/pkg/convert"
14+
15+
)
16+
17+
//export Analyze
18+
func Analyze(bundleURL string, outputFormat string, compatibility string) *C.char {
19+
logger.SetQuiet(true)
20+
21+
result, err := analyzer.DownloadAndAnalyze(context.TODO(), bundleURL)
22+
if err != nil {
23+
fmt.Printf("error downloading and analyzing: %s\n", err.Error())
24+
return C.CString("")
25+
}
26+
27+
var data interface{}
28+
switch compatibility {
29+
case "support-bundle":
30+
data = convert.FromAnalyzerResult(result)
31+
default:
32+
data = result
33+
}
34+
35+
var formatted []byte
36+
switch outputFormat {
37+
case "json":
38+
formatted, err = json.MarshalIndent(data, "", " ")
39+
case "", "yaml":
40+
formatted, err = yaml.Marshal(data)
41+
default:
42+
fmt.Printf("unknown output format: %s\n", outputFormat)
43+
return C.CString("")
44+
}
45+
46+
if err != nil {
47+
fmt.Printf("error formatting output: %#v\n", err)
48+
return C.CString("")
49+
}
50+
51+
return C.CString(string(formatted))
52+
}
53+
54+
func main() {}

go.mod

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,21 @@ module github.com/replicatedhq/troubleshoot
33
go 1.12
44

55
require (
6-
github.com/Masterminds/goutils v1.1.0 // indirect
7-
github.com/Masterminds/semver v1.4.2 // indirect
8-
github.com/Masterminds/sprig v2.20.0+incompatible // indirect
96
github.com/ahmetalpbalkan/go-cursor v0.0.0-20131010032410-8136607ea412
10-
github.com/andrewchambers/go-jqpipe v0.0.0-20180509223707-2d54cef8cd94 // indirect
117
github.com/blang/semver v3.5.1+incompatible
12-
github.com/docker/distribution v2.7.1+incompatible // indirect
13-
github.com/docker/docker v1.13.1 // indirect
14-
github.com/docker/go-connections v0.4.0 // indirect
15-
github.com/docker/go-units v0.4.0 // indirect
16-
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
17-
github.com/dsnet/compress v0.0.1 // indirect
188
github.com/fatih/color v1.7.0
199
github.com/gin-gonic/gin v1.4.0
2010
github.com/gizak/termui/v3 v3.1.0
21-
github.com/golang/snappy v0.0.1 // indirect
22-
github.com/hashicorp/go-getter v1.3.0
11+
github.com/hashicorp/go-getter v1.3.1-0.20190627223108-da0323b9545e
2312
github.com/hashicorp/go-multierror v1.0.0
24-
github.com/huandu/xstrings v1.2.0 // indirect
25-
github.com/manifoldco/promptui v0.3.2 // indirect
2613
github.com/mholt/archiver v3.1.1+incompatible
27-
github.com/nwaples/rardecode v1.0.0 // indirect
2814
github.com/onsi/gomega v1.5.0
29-
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
30-
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
31-
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
3215
github.com/pkg/errors v0.8.1
33-
github.com/spf13/cobra v0.0.4
16+
github.com/replicatedhq/kots v0.2.0 // indirect
17+
github.com/spf13/cobra v0.0.5
3418
github.com/spf13/viper v1.4.0
3519
github.com/stretchr/testify v1.3.0
3620
github.com/tj/go-spin v1.1.0
37-
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
38-
github.com/xo/dburl v0.0.0-20190203050942-98997a05b24f // indirect
3921
golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
4022
gopkg.in/yaml.v2 v2.2.2
4123
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
@@ -47,6 +29,4 @@ require (
4729
k8s.io/kube-openapi v0.0.0-20190815110238-8ff09bc626d6 // indirect
4830
k8s.io/utils v0.0.0-20190809000727-6c36bc71fc4a // indirect
4931
sigs.k8s.io/controller-runtime v0.2.0-beta.2
50-
sigs.k8s.io/controller-tools v0.2.0-beta.2 // indirect
51-
sigs.k8s.io/kustomize v2.0.3+incompatible // indirect
5232
)

0 commit comments

Comments
 (0)