Skip to content

Commit

Permalink
Set gist visibility via Git push options (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Apr 2, 2024
1 parent 7a75c5e commit db6d6a5
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 212 deletions.
13 changes: 13 additions & 0 deletions docs/usage/git-push-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Push Options

Opengist has support for a few [Git push options](https://git-scm.com/docs/git-push#Documentation/git-push.txt--oltoptiongt).

These options are passed to `git push` command and can be used to change the metadata of a gist.

## Change visibility

```shell
git push -o visibility=public
git push -o visibility=unlisted
git push -o visibility=private
```
18 changes: 18 additions & 0 deletions internal/cli/hook.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cli

import (
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/config"
"github.com/thomiceli/opengist/internal/db"
"github.com/thomiceli/opengist/internal/hooks"
"github.com/urfave/cli/v2"
"io"
"os"
"path/filepath"
)

var CmdHook = cli.Command{
Expand All @@ -19,6 +24,7 @@ var CmdHookPreReceive = cli.Command{
Name: "pre-receive",
Usage: "Run Git server pre-receive hook for a repository",
Action: func(ctx *cli.Context) error {
initialize(ctx)
if err := hooks.PreReceive(os.Stdin, os.Stdout, os.Stderr); err != nil {
os.Exit(1)
}
Expand All @@ -30,9 +36,21 @@ var CmdHookPostReceive = cli.Command{
Name: "post-receive",
Usage: "Run Git server post-receive hook for a repository",
Action: func(ctx *cli.Context) error {
initialize(ctx)
if err := hooks.PostReceive(os.Stdin, os.Stdout, os.Stderr); err != nil {
os.Exit(1)
}
return nil
},
}

func initialize(ctx *cli.Context) {
if err := config.InitConfig(ctx.String("config"), io.Discard); err != nil {
panic(err)
}
config.InitLog()

if err := db.Setup(filepath.Join(config.GetHomeDir(), config.C.DBFilename), false); err != nil {
log.Fatal().Err(err).Msg("Failed to initialize database in hooks")
}
}
38 changes: 30 additions & 8 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func App() error {
func Initialize(ctx *cli.Context) {
fmt.Println("Opengist v" + config.OpengistVersion)

if err := config.InitConfig(ctx.String("config")); err != nil {
if err := config.InitConfig(ctx.String("config"), os.Stdout); err != nil {
panic(err)
}
if err := os.MkdirAll(filepath.Join(config.GetHomeDir()), 0755); err != nil {
Expand All @@ -83,8 +83,8 @@ func Initialize(ctx *cli.Context) {
homePath := config.GetHomeDir()
log.Info().Msg("Data directory: " + homePath)

if err := createSymlink(); err != nil {
log.Fatal().Err(err).Send()
if err := createSymlink(homePath, ctx.String("config")); err != nil {
log.Fatal().Err(err).Msg("Failed to create symlinks")
}

if err := os.MkdirAll(filepath.Join(homePath, "repos"), 0755); err != nil {
Expand Down Expand Up @@ -113,19 +113,41 @@ func Initialize(ctx *cli.Context) {
}
}

func createSymlink() error {
func createSymlink(homePath string, configPath string) error {
if err := os.MkdirAll(filepath.Join(homePath, "symlinks"), 0755); err != nil {
return err
}

exePath, err := os.Executable()
if err != nil {
return err
}

symlinkPath := path.Join(config.GetHomeDir(), "opengist-bin")
symlinkExePath := path.Join(config.GetHomeDir(), "symlinks", "opengist")
if _, err := os.Lstat(symlinkExePath); err == nil {
if err := os.Remove(symlinkExePath); err != nil {
return err
}
}
if err = os.Symlink(exePath, symlinkExePath); err != nil {
return err
}

if configPath == "" {
return nil
}

if _, err := os.Lstat(symlinkPath); err == nil {
if err := os.Remove(symlinkPath); err != nil {
configPath, _ = filepath.Abs(configPath)
configPath = filepath.Clean(configPath)
symlinkConfigPath := path.Join(config.GetHomeDir(), "symlinks", "config.yml")
if _, err := os.Lstat(symlinkConfigPath); err == nil {
if err := os.Remove(symlinkConfigPath); err != nil {
return err
}
}
if err = os.Symlink(configPath, symlinkConfigPath); err != nil {
return err
}

return os.Symlink(exePath, symlinkPath)
return nil
}
20 changes: 10 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ func configWithDefaults() (*config, error) {
return c, nil
}

func InitConfig(configPath string) error {
func InitConfig(configPath string, out io.Writer) error {
// Default values
c, err := configWithDefaults()
if err != nil {
return err
}

if err = loadConfigFromYaml(c, configPath); err != nil {
if err = loadConfigFromYaml(c, configPath, out); err != nil {
return err
}

if err = loadConfigFromEnv(c); err != nil {
if err = loadConfigFromEnv(c, out); err != nil {
return err
}

Expand Down Expand Up @@ -202,7 +202,7 @@ func GetHomeDir() string {
return filepath.Clean(absolutePath)
}

func loadConfigFromYaml(c *config, configPath string) error {
func loadConfigFromYaml(c *config, configPath string, out io.Writer) error {
if configPath != "" {
absolutePath, _ := filepath.Abs(configPath)
absolutePath = filepath.Clean(absolutePath)
Expand All @@ -211,9 +211,9 @@ func loadConfigFromYaml(c *config, configPath string) error {
if !os.IsNotExist(err) {
return err
}
fmt.Println("No YAML config file found at " + absolutePath)
_, _ = fmt.Fprintln(out, "No YAML config file found at "+absolutePath)
} else {
fmt.Println("Using YAML config file: " + absolutePath)
_, _ = fmt.Fprintln(out, "Using YAML config file: "+absolutePath)

// Override default values with values from config.yml
d := yaml.NewDecoder(file)
Expand All @@ -223,13 +223,13 @@ func loadConfigFromYaml(c *config, configPath string) error {
defer file.Close()
}
} else {
fmt.Println("No YAML config file specified.")
_, _ = fmt.Fprintln(out, "No YAML config file specified.")
}

return nil
}

func loadConfigFromEnv(c *config) error {
func loadConfigFromEnv(c *config, out io.Writer) error {
v := reflect.ValueOf(c).Elem()
var envVars []string

Expand Down Expand Up @@ -260,9 +260,9 @@ func loadConfigFromEnv(c *config) error {
}

if len(envVars) > 0 {
fmt.Println("Using environment variables config: " + strings.Join(envVars, ", "))
_, _ = fmt.Fprintln(out, "Using environment variables config: "+strings.Join(envVars, ", "))
} else {
fmt.Println("No environment variables config specified.")
_, _ = fmt.Fprintln(out, "No environment variables config specified.")
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/db/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func (v Visibility) Next() Visibility {

func ParseVisibility[T string | int](v T) (Visibility, error) {
switch s := fmt.Sprint(v); s {
case "0":
case "0", "public":
return PublicVisibility, nil
case "1":
case "1", "unlisted":
return UnlistedVisibility, nil
case "2":
case "2", "private":
return PrivateVisibility, nil
default:
return -1, fmt.Errorf("unknown visibility %q", s)
Expand Down
3 changes: 1 addition & 2 deletions internal/git/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var (
)

const truncateLimit = 2 << 18
const BaseHash = "0000000000000000000000000000000000000000"

type RevisionNotFoundError struct{}

Expand Down Expand Up @@ -565,5 +564,5 @@ func removeFilesExceptGit(dir string) error {
}

const hookTemplate = `#!/bin/sh
"$OG_OPENGIST_HOME_INTERNAL/opengist-bin" hook %s
"$OG_OPENGIST_HOME_INTERNAL/symlinks/opengist" --config=$OG_OPENGIST_HOME_INTERNAL/symlinks/config.yml hook %s
`

0 comments on commit db6d6a5

Please sign in to comment.