Skip to content

Commit

Permalink
Allow repos to be private / admin only
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Padilla committed Oct 5, 2021
1 parent b4d6c99 commit 5ff41c0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
16 changes: 13 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ type User struct {
}

type Repo struct {
Name string `yaml:"name"`
Repo string `yaml:"repo"`
Note string `yaml:"note"`
Name string `yaml:"name"`
Repo string `yaml:"repo"`
Note string `yaml:"note"`
Private bool `yaml:"private"`
}

func NewConfig(host string, port int, pk string, rs *git.RepoSource) (*Config, error) {
Expand Down Expand Up @@ -155,3 +156,12 @@ func (cfg *Config) createDefaultConfigRepo(yaml string) error {
}
return cfg.reload()
}

func (cfg *Config) isPrivate(repo string) bool {
for _, r := range cfg.Repos {
if r.Repo == repo {
return r.Private
}
}
return false
}
3 changes: 2 additions & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package config

const defaultReadme = "# Soft Serve\n\n Welcome! You can configure your Soft Serve server by cloning this repo and pushing changes.\n\n## Repos\n\n{{ range .Repos }}* {{ .Name }}{{ if .Note }} - {{ .Note }} {{ end }}\n - `git clone ssh://{{$.Host}}:{{$.Port}}/{{.Repo}}`\n{{ end }}"
const defaultReadme = "# Soft Serve\n\n Welcome! You can configure your Soft Serve server by cloning this repo and pushing changes.\n\n```\ngit clone ssh://{{.Host}}:{{.Port}}/config\n```"

const defaultConfig = `name: Soft Serve
host: %s
Expand All @@ -16,6 +16,7 @@ allow-no-keys: false
repos:
- name: Home
repo: config
private: true
note: "Configuration and content repo for this server"`

const hasKeyUserConfig = `
Expand Down
8 changes: 6 additions & 2 deletions config/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (cfg *Config) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool {
}

func (cfg *Config) accessForKey(repo string, pk ssh.PublicKey) gm.AccessLevel {
private := cfg.isPrivate(repo)
if repo == "config" {
private = true
}
for _, u := range cfg.Users {
apk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(u.PublicKey))
if err != nil {
Expand All @@ -50,12 +54,12 @@ func (cfg *Config) accessForKey(repo string, pk ssh.PublicKey) gm.AccessLevel {
return gm.ReadWriteAccess
}
}
if repo != "config" {
if !private {
return gm.ReadOnlyAccess
}
}
}
if repo == "config" && (cfg.AnonAccess != "read-write") {
if private && (cfg.AnonAccess != "read-write") {
return gm.NoAccess
}
switch cfg.AnonAccess {
Expand Down
4 changes: 4 additions & 0 deletions tui/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/soft/tui/bubbles/repo"
"github.com/charmbracelet/soft/tui/bubbles/selection"
"github.com/charmbracelet/soft/tui/style"
"github.com/gliderlabs/ssh"
)

type sessionState int
Expand All @@ -27,6 +28,7 @@ type SessionConfig struct {
Width int
Height int
InitialRepo string
Session ssh.Session
}

type MenuEntry struct {
Expand All @@ -49,6 +51,7 @@ type Bubble struct {
boxes []tea.Model
activeBox int
repoSelect *selection.Bubble
session ssh.Session
}

func NewBubble(cfg *config.Config, sCfg *SessionConfig) *Bubble {
Expand All @@ -60,6 +63,7 @@ func NewBubble(cfg *config.Config, sCfg *SessionConfig) *Bubble {
repoMenu: make([]MenuEntry, 0),
boxes: make([]tea.Model, 2),
initialRepo: sCfg.InitialRepo,
session: sCfg.Session,
}
b.state = startState
return b
Expand Down
27 changes: 15 additions & 12 deletions tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/soft/config"
br "github.com/charmbracelet/soft/tui/bubbles/repo"
"github.com/charmbracelet/soft/tui/bubbles/selection"
gm "github.com/charmbracelet/wish/git"
"github.com/muesli/termenv"
)

Expand Down Expand Up @@ -61,18 +62,20 @@ func (b *Bubble) setupCmd() tea.Msg {

func (b *Bubble) menuEntriesFromSource() ([]MenuEntry, error) {
mes := make([]MenuEntry, 0)
for _, r := range b.config.Repos {
me, err := b.newMenuEntry(r.Name, r.Repo)
if err != nil {
return nil, err
}
mes = append(mes, me)
}
rs := b.config.Source.AllRepos()
OUTER:
for _, r := range rs {
for _, me := range mes {
if r.Name == me.Repo {
acc := b.config.AuthRepo(r.Name, b.session.PublicKey())
if acc == gm.NoAccess && r.Name != "config" {
continue
}
for _, cr := range b.config.Repos {
if r.Name == cr.Repo {
me, err := b.newMenuEntry(cr.Name, cr.Repo)
if err != nil {
return nil, err
}
mes = append(mes, me)
continue OUTER
}
}
Expand All @@ -87,6 +90,9 @@ OUTER:

func (b *Bubble) newMenuEntry(name string, repo string) (MenuEntry, error) {
var tmplConfig *config.Config
if repo == "config" {
tmplConfig = b.config
}
me := MenuEntry{Name: name, Repo: repo}
width := b.width
boxLeftWidth := b.styles.Menu.GetWidth() + b.styles.Menu.GetHorizontalFrameSize()
Expand All @@ -95,9 +101,6 @@ func (b *Bubble) newMenuEntry(name string, repo string) (MenuEntry, error) {
lipgloss.Height(b.footerView()) +
b.styles.RepoBody.GetVerticalFrameSize() +
b.styles.App.GetVerticalMargins()
if repo == "config" {
tmplConfig = b.config
}
rb := br.NewBubble(
b.config.Source,
me.Repo,
Expand Down
2 changes: 1 addition & 1 deletion tui/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func SessionHandler(cfg *config.Config) func(ssh.Session) (tea.Model, []tea.ProgramOption) {
return func(s ssh.Session) (tea.Model, []tea.ProgramOption) {
cmd := s.Command()
scfg := &SessionConfig{}
scfg := &SessionConfig{Session: s}
switch len(cmd) {
case 0:
scfg.InitialRepo = ""
Expand Down

0 comments on commit 5ff41c0

Please sign in to comment.