Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional config param for support guess envs, update fallback type & more docs #158

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/self-hosting.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The architecture of snips.sh was designed with self-hosting in mind, it's very s
- [Host Keys](#host-keys)
- [Limiting SSH Access](#limiting-ssh-access)
- [Statsd Metrics](#statsd-metrics)
- [Build without Tensorflow](#build-without-tensorflow)
- [Examples](#examples)
- [Docker Compose](#docker-compose)

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

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`.

### Build without Tensorflow

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.

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:

```
go build -tags noguesser
```

For `arm64` systems, this is automatic and will default to not compiling with the guesslang dependency.

## Examples

Expand Down
3 changes: 1 addition & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/url"
"os"
"runtime"
"text/tabwriter"
"time"

Expand Down Expand Up @@ -127,7 +126,7 @@ func Load() (*Config, error) {
return nil, err
}

cfg.EnableGuesser = cfg.EnableGuesser && runtime.GOARCH == "amd64"
cfg.EnableGuesser = cfg.EnableGuesser && GuessingSupported

return cfg, nil
}
5 changes: 5 additions & 0 deletions internal/config/config_guesser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build amd64 && !noguesser

package config

const GuessingSupported = true
6 changes: 6 additions & 0 deletions internal/config/config_noguesser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build arm64 || noguesser

package config

// https://github.com/robherley/snips.sh/issues/39
const GuessingSupported = false
33 changes: 29 additions & 4 deletions internal/renderer/guess.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
//go:build arm64 || noguesser
//go:build amd64 && !noguesser

package renderer

import (
"strings"
"time"

"github.com/armon/go-metrics"
"github.com/robherley/guesslang-go/pkg/guesser"
"github.com/rs/zerolog/log"
)

var guesslang *guesser.Guesser

func init() {
var err error
guesslang, err = guesser.New()
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize guesslang")
}
}

func Guess(content string) string {
// currently not supporting guessing in arm64 bc of libtensorflow requirements
// https://github.com/robherley/snips.sh/issues/39
panic("not implemented")
guessStart := time.Now()
answer, err := guesslang.Guess(content)
metrics.MeasureSince([]string{"guess", "duration"}, guessStart)
if err != nil {
log.Warn().Err(err).Msg("failed to guess the file type")
return ""
}

return strings.ToLower(answer.Predictions[0].Language)
}
34 changes: 0 additions & 34 deletions internal/renderer/guess_amd64.go

This file was deleted.

9 changes: 9 additions & 0 deletions internal/renderer/guess_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build arm64 || noguesser

package renderer

func Guess(content string) string {
// currently not supporting guessing in arm64 bc of libtensorflow requirements
// https://github.com/robherley/snips.sh/issues/39
panic("not implemented")
}
8 changes: 5 additions & 3 deletions internal/renderer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"github.com/alecthomas/chroma/v2/lexers"
)

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

func Analyze(content string) chroma.Lexer {
lexer := lexers.Analyse(content)
Expand Down
Loading