Skip to content

Commit d42b200

Browse files
authored
Additional config param for support guess envs, update fallback type & more docs (#158)
1 parent 58fa08a commit d42b200

File tree

8 files changed

+67
-43
lines changed

8 files changed

+67
-43
lines changed

docs/self-hosting.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The architecture of snips.sh was designed with self-hosting in mind, it's very s
1010
- [Host Keys](#host-keys)
1111
- [Limiting SSH Access](#limiting-ssh-access)
1212
- [Statsd Metrics](#statsd-metrics)
13+
- [Build without Tensorflow](#build-without-tensorflow)
1314
- [Examples](#examples)
1415
- [Docker Compose](#docker-compose)
1516

@@ -143,6 +144,17 @@ ssh-import-id gh:robherley -o snips_authorized_keys
143144

144145
At runtime, snips.sh will emit various metrics if the `SNIPS_METRICS_STATSD` is defined. This should be the full udp address with the protocol, e.g. `udp://localhost:8125`.
145146

147+
### Build without Tensorflow
148+
149+
In order to "guess" what language a snippet is, snips.sh will use [guesslang](https://github.com/yoeo/guesslang) (specifically [guesslang-go](https://github.com/robherley/guesslang-go)). This requires the [`libtensorflow`](https://www.tensorflow.org/install/lang_c) C API.
150+
151+
If you do not want tensorflow as a runtime dependency (or if your environment does not support it) you can build without the guesser activated with the `noguesser` build tag:
152+
153+
```
154+
go build -tags noguesser
155+
```
156+
157+
For `arm64` systems, this is automatic and will default to not compiling with the guesslang dependency.
146158

147159
## Examples
148160

internal/config/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net/url"
77
"os"
8-
"runtime"
98
"text/tabwriter"
109
"time"
1110

@@ -127,7 +126,7 @@ func Load() (*Config, error) {
127126
return nil, err
128127
}
129128

130-
cfg.EnableGuesser = cfg.EnableGuesser && runtime.GOARCH == "amd64"
129+
cfg.EnableGuesser = cfg.EnableGuesser && GuessingSupported
131130

132131
return cfg, nil
133132
}

internal/config/config_guesser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build amd64 && !noguesser
2+
3+
package config
4+
5+
const GuessingSupported = true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build arm64 || noguesser
2+
3+
package config
4+
5+
// https://github.com/robherley/snips.sh/issues/39
6+
const GuessingSupported = false

internal/renderer/guess.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1-
//go:build arm64 || noguesser
1+
//go:build amd64 && !noguesser
22

33
package renderer
44

5+
import (
6+
"strings"
7+
"time"
8+
9+
"github.com/armon/go-metrics"
10+
"github.com/robherley/guesslang-go/pkg/guesser"
11+
"github.com/rs/zerolog/log"
12+
)
13+
14+
var guesslang *guesser.Guesser
15+
16+
func init() {
17+
var err error
18+
guesslang, err = guesser.New()
19+
if err != nil {
20+
log.Fatal().Err(err).Msg("failed to initialize guesslang")
21+
}
22+
}
23+
524
func Guess(content string) string {
6-
// currently not supporting guessing in arm64 bc of libtensorflow requirements
7-
// https://github.com/robherley/snips.sh/issues/39
8-
panic("not implemented")
25+
guessStart := time.Now()
26+
answer, err := guesslang.Guess(content)
27+
metrics.MeasureSince([]string{"guess", "duration"}, guessStart)
28+
if err != nil {
29+
log.Warn().Err(err).Msg("failed to guess the file type")
30+
return ""
31+
}
32+
33+
return strings.ToLower(answer.Predictions[0].Language)
934
}

internal/renderer/guess_amd64.go

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build arm64 || noguesser
2+
3+
package renderer
4+
5+
func Guess(content string) string {
6+
// currently not supporting guessing in arm64 bc of libtensorflow requirements
7+
// https://github.com/robherley/snips.sh/issues/39
8+
panic("not implemented")
9+
}

internal/renderer/lexer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"github.com/alecthomas/chroma/v2/lexers"
66
)
77

8-
var (
9-
FallbackLexer = lexers.Fallback
10-
)
8+
var FallbackLexer chroma.Lexer = chroma.MustNewLexer(&chroma.Config{
9+
Name: "plaintext",
10+
Filenames: []string{"*"},
11+
Priority: -1,
12+
}, lexers.PlaintextRules)
1113

1214
func Analyze(content string) chroma.Lexer {
1315
lexer := lexers.Analyse(content)

0 commit comments

Comments
 (0)