Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd/skpr/alias/delete/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ func NewCommand() *cobra.Command {
},
}

cmd.Flags().StringVar(&command.Dir, "dir", ".skpr", "The skpr config directory.")

return cmd
}
6 changes: 2 additions & 4 deletions cmd/skpr/alias/list/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var (
cmdLong = `List all aliases.`

cmdExample = `
# List all aliases and specify the skpr config directory.
skpr alias list --dir="/path/to/.skpr"`
# List all aliases.
skpr alias list`
)

// NewCommand creates a new cobra.Command for `skpr alias list`.
Expand All @@ -30,7 +30,5 @@ func NewCommand() *cobra.Command {
},
}

cmd.Flags().StringVar(&command.Dir, "dir", ".skpr", "The skpr config directory.")

return cmd
}
8 changes: 4 additions & 4 deletions cmd/skpr/alias/set/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package set

import (
"strings"

"github.com/spf13/cobra"

v1set "github.com/skpr/cli/internal/command/alias/set"
Expand All @@ -23,19 +25,17 @@ func NewCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "set <alias> <expansion>",
Args: cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs),
Args: cobra.MinimumNArgs(2),
DisableFlagsInUseLine: true,
Short: "Set your alias",
Long: cmdLong,
Example: cmdExample,
RunE: func(cmd *cobra.Command, args []string) error {
command.Alias = args[0]
command.Expansion = args[1]
command.Expansion = strings.Join(args[1:], " ")
return command.Run()
},
}

cmd.Flags().StringVar(&command.Dir, "dir", ".skpr", "The skpr config directory.")

return cmd
}
65 changes: 62 additions & 3 deletions cmd/skpr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"context"
"fmt"
"os"
"os/exec"
"strings"

"github.com/charmbracelet/fang"
"github.com/charmbracelet/lipgloss/v2"
Expand All @@ -11,12 +14,12 @@ import (
"github.com/skpr/cli/cmd/skpr/alias"
"github.com/skpr/cli/cmd/skpr/backup"
"github.com/skpr/cli/cmd/skpr/config"
"github.com/skpr/cli/cmd/skpr/cron"
"github.com/skpr/cli/cmd/skpr/create"
"github.com/skpr/cli/cmd/skpr/cron"
"github.com/skpr/cli/cmd/skpr/daemon"
deletecmd "github.com/skpr/cli/cmd/skpr/delete"
"github.com/skpr/cli/cmd/skpr/deploy"
"github.com/skpr/cli/cmd/skpr/exec"
execcmd "github.com/skpr/cli/cmd/skpr/exec"
"github.com/skpr/cli/cmd/skpr/info"
"github.com/skpr/cli/cmd/skpr/list"
"github.com/skpr/cli/cmd/skpr/login"
Expand All @@ -29,9 +32,15 @@ import (
"github.com/skpr/cli/cmd/skpr/rsync"
"github.com/skpr/cli/cmd/skpr/shell"
"github.com/skpr/cli/cmd/skpr/version"
"github.com/skpr/cli/internal/client/config/user"
"github.com/skpr/cli/internal/color"
)

const (
// GroupAliases is the ID for the alias command group.
GroupAliases = "aliases"
)

const cmdExample = `
# Package application into container images.
skpr package 0.0.1
Expand Down Expand Up @@ -69,7 +78,7 @@ func main() {
cmd.AddCommand(daemon.NewCommand())
cmd.AddCommand(deletecmd.NewCommand())
cmd.AddCommand(deploy.NewCommand())
cmd.AddCommand(exec.NewCommand())
cmd.AddCommand(execcmd.NewCommand())
cmd.AddCommand(info.NewCommand())
cmd.AddCommand(list.NewCommand())
cmd.AddCommand(login.NewCommand())
Expand All @@ -83,6 +92,13 @@ func main() {
cmd.AddCommand(version.NewCommand())
cmd.AddCommand(release.NewCommand())

// Add user set aliases to the root command.
err := addAliases(cmd)
if err != nil {
fmt.Println("Failed to add alias commands:", err)
os.Exit(1)
}

if err := fang.Execute(context.Background(), cmd, fang.WithColorSchemeFunc(MyColorScheme)); err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -111,3 +127,46 @@ func MyColorScheme(ld lipgloss.LightDarkFunc) fang.ColorScheme {

return s
}

// Adds user defined aliases to the root command.
func addAliases(cmd *cobra.Command) error {
configFile, err := user.NewConfigFile()

// Only add aliases if we got an error.
if err == nil {
// Load the aliases.
aliases, err := configFile.ReadAliases()
if err != nil {
return fmt.Errorf("failed to read aliases: %w", err)
}

binPath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get skpr executable path: %w", err)
}

cmd.AddGroup(&cobra.Group{
ID: GroupAliases,
Title: "Alias Commands",
})

for k, v := range aliases {
cmd.AddCommand(&cobra.Command{
Use: k,
Short: fmt.Sprintf("Command: %s", v),
DisableFlagsInUseLine: true,
GroupID: GroupAliases,
RunE: func(cmd *cobra.Command, args []string) error {
e := exec.Command(binPath, strings.Split(v, " ")...)
e.Stdin = os.Stdin
e.Stdout = os.Stdout
e.Stderr = os.Stderr
e.Env = os.Environ()
return e.Run()
},
})
}
}

return nil
}
2 changes: 1 addition & 1 deletion internal/client/config/user/aliases/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Expand expands aliases in the list of args.
func Expand(args []string, aliases command.Aliases) (bool, []string, error) {
func Expand(args []string, aliases user.Aliases) (bool, []string, error) {
found := false
if len(args) == 0 {
return found, args, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/client/config/user/aliases/aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestExpandAliases(t *testing.T) {
args := []string{"mp", "dev"}
aliases := command.Aliases{
aliases := user.Aliases{
"mp": "mysql image pull",
}
found, newArgs, err := Expand(args, aliases)
Expand All @@ -20,7 +20,7 @@ func TestExpandAliases(t *testing.T) {
}

func TestExpandAliasesWithPlaceholder(t *testing.T) {
aliases := command.Aliases{
aliases := user.Aliases{
"mp": "mysql image pull $1",
"fs": "rsync $1:/data/app/sites/default/files $2",
}
Expand All @@ -40,7 +40,7 @@ func TestExpandAliasesWithPlaceholder(t *testing.T) {

func TestExpandNoArgs(t *testing.T) {
args := []string{}
aliases := command.Aliases{}
aliases := user.Aliases{}
found, newArgs, err := Expand(args, aliases)
assert.NoError(t, err)
assert.Len(t, newArgs, 0)
Expand Down
45 changes: 35 additions & 10 deletions internal/client/config/user/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package user

import (
"fmt"
Expand All @@ -18,20 +18,37 @@ type Aliases map[string]string

// ConfigFile is the config file.
type ConfigFile struct {
Filename string
Path string
}

// NewConfigFile creates a new config file.
func NewConfigFile(filename string) *ConfigFile {
return &ConfigFile{
Filename: filename,
func NewConfigFile() (*ConfigFile, error) {
configFile := &ConfigFile{}

homeDir, err := os.UserHomeDir()
if err != nil {
return configFile, err
}

configFile.Path = filepath.Join(homeDir, ".skpr", "config.yml")

return configFile, nil
}

// Read reads config from file.
func (c *ConfigFile) Read() (Config, error) {
var config Config
data, err := os.ReadFile(c.Filename)

exists, err := c.Exists()
if err != nil {
return config, err
}

if !exists {
return config, nil
}

data, err := os.ReadFile(c.Path)
if err != nil {
return config, fmt.Errorf("failed to read command config: %w", err)
}
Expand All @@ -40,18 +57,21 @@ func (c *ConfigFile) Read() (Config, error) {
if err != nil {
return config, fmt.Errorf("failed to unmarshal command config: %w", err)
}

return config, nil
}

// Exists checks if the config file exists.
func (c *ConfigFile) Exists() (bool, error) {
_, err := os.Lstat(c.Filename)
_, err := os.Lstat(c.Path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}

return false, err
}

return true, nil
}

Expand All @@ -62,16 +82,16 @@ func (c *ConfigFile) Write(config Config) error {
return fmt.Errorf("failed to marshal command config: %w", err)
}

dir := filepath.Dir(c.Filename)

// Ensure the directory exists.
dir := filepath.Dir(c.Path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.Mkdir(dir, 0700)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
}

err = os.WriteFile(c.Filename, data, 0600)
err = os.WriteFile(c.Path, data, 0600)
if err != nil {
return fmt.Errorf("failed to write command config to file: %w", err)
}
Expand All @@ -82,20 +102,25 @@ func (c *ConfigFile) Write(config Config) error {
// ReadAliases reads the Aliases from config.
func (c *ConfigFile) ReadAliases() (Aliases, error) {
aliases := Aliases{}

exists, err := c.Exists()
if err != nil {
return Aliases{}, err
}

if !exists {
return aliases, nil
}

cfg, err := c.Read()
if err != nil {
return aliases, err
}

// Ensure aliases are not nil.
if cfg.Aliases == nil {
return aliases, nil
}

return cfg.Aliases, nil
}
16 changes: 10 additions & 6 deletions internal/client/config/user/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package user

import (
"path/filepath"
Expand All @@ -8,15 +8,18 @@ import (
)

func TestReadWriteConfig(t *testing.T) {
filename := filepath.Join(t.TempDir(), ".skpr", "config.yml")
configFile := NewConfigFile(filename)
filePath := filepath.Join(t.TempDir(), ".skpr", "config.yml")

configFile := ConfigFile{Path: filePath}

config := Config{
Aliases: Aliases{"foo": "bar baz", "whiz": "whang woo"},
}

err := configFile.Write(config)
assert.NoError(t, err)

assert.FileExists(t, filename)
assert.FileExists(t, filePath)

newCfg, err := configFile.Read()
assert.NoError(t, err)
Expand All @@ -28,8 +31,9 @@ func TestReadWriteConfig(t *testing.T) {
}

func TestConfigExists(t *testing.T) {
filename := filepath.Join(t.TempDir(), ".skpr", "config.yml")
configFile := NewConfigFile(filename)
filePath := filepath.Join(t.TempDir(), ".skpr", "config.yml")

configFile := ConfigFile{Path: filePath}
exists, err := configFile.Exists()
assert.NoError(t, err)
assert.False(t, exists)
Expand Down
8 changes: 4 additions & 4 deletions internal/command/alias/delete/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package delete
import (
"context"
"fmt"
"path/filepath"

cmdconfig "github.com/skpr/cli/internal/client/config/user"
)

// Command struct.
type Command struct {
Dir string
Alias string
}

// Run the command.
func (cmd *Command) Run(ctx context.Context) error {
configPath := filepath.Join(cmd.Dir, "config.yml")
configFile := cmdconfig.NewConfigFile(configPath)
configFile, err := cmdconfig.NewConfigFile()
if err != nil {
return fmt.Errorf("could not get user config file: %w", err)
}

exists, err := configFile.Exists()
if err != nil {
Expand Down
Loading