Skip to content
Snippets Groups Projects
Commit edd4bb76 authored by Tamer Tas's avatar Tamer Tas
Browse files

Implement rename command

parent 890c22a5
No related branches found
No related tags found
No related merge requests found
package cmd
import (
"fmt"
"os"
cli "github.com/spf13/cobra"
"github.com/tmrts/boilr/pkg/boilr"
"github.com/tmrts/boilr/pkg/util/exit"
"github.com/tmrts/boilr/pkg/util/validate"
)
func renameTemplate(oldPath, newPath string) error {
if err := os.Rename(oldPath, newPath); err != nil {
return err
}
return nil
}
// Rename contains the cli-command for renaming templates in the template registry.
var Rename = &cli.Command{
Hidden: true,
Use: "rename <old-template-name> <new-template-name>",
Short: "Rename a project template",
Run: func(c *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{
{"old-template-name", validate.UnixPath},
{"new-template-name", validate.UnixPath},
})
tmplName, newTmplName := args[0], args[1]
if ok, err := TemplateInRegistry(tmplName); err != nil {
exit.Fatal(fmt.Errorf("rename: %s", err))
} else if !ok {
exit.Fatal(fmt.Errorf("Template %q couldn't be found in the template registry", tmplName))
}
tmplPath, err := boilr.TemplatePath(tmplName)
if err != nil {
exit.Fatal(fmt.Errorf("rename: %s", err))
}
newTmplPath, err := boilr.TemplatePath(newTmplName)
if err != nil {
exit.Fatal(fmt.Errorf("rename: %s", err))
}
if err := renameTemplate(tmplPath, newTmplPath); err != nil {
exit.Error(fmt.Errorf("rename: %s", err))
}
exit.OK("Successfully renamed the template %q to %q", tmplName, newTmplName)
},
}
...@@ -17,24 +17,26 @@ func Run() { ...@@ -17,24 +17,26 @@ func Run() {
Init.PersistentFlags().BoolP("force", "f", false, "Recreate directories if they exist") Init.PersistentFlags().BoolP("force", "f", false, "Recreate directories if they exist")
Root.AddCommand(Init) Root.AddCommand(Init)
Use.PersistentFlags().BoolP("use-defaults", "f", false, "Uses default values in project.json instead of prompting the user") Root.AddCommand(Delete)
Root.AddCommand(Use)
Save.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name")
Root.AddCommand(Save)
Download.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name") Download.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name")
Root.AddCommand(Download) Root.AddCommand(Download)
Root.AddCommand(Delete)
Root.AddCommand(List) Root.AddCommand(List)
Root.AddCommand(Rename)
Root.AddCommand(Report)
Save.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name")
Root.AddCommand(Save)
Use.PersistentFlags().BoolP("use-defaults", "f", false, "Uses default values in project.json instead of prompting the user")
Root.AddCommand(Use)
Root.AddCommand(Validate) Root.AddCommand(Validate)
Root.AddCommand(Version) Root.AddCommand(Version)
Root.AddCommand(Report)
Root.Execute() Root.Execute()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment