Skip to content

Commit

Permalink
Merge pull request #16 from eunomie/img-ref
Browse files Browse the repository at this point in the history
Image references
  • Loading branch information
eunomie authored Oct 14, 2024
2 parents 9d9c14f + 3dd76ac commit 67f4404
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
8 changes: 4 additions & 4 deletions internal/commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
return nil
}

action = selectAction(action, lc.Images[src], rk.Config.Default)
action = selectAction(action, src, rk.Config.Default)

if list || action == "" {
if tui.IsATTY(dockerCli.In().FD()) && len(rk.Config.Actions) > 0 {
Expand Down Expand Up @@ -176,7 +176,7 @@ func getValuesLocal(src, action string) map[string]string {
localOpts := make(map[string]string)

lc := runkit.GetLocalConfig()
img, ok := lc.Images[src]
img, ok := lc.Image(src)
if !ok {
return localOpts
}
Expand Down Expand Up @@ -235,12 +235,12 @@ func run(ctx context.Context, out io.Writer, src string, rk *runkit.RunKit, acti
return runnable.Run(ctx)
}

func selectAction(action string, conf runkit.ConfigImage, defaultAction string) string {
func selectAction(action, src, defaultAction string) string {
if action != "" {
return action
}

if conf.Default != "" {
if conf, ok := runkit.GetLocalConfig().Image(src); ok && conf.Default != "" {
return conf.Default
}

Expand Down
3 changes: 2 additions & 1 deletion runkit/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewLocalCache(cli command.Cli) *LocalCache {
}
}

func (c *LocalCache) Get(digest string) (*RunKit, error) {
func (c *LocalCache) Get(digest, src string) (*RunKit, error) {
rk := &RunKit{
Files: make(map[string]string),
}
Expand Down Expand Up @@ -66,6 +66,7 @@ func (c *LocalCache) Get(digest string) (*RunKit, error) {
}

if found {
rk.src = src
return rk, nil
}
return nil, nil
Expand Down
25 changes: 23 additions & 2 deletions runkit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"sync"

"github.com/google/go-containerregistry/pkg/name"
"gopkg.in/yaml.v2"
)

Expand All @@ -20,7 +21,7 @@ var (
readOnce = sync.Once{}
)

func GetLocalConfig() LocalConfig {
func GetLocalConfig() *LocalConfig {
readOnce.Do(func() {
lc, err := getLocalConfig()
if err != nil {
Expand All @@ -29,7 +30,7 @@ func GetLocalConfig() LocalConfig {
}
localConfig = lc
})
return localConfig
return &localConfig
}

func getLocalConfig() (LocalConfig, error) {
Expand Down Expand Up @@ -118,3 +119,23 @@ func read(filePath string) (LocalConfig, error) {

return config, nil
}

func (lc *LocalConfig) Image(src string) (*ConfigImage, bool) {
ref, err := name.ParseReference(src)
if err != nil {
return nil, false
}
refName := ref.Name()

for imgName, img := range lc.Images {
imgRef, err := name.ParseReference(imgName)
if err != nil {
continue
}
if imgRef.Name() == refName {
return &img, true
}
}

return nil, false
}
4 changes: 2 additions & 2 deletions runkit/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type (
}

Cache interface {
Get(digest string) (*RunKit, error)
Get(digest, src string) (*RunKit, error)
Set(digest string, runxConfig, runxDoc []byte) error
}
)
Expand Down Expand Up @@ -60,7 +60,7 @@ func Get(ctx context.Context, cache Cache, src string) (*RunKit, error) {

indexDigest = desc.Digest.String()

cached, err = cache.Get(indexDigest)
cached, err = cache.Get(indexDigest, src)
if err == nil && cached != nil {
return cached, nil
}
Expand Down

0 comments on commit 67f4404

Please sign in to comment.