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

Implement an SSH config parser #181

Merged
merged 5 commits into from
Apr 4, 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
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ linters:
- forbidigo
- gochecknoinits
- gochecknoglobals
- gocognit # cyclop is used instead
- gocyclo # cyclop is used instead
- godox
- golint
- interfacer
Expand Down Expand Up @@ -50,8 +52,11 @@ linters-settings:
ignore-decls:
- w http.ResponseWriter
- r *http.Request
- r io.Reader
- i int
- n int
- j int
- ok bool
- p []byte
- mu sync.Mutex
- wg sync.WaitGroup
Expand All @@ -73,3 +78,9 @@ issues:
- EXC0015
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
- linters:
- errname # lots of false positives for an unknown reason
- revive # no need for comments on obvious vars like BooleanOptionYes
path: 'options/options\.go'

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Rig

[![GoDoc](https://godoc.org/github.com/k0sproject/rig?status.svg)](https://godoc.org/github.com/k0sproject/rig)
[![GoDoc](https://godoc.org/github.com/k0sproject/rig/v2/?status.svg)](https://godoc.org/github.com/k0sproject/rig/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/k0sproject/rig)](https://goreportcard.com/report/github.com/k0sproject/rig)
[![Build Status](https://travis-ci.com/k0sproject/rig.svg?branch=main)](https://travis-ci.com/k0sproject/rig)
[![codecov](https://codecov.io/gh/k0sproject/rig/branch/main/graph/badge.svg)](https://codecov.io/gh/k0sproject/rig)
Expand Down
12 changes: 7 additions & 5 deletions homedir/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
)

var errNotImplemented = errors.New("not implemented")

// Expand does ~/ style path expansion for files under current user home. ~user/ style paths are not supported.
func Expand(path string) (string, error) {
if !strings.HasPrefix(path, "~") {
return path, nil
func Expand(dir string) (string, error) {
if !strings.HasPrefix(dir, "~") {
return dir, nil
}

parts := strings.Split(path, string(os.PathSeparator))
parts := strings.Split(dir, string(os.PathSeparator))
if parts[0] != "~" {
return "", fmt.Errorf("%w: ~user/ style paths not supported", errNotImplemented)
}
Expand All @@ -27,8 +28,9 @@ func Expand(path string) (string, error) {
if err != nil {
return "", fmt.Errorf("homedir expand: %w", err)
}
home = strings.ReplaceAll(filepath.Clean(home), "\\", "/")

parts[0] = home

return filepath.Join(parts...), nil
return path.Join(parts...), nil
}
3 changes: 3 additions & 0 deletions homedir/expand_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

// Expand does ~/ style path expansion for files under current user home. On windows, this supports paths like %USERPROFILE%\path.
func Expand(path string) (string, error) {
if strings.Contains(path, "/") {
path = filepath.FromSlash(path)
}
parts := strings.Split(path, string(os.PathSeparator))
if parts[0] != "~" && parts[0] != "%USERPROFILE%" && parts[0] != "%userprofile%" && parts[0] != "%HOME%" && parts[0] != "%home%" {
return path, nil
Expand Down
5 changes: 5 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func SetTraceLogger(l TraceLogger) {
trace = sync.OnceValue(func() TraceLogger { return l })
}

// GetTraceLogger gets the current value of trace logger.
func GetTraceLogger() TraceLogger {
return trace()
}

// TraceLogger is a logger for rig's internal trace logging.
type TraceLogger interface {
Log(ctx context.Context, level slog.Level, msg string, keysAndValues ...any)
Expand Down
4 changes: 3 additions & 1 deletion sh/shellescape/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

A drop-in replacement for [alessio/shellescape](https://github.com/alessio/shellescape) / ["gopkg.in/alessio/shellescape.v1"]("gopkg.in/alessio/shellescape.v1").

It's a bit faster and allocates a little bit less. It's quite unlikely that anyone will notice any difference in a real-world application, the is here just to reduce dependencies of the `rig` package.
It's a tiny bit faster and allocates a tiny bit less. It's quite unlikely that anyone will notice any difference in a real-world application, the is here just to reduce dependencies of the `rig` package.

To use, replace `alessio/shellescape` with `github.com/k0sproject/rig/v2/sh/shellescape` in your imports.

In addition to the original package, this package also includes `Unquote`, `Split` and `Expand` (which supports `${var}`, `$var`, `$(cmd)` and `${var:-word}` and some other expansions).

## Benchmarks

Just out of curiosity.
Expand Down
Loading