Skip to content

Commit

Permalink
added direnv
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 20, 2022
1 parent ebff91e commit 4ea196c
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 0 deletions.
23 changes: 23 additions & 0 deletions completers/direnv_completer/cmd/allow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var allowCmd = &cobra.Command{
Use: "allow",
Aliases: []string{"permit", "grant"},
Short: "Grants direnv permission to load the given .envrc or .env file",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(allowCmd).Standalone()

rootCmd.AddCommand(allowCmd)

carapace.Gen(allowCmd).PositionalCompletion(
carapace.ActionFiles(".envrc", ".env"),
)
}
24 changes: 24 additions & 0 deletions completers/direnv_completer/cmd/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/direnv"
"github.com/spf13/cobra"
)

var blockCmd = &cobra.Command{
Use: "block",
Aliases: []string{"deny", "revoke"},
Short: "Revokes the authorization of a given .envrc or .env file.",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(blockCmd).Standalone()

rootCmd.AddCommand(blockCmd)

carapace.Gen(blockCmd).PositionalCompletion(
direnv.ActionAuths(),
)
}
26 changes: 26 additions & 0 deletions completers/direnv_completer/cmd/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/direnv"
"github.com/spf13/cobra"
)

var editCmd = &cobra.Command{
Use: "edit",
Short: "Opens PATH_TO_RC or the current .envrc or .env into an $EDITOR and allow the file to be loaded afterwards",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(editCmd).Standalone()

rootCmd.AddCommand(editCmd)

carapace.Gen(editCmd).PositionalCompletion(
carapace.Batch(
direnv.ActionAuths(),
carapace.ActionFiles(),
).ToA(),
)
}
38 changes: 38 additions & 0 deletions completers/direnv_completer/cmd/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/bridge"
"github.com/rsteube/carapace-bin/pkg/actions/os"
"github.com/spf13/cobra"
)

var execCmd = &cobra.Command{
Use: "exec",
Short: "Executes a command after loading the first .envrc or .env found in DIR",
Run: func(cmd *cobra.Command, args []string) {},
DisableFlagParsing: true,
}

func init() {
carapace.Gen(execCmd).Standalone()

rootCmd.AddCommand(execCmd)

carapace.Gen(execCmd).PositionalCompletion(
carapace.ActionDirectories(),
carapace.Batch(
os.ActionPathExecutables(),
carapace.ActionFiles(),
).ToA(),
)

carapace.Gen(execCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
cmd := c.Args[1]
c.Args = c.Args[2:]
return bridge.ActionCarapaceBin(cmd).Invoke(c).ToA()
}),
)

}
18 changes: 18 additions & 0 deletions completers/direnv_completer/cmd/fetchurl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var fetchurlCmd = &cobra.Command{
Use: "fetchurl",
Short: "Fetches a given URL into direnv's CAS",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(fetchurlCmd).Standalone()

rootCmd.AddCommand(fetchurlCmd)
}
18 changes: 18 additions & 0 deletions completers/direnv_completer/cmd/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var helpCmd = &cobra.Command{
Use: "help",
Short: "show help",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(helpCmd).Standalone()

rootCmd.AddCommand(helpCmd)
}
22 changes: 22 additions & 0 deletions completers/direnv_completer/cmd/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var hookCmd = &cobra.Command{
Use: "hook",
Short: "Used to setup the shell hook",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(hookCmd).Standalone()

rootCmd.AddCommand(hookCmd)

carapace.Gen(hookCmd).PositionalCompletion(
carapace.ActionValues("bash", "elvish", "fish", "tcsh", "zsh"),
)
}
18 changes: 18 additions & 0 deletions completers/direnv_completer/cmd/prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var pruneCmd = &cobra.Command{
Use: "prune",
Short: "removes old allowed files",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(pruneCmd).Standalone()

rootCmd.AddCommand(pruneCmd)
}
18 changes: 18 additions & 0 deletions completers/direnv_completer/cmd/reload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var reloadCmd = &cobra.Command{
Use: "reload",
Short: "triggers an env reload",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(reloadCmd).Standalone()

rootCmd.AddCommand(reloadCmd)
}
22 changes: 22 additions & 0 deletions completers/direnv_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "direnv",
Short: "unclutter your .profile",
Long: "https://direnv.net/",
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()

rootCmd.Flags().Bool("help", false, "show help")
}
18 changes: 18 additions & 0 deletions completers/direnv_completer/cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var statusCmd = &cobra.Command{
Use: "status",
Short: "prints some debug status information",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(statusCmd).Standalone()

rootCmd.AddCommand(statusCmd)
}
18 changes: 18 additions & 0 deletions completers/direnv_completer/cmd/stdlib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var stdlibCmd = &cobra.Command{
Use: "stdlib",
Short: "Displays the stdlib available in the .envrc execution context",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(stdlibCmd).Standalone()

rootCmd.AddCommand(stdlibCmd)
}
18 changes: 18 additions & 0 deletions completers/direnv_completer/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "prints the version or checks that direnv is older than VERSION_AT_LEAST",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(versionCmd).Standalone()

rootCmd.AddCommand(versionCmd)
}
7 changes: 7 additions & 0 deletions completers/direnv_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/direnv_completer/cmd"

func main() {
cmd.Execute()
}
56 changes: 56 additions & 0 deletions pkg/actions/tools/direnv/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package direnv

import (
"bufio"
"fmt"
"os"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
)

// ActionAuths completes authorizations
//
// /project/dir/.envrc
// /home/user/project/.envrc
func ActionAuths() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// TODO support XDG folders: https://github.com/direnv/direnv/blob/729fbecd96f3e827575f19497bc01df33395d679/xdg/xdg.go
dir, err := c.Abs("~/.local/share/direnv/allow/")
if err != nil {
return carapace.ActionMessage(err.Error())
}

entries, err := os.ReadDir(dir)
if err != nil {
return carapace.ActionMessage(err.Error())
}

vals := make([]string, 0)
for _, entry := range entries {
if !entry.IsDir() {
auth, err := readFirstLine(dir + entry.Name())
if err != nil {
return carapace.ActionMessage(err.Error())
}

vals = append(vals, auth)
}
}
return carapace.ActionValues(vals...).StyleF(style.ForPath)
})
}

func readFirstLine(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()

scanner := bufio.NewScanner(file)
if scanner.Scan() {
return scanner.Text(), nil
}
return "", fmt.Errorf("file is empty: %v", path)
}

0 comments on commit 4ea196c

Please sign in to comment.