Skip to content

Commit

Permalink
Add some missing godocs
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Dec 10, 2021
1 parent ddff8b0 commit 3b51af4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cmd/soft/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import (
)

var (
Version = ""
// Version contains the application version number. It's set via ldflags
// when building.
Version = ""

// CommitSHA contains the SHA of the commit that this application was built
// against. It's set via lgflags when building.
CommitSHA = ""

version = flag.Bool("version", false, "display version")
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Callbacks interface {
Fetch(repo string)
}

// Config is the configuration for the soft-serve.
// Config is the configuration for Soft Serve.
type Config struct {
Host string `env:"SOFT_SERVE_HOST" default:""`
Port int `env:"SOFT_SERVE_PORT" default:"23231"`
Expand All @@ -34,6 +34,7 @@ func DefaultConfig() *Config {
return scfg.WithCallbacks(nil)
}

// WithCallbacks applies the given Callbacks to the configuration.
func (cfg *Config) WithCallbacks(c Callbacks) *Config {
cfg.Callbacks = c
return cfg
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
)

// Config is the Soft Serve configuration.
type Config struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Expand All @@ -28,20 +29,23 @@ type Config struct {
Cfg *config.Config
}

// User contains user-level configuration for a repository.
type User struct {
Name string `yaml:"name"`
Admin bool `yaml:"admin"`
PublicKeys []string `yaml:"public-keys"`
CollabRepos []string `yaml:"collab-repos"`
}

// Repo contains repository configuration information.
type Repo struct {
Name string `yaml:"name"`
Repo string `yaml:"repo"`
Note string `yaml:"note"`
Private bool `yaml:"private"`
}

// NewConfig creates a new internal Config struct.
func NewConfig(cfg *config.Config) (*Config, error) {
var anonAccess string
var yamlUsers string
Expand Down Expand Up @@ -84,6 +88,7 @@ func NewConfig(cfg *config.Config) (*Config, error) {
return c, nil
}

// Reload reloads the configuration.
func (cfg *Config) Reload() error {
err := cfg.Source.LoadRepos()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/config/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gliderlabs/ssh"
)

// Push registers Git push functionality for the given repo and key.
func (cfg *Config) Push(repo string, pk ssh.PublicKey) {
err := cfg.Reload()
if err != nil {
Expand All @@ -18,20 +19,25 @@ func (cfg *Config) Push(repo string, pk ssh.PublicKey) {
}
}

// Fetch registers Git fetch functionality for the given repo and key.
func (cfg *Config) Fetch(repo string, pk ssh.PublicKey) {
if cfg.Cfg.Callbacks != nil {
cfg.Cfg.Callbacks.Fetch(repo)
}
}

// AuthRepo grants repo authorization to the given key.
func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel {
return cfg.accessForKey(repo, pk)
}

// PasswordHandler returns whether or not password access is allowed.
func (cfg *Config) PasswordHandler(ctx ssh.Context, password string) bool {
return (cfg.AnonAccess != "no-access") && cfg.AllowKeyless
}

// PublicKeyHandler returns whether or not the given public key may access the
// repo.
func (cfg *Config) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool {
return cfg.accessForKey("", pk) != gm.NoAccess
}
Expand Down
12 changes: 12 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ import (
"github.com/go-git/go-git/v5/storage/memory"
)

// ErrMissingRepo indicates that the requested repository could not be found.
var ErrMissingRepo = errors.New("missing repo")

// Repo represents a Git repository.
type Repo struct {
Name string
Repository *git.Repository
Readme string
LastUpdated *time.Time
}

// RepoCommit contains metadata for a Git commit.
type RepoCommit struct {
Name string
Commit *object.Commit
}

// CommitLog is a series of Git commits.
type CommitLog []RepoCommit

func (cl CommitLog) Len() int { return len(cl) }
Expand All @@ -38,13 +42,15 @@ func (cl CommitLog) Less(i, j int) bool {
return cl[i].Commit.Author.When.After(cl[j].Commit.Author.When)
}

// RepoSource is a reference to an on-disk repositories.
type RepoSource struct {
Path string
mtx sync.Mutex
repos []*Repo
commits CommitLog
}

// NewRepoSource creates a new RepoSource.
func NewRepoSource(repoPath string) *RepoSource {
err := os.MkdirAll(repoPath, os.ModeDir|os.FileMode(0700))
if err != nil {
Expand All @@ -54,12 +60,14 @@ func NewRepoSource(repoPath string) *RepoSource {
return rs
}

// AllRepos returns all repositories for the given RepoSource.
func (rs *RepoSource) AllRepos() []*Repo {
rs.mtx.Lock()
defer rs.mtx.Unlock()
return rs.repos
}

// GetRepo returns a repository by name.
func (rs *RepoSource) GetRepo(name string) (*Repo, error) {
rs.mtx.Lock()
defer rs.mtx.Unlock()
Expand All @@ -71,6 +79,7 @@ func (rs *RepoSource) GetRepo(name string) (*Repo, error) {
return nil, ErrMissingRepo
}

// InitRepo initializes a new Git repository.
func (rs *RepoSource) InitRepo(name string, bare bool) (*Repo, error) {
rs.mtx.Lock()
defer rs.mtx.Unlock()
Expand All @@ -97,6 +106,7 @@ func (rs *RepoSource) InitRepo(name string, bare bool) (*Repo, error) {
return r, nil
}

// GetCommits returns commits for the repository.
func (rs *RepoSource) GetCommits(limit int) []RepoCommit {
rs.mtx.Lock()
defer rs.mtx.Unlock()
Expand All @@ -106,6 +116,7 @@ func (rs *RepoSource) GetCommits(limit int) []RepoCommit {
return rs.commits[:limit]
}

// LoadRepos opens Git repositories.
func (rs *RepoSource) LoadRepos() error {
rs.mtx.Lock()
defer rs.mtx.Unlock()
Expand Down Expand Up @@ -158,6 +169,7 @@ func (rs *RepoSource) loadRepo(name string, rg *git.Repository) (*Repo, error) {
return r, nil
}

// LatestFile returns the latest file at the specified path in the repository.
func (r *Repo) LatestFile(path string) (string, error) {
lg, err := r.Repository.Log(&git.LogOptions{})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/tui/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
// XXX: For now, this is in its own package so that it can be shared between
// different packages without incurring an illegal import cycle.

// Styles defines styles for the TUI.
type Styles struct {
ActiveBorderColor lipgloss.Color
InactiveBorderColor lipgloss.Color
Expand Down Expand Up @@ -39,6 +40,7 @@ type Styles struct {
ErrorBody lipgloss.Style
}

// DefaultStyles returns default styles for the TUI.
func DefaultStyles() *Styles {
s := new(Styles)

Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/gliderlabs/ssh"
)

// Server is the Soft Serve server.
type Server struct {
SSHServer *ssh.Server
Config *config.Config
Expand Down Expand Up @@ -53,10 +54,12 @@ func NewServer(cfg *config.Config) *Server {
}
}

// Reload reloads the server configuration.
func (srv *Server) Reload() error {
return srv.config.Reload()
}

// Start starts the SSH server.
func (srv *Server) Start() error {
return srv.SSHServer.ListenAndServe()
}

0 comments on commit 3b51af4

Please sign in to comment.