Skip to content

Commit

Permalink
feat: implement config rename
Browse files Browse the repository at this point in the history
  • Loading branch information
gchiesa committed Oct 20, 2024
1 parent 819446b commit 0e67d51
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
20 changes: 20 additions & 0 deletions cmd/config-rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"context"
"github.com/gchiesa/ska/pkg/skaffolder"
)

type ConfigRenameCmd struct {
Name string `arg:"-o,--name,required" help:"The name of the named configuration to rename"`
NewName string `arg:"-n,--new-name,required" help:"The new name to give to the named configuration"`
}

func (c *ConfigRenameCmd) Execute(ctx context.Context) error {
ska := skaffolder.NewSkaConfigTask(ctx.Value(configFolderPath("path")).(string))

if err := ska.RenameNamedConfig(c.Name, c.NewName); err != nil {
return err
}
return nil
}
9 changes: 7 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
)

type ConfigCmd struct {
*ConfigListCmd `arg:"subcommand:list"`
FolderPath string `arg:"-p,--path,required" help:"Local path where the .ska-config folder is located"`
*ConfigListCmd `arg:"subcommand:list"`
*ConfigRenameCmd `arg:"subcommand:rename"`
FolderPath string `arg:"-p,--path,required" help:"Local path where the .ska-config folder is located"`
}

type configFolderPath string
Expand All @@ -20,6 +21,10 @@ func (c *ConfigCmd) Execute(ctx context.Context) error {
if err := args.ConfigCmd.ConfigListCmd.Execute(configCtx); err != nil {
log.Fatalf("error executing config list command: %v", err)
}
case c.ConfigRenameCmd != nil:
if err := args.ConfigCmd.ConfigRenameCmd.Execute(configCtx); err != nil {
log.Fatalf("error executing config rename command: %v", err)
}
default:
fmt.Println("no subcommand specified, please use the --help flag to check available commands")
}
Expand Down
5 changes: 5 additions & 0 deletions internal/localconfigservice/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package localconfigservice
import (
"fmt"
"github.com/huandu/xstrings"
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -44,3 +45,7 @@ func configEntries(dirPath string) ([]string, error) {
}
return result, nil
}

func deleteConfigWithName(dirPath, name string) error {
return os.RemoveAll(filepath.Join(makeConfigPath(dirPath), makeConfigFileName(name)))
}
13 changes: 7 additions & 6 deletions internal/localconfigservice/localconfigservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ func (cs *LocalConfigService) WithVariables(variables map[string]interface{}) *L
return cs
}

func (cs *LocalConfigService) RenameNamedConfig(dirPath, namedConfig string) error {
if cs.ConfigExistsWithNamedConfig(dirPath, namedConfig) {
func (cs *LocalConfigService) RenameNamedConfig(dirPath, namedConfigNew string) error {
if cs.ConfigExistsWithNamedConfig(dirPath, namedConfigNew) {
return fmt.Errorf("cannot rename %s to %s because named configuration already exists on path: %s",
cs.namedConfig, namedConfig, dirPath)
cs.namedConfig, namedConfigNew, dirPath)
}

cs.namedConfig = namedConfig
oldConfig := cs.namedConfig
cs.namedConfig = namedConfigNew

if err := cs.WriteConfig(dirPath); err != nil {
return err
}
return cs.DeleteConfig(dirPath)
return deleteConfigWithName(dirPath, oldConfig)
}

func (cs *LocalConfigService) WriteConfig(dirPath string) error {
Expand Down Expand Up @@ -143,7 +144,7 @@ func (cs *LocalConfigService) ToJSON() ([]byte, error) {
}

func (cs *LocalConfigService) DeleteConfig(dirPath string) error {
return os.RemoveAll(makeConfigPath(dirPath))
return deleteConfigWithName(dirPath, cs.namedConfig)
}

func (cs *LocalConfigService) ReadValidConfig(dirPath string) error {
Expand Down
6 changes: 5 additions & 1 deletion pkg/skaffolder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ func (c *SkaConfigTask) RenameNamedConfig(name, newName string) error {
return err
}

return localConfig.RenameNamedConfig(c.BaseURI, newName)
err := localConfig.RenameNamedConfig(c.BaseURI, newName)
if err == nil {
c.Log.WithFields(log.Fields{"name": name, "newName": newName}).Infof("Renamed config from %s to %s", name, newName)
}
return err
}

func (c *SkaConfigTask) DeleteConfig(name string) error {
Expand Down

0 comments on commit 0e67d51

Please sign in to comment.