Skip to content

Commit

Permalink
feat: config aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Nov 11, 2024
1 parent 9d30e98 commit 8731be4
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 9 deletions.
17 changes: 12 additions & 5 deletions pkg/commands/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ func Execute(c *cli.Context) error {
return err
}

if c.Args().First() == "ekristen/distillery" {
_ = c.Set("include-pre-releases", "true")
inv := inventory.New(os.DirFS(cfg.BinPath), cfg.BinPath, cfg.GetOptPath(), cfg)

name := c.Args().First()
alias := cfg.GetAlias(c.Args().First())
if alias != nil {
name = alias.Name
_ = c.Set("version", alias.Version)
}

inv := inventory.New(os.DirFS(cfg.BinPath), cfg.BinPath, cfg.GetOptPath(), cfg)
if name == "ekristen/distillery" {
_ = c.Set("include-pre-releases", "true")
}

src, err := NewSource(c.Args().First(), &provider.Options{
src, err := NewSource(name, &provider.Options{
OS: c.String("os"),
Arch: c.String("arch"),
Config: cfg,
Expand Down Expand Up @@ -186,7 +193,7 @@ func Flags() []cli.Flag {
Aliases: []string{"c"},
Usage: "Specify the configuration file to use",
EnvVars: []string{"DISTILLERY_CONFIG"},
Value: filepath.Join(cfgDir, fmt.Sprintf("%s.toml", common.NAME)),
Value: filepath.Join(cfgDir, fmt.Sprintf("%s.yaml", common.NAME)),
},
&cli.StringFlag{
Name: "github-token",
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/install/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewSource(src string, opts *provider.Options) (provider.ISource, error) { /
}, nil
}

return nil, fmt.Errorf("invalid install source, expect format of owner/repo or owner/repo@version")
return nil, fmt.Errorf("invalid install source, expect alias or format of owner/repo or owner/repo@version")
}

if len(parts) == 2 {
Expand Down Expand Up @@ -95,7 +95,7 @@ func NewSource(src string, opts *provider.Options) (provider.ISource, error) { /
}, nil
}

return nil, fmt.Errorf("invalid install source, expect format of owner/repo or owner/repo@version")
return nil, fmt.Errorf("invalid install source, expect alias or format of owner/repo or owner/repo@version")
} else if len(parts) >= 3 {
if strings.HasPrefix(parts[0], "github") {
if parts[1] == source.HashicorpSource {
Expand Down
44 changes: 44 additions & 0 deletions pkg/config/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"strings"

"github.com/ekristen/distillery/pkg/common"
)

type Aliases map[string]*Alias

type Alias struct {
Name string `yaml:"name" toml:"name"`
Version string `yaml:"version" toml:"version"`
Bin string `yaml:"bin" toml:"bin"`
}

func (a *Alias) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value string
if unmarshal(&value) == nil {
p := strings.Split(value, "@")
a.Name = p[0]
a.Version = common.Latest
if len(p) > 1 {
a.Version = p[1]
}
return nil
}

type alias Alias
aux := (*alias)(a)
if err := unmarshal(aux); err != nil {
return err
}

return nil
}

func (a *Alias) UnmarshalText(b []byte) error {
*a = Alias{
Name: string(b),
Version: "latest",
}
return nil
}
18 changes: 16 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Config struct {

// Aliases - Allow for creating shorthand aliases for source locations that you use frequently. A good example
// of this is `distillery` -> `ekristen/distillery`
Aliases map[string]string `yaml:"aliases" toml:"aliases"`
Aliases *Aliases `yaml:"aliases" toml:"aliases"`

// Language - the language to use for the output of the application
Language string `yaml:"language" toml:"language"`
Expand All @@ -56,6 +56,20 @@ func (c *Config) GetOptPath() string {
return filepath.Join(c.Path, "opt")
}

func (c *Config) GetAlias(name string) *Alias {
if c.Aliases == nil {
return nil
}

for short, alias := range *c.Aliases {
if short == name {
return alias
}
}

return nil
}

func (c *Config) MkdirAll() error {
paths := []string{c.BinPath, c.GetOptPath(), c.CachePath, c.GetMetadataPath(), c.GetDownloadsPath()}

Expand Down Expand Up @@ -85,7 +99,7 @@ func (c *Config) Load(path string) error {
return toml.Unmarshal(data, c)
}

return nil
return fmt.Errorf("unknown configuration file suffix")
}

// New - create a new configuration object
Expand Down
49 changes: 49 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfigNewYAML(t *testing.T) {
cfg, err := New("testdata/base.yaml")
assert.NoError(t, err)

assert.Equal(t, "/home/test/.distillery", cfg.Path)
assert.Equal(t, "/home/test/.cache", cfg.CachePath)

aliases := &Aliases{
"dist": &Alias{
Name: "ekristen/distillery",
Version: "latest",
},
"aws-nuke": &Alias{
Name: "ekristen/aws-nuke",
Version: "3.29.3",
},
}

assert.EqualValues(t, aliases, cfg.Aliases)
}

func TestConfigNewTOML(t *testing.T) {
cfg, err := New("testdata/base.toml")
assert.NoError(t, err)

assert.Equal(t, "/home/test/.distillery", cfg.Path)
assert.Equal(t, "/home/test/.cache", cfg.CachePath)

aliases := &Aliases{
"dist": &Alias{
Name: "ekristen/distillery",
Version: "latest",
},
"aws-nuke": &Alias{
Name: "ekristen/aws-nuke",
Version: "3.29.3",
},
}

assert.EqualValues(t, aliases, cfg.Aliases)
}
9 changes: 9 additions & 0 deletions pkg/config/testdata/base.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
path = "/home/test/.distillery"
cache_path = "/home/test/.cache"

[aliases]
dist = "ekristen/distillery"

[aliases.aws-nuke]
name = "ekristen/aws-nuke"
version = "3.29.3"
7 changes: 7 additions & 0 deletions pkg/config/testdata/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
path: /home/test/.distillery
cache_path: /home/test/.cache
aliases:
dist: ekristen/distillery
aws-nuke:
name: ekristen/aws-nuke
version: 3.29.3

0 comments on commit 8731be4

Please sign in to comment.