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

refactor: fix all linter issues #27

Merged
merged 2 commits into from Jan 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion accesscontrol/accesscontrol.go
Expand Up @@ -24,7 +24,7 @@ func Middleware(cmds ...string) wish.Middleware {
}
}
fmt.Fprintln(s, "Command is not allowed: "+s.Command()[0])
s.Exit(1)
s.Exit(1) // nolint: errcheck
}
}
}
4 changes: 2 additions & 2 deletions activeterm/activeterm.go
Expand Up @@ -15,8 +15,8 @@ func Middleware() wish.Middleware {
_, _, active := s.Pty()
if !active {
fmt.Fprintln(s, "Requires an active PTY")
s.Exit(1)
return
s.Exit(1) // nolint: errcheck
return // unreachable
}
sh(s)
}
Expand Down
8 changes: 4 additions & 4 deletions bubbletea/tea.go
Expand Up @@ -10,17 +10,17 @@ import (
"github.com/muesli/termenv"
)

// BubbleTeaHander is the function Bubble Tea apps implement to hook into the
// BubbleTeaHandler is the function Bubble Tea apps implement to hook into the
// SSH Middleware. This will create a new tea.Program for every connection and
// start it with the tea.ProgramOptions returned.
//
// Deprecated: use Handler instead.
type BubbleTeaHandler func(ssh.Session) (tea.Model, []tea.ProgramOption) // nolint: revive
type BubbleTeaHandler = Handler // nolint: revive
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to make the deletion of this a 1 line change in the future :)


// Hander is the function Bubble Tea apps implement to hook into the
// Handler is the function Bubble Tea apps implement to hook into the
// SSH Middleware. This will create a new tea.Program for every connection and
// start it with the tea.ProgramOptions returned.
type Handler = BubbleTeaHandler
type Handler func(ssh.Session) (tea.Model, []tea.ProgramOption)

// Middleware takes a Handler and hooks the input and output for the
// ssh.Session into the tea.Program. It also captures window resize events and
Expand Down
36 changes: 24 additions & 12 deletions git/git.go
@@ -1,7 +1,6 @@
package git

import (
"context"
"fmt"
"os"
"os/exec"
Expand All @@ -19,35 +18,50 @@ var ErrNotAuthed = fmt.Errorf("you are not authorized to do this")
// ErrSystemMalfunction represents a general system error returned to clients.
var ErrSystemMalfunction = fmt.Errorf("something went wrong")

// ErrInvalidRepo represents an attempt to access a non-existent repo
// ErrInvalidRepo represents an attempt to access a non-existent repo.
var ErrInvalidRepo = fmt.Errorf("invalid repo")

// AccessLevel is the level of access allowed to a repo.
type AccessLevel int

const (
// NoAccess does not allow access to the repo.
NoAccess AccessLevel = iota

// ReadOnlyAccess allows read-only access to the repo.
ReadOnlyAccess

// ReadWriteAccess allows read and write access to the repo.
ReadWriteAccess

// AdminAccess allows read, write, and admin access to the repo.
AdminAccess
)

// GitHooks is an interface that allows for custom authorization
// implementations and post push/fetch notifications. Prior to git access,
// AuthRepo will be called with the ssh.Session public key and the repo name.
// Implementers return the appropriate AccessLevel.
type GitHooks interface {
//
// Deprecated: use Hooks instead.
type GitHooks = Hooks // nolint: revive

// Hooks is an interface that allows for custom authorization
// implementations and post push/fetch notifications. Prior to git access,
// AuthRepo will be called with the ssh.Session public key and the repo name.
// Implementers return the appropriate AccessLevel.
type Hooks interface {
AuthRepo(string, ssh.PublicKey) AccessLevel
Push(string, ssh.PublicKey)
Fetch(string, ssh.PublicKey)
}

// Middleware adds Git server functionality to the ssh.Server. Repos are stored
// in the specified repo directory. The provided GitHooks implementation will be
// in the specified repo directory. The provided Hooks implementation will be
// checked for access on a per repo basis for a ssh.Session public key.
// GitHooks.Push and GitHooks.Fetch will be called on successful completion of
// Hooks.Push and Hooks.Fetch will be called on successful completion of
// their commands.
func Middleware(repoDir string, gh GitHooks) wish.Middleware {
func Middleware(repoDir string, gh Hooks) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
cmd := s.Command()
Expand Down Expand Up @@ -94,8 +108,7 @@ func Middleware(repoDir string, gh GitHooks) wish.Middleware {
}

func gitReceivePack(s ssh.Session, gitCmd string, repoDir string, repo string) error {
ctx := s.Context()
err := ensureRepo(ctx, repoDir, repo)
err := ensureRepo(repoDir, repo)
if err != nil {
return err
}
Expand Down Expand Up @@ -147,10 +160,10 @@ func fatalGit(s ssh.Session, err error) {
msg := err.Error()
pktLine := fmt.Sprintf("%04x%s\n", len(msg)+5, msg)
_, _ = s.Write([]byte(pktLine))
s.Exit(1)
s.Exit(1) // nolint: errcheck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: a slightly easier way to communicate we intentionally ignore a returned error in these situations, is following what we do in the line above:

_ = s.Exit(1)

This also satisfies the linter. The same goes for srv.Close() below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed

}

func ensureRepo(ctx context.Context, dir string, repo string) error {
func ensureRepo(dir string, repo string) error {
exists, err := fileExists(dir)
if err != nil {
return err
Expand Down Expand Up @@ -180,8 +193,7 @@ func runCmd(s ssh.Session, dir, name string, args ...string) error {
usi.Dir = dir
usi.Stdout = s
usi.Stdin = s
err := usi.Run()
if err != nil {
if err := usi.Run(); err != nil {
return err
}
return nil
Expand Down
10 changes: 6 additions & 4 deletions testsession/testsession.go
Expand Up @@ -21,7 +21,9 @@ func New(tb testing.TB, srv *ssh.Server, cfg *gossh.ClientConfig) *gossh.Session
tb.Fatalf("failed to serve: %v", err)
}
}()
tb.Cleanup(func() { srv.Close() })
tb.Cleanup(func() {
srv.Close() // nolint: errcheck
})
return newClientSession(tb, l.Addr().String(), cfg)
}

Expand All @@ -47,7 +49,7 @@ func newClientSession(tb testing.TB, addr string, config *gossh.ClientConfig) *g
}
}
if config.HostKeyCallback == nil {
config.HostKeyCallback = gossh.InsecureIgnoreHostKey()
config.HostKeyCallback = gossh.InsecureIgnoreHostKey() // nolint: gosec
}
client, err := gossh.Dial("tcp", addr, config)
if err != nil {
Expand All @@ -58,8 +60,8 @@ func newClientSession(tb testing.TB, addr string, config *gossh.ClientConfig) *g
tb.Fatal(err)
}
tb.Cleanup(func() {
session.Close()
client.Close()
session.Close() // nolint: errcheck
client.Close() // nolint: errcheck
})
return session
}