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

Add new commands

parent 74d33721
No related branches found
No related tags found
No related merge requests found
package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
cli "github.com/spf13/cobra"
"github.com/tmrts/tmplt/pkg/tmplt"
"github.com/tmrts/tmplt/pkg/util/exit"
"github.com/tmrts/tmplt/pkg/util/inpututil"
"github.com/tmrts/tmplt/pkg/util/osutil"
)
var Save = &cli.Command{
Use: "save",
Short: "Saves a project template to template registry",
Run: func(_ *cli.Command, args []string) {
templateName, sourceDir := args[0], args[1]
targetDir := filepath.Join(tmplt.TemplateDirPath, templateName)
switch err := osutil.FileExists(targetDir); {
case os.IsNotExist(err):
break
case err == nil:
// Template Already Exists Ask If Should be Replaced
shouldOverride, err := inpututil.ScanYesOrNo("Template already exists. Override?", false)
if err != nil {
exit.Error(fmt.Errorf("save: %v", err))
}
if !shouldOverride {
exit.OK("Exiting")
}
default:
exit.Error(err)
}
if _, err := exec.Command("/usr/bin/cp", "-r", "--remove-destination", sourceDir, targetDir).Output(); err != nil {
// TODO create exec package
exit.Error(err)
}
},
}
package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
cli "github.com/spf13/cobra"
"github.com/tmrts/tmplt/pkg/template"
"github.com/tmrts/tmplt/pkg/tmplt"
"github.com/tmrts/tmplt/pkg/util/osutil"
)
var Use = &cli.Command{
......@@ -41,24 +37,3 @@ var Use = &cli.Command{
}
},
}
var Save = &cli.Command{
Use: "save",
Short: "Saves a project template to template registry",
Run: func(_ *cli.Command, args []string) {
templateName, sourceDir := args[0], args[1]
targetDir := filepath.Join(tmplt.TemplateDirPath, templateName)
switch err := osutil.FileExists(targetDir); {
case !os.IsNotExist(err):
// Template Already Exists Ask If Should be Replaced
panic(err)
}
if _, err := exec.Command("/usr/bin/cp", "-r", sourceDir, targetDir).Output(); err != nil {
fmt.Println(sourceDir, targetDir)
panic(err)
}
},
}
package cmd
import (
"errors"
"os"
"path/filepath"
cli "github.com/spf13/cobra"
)
var (
ErrTemplateInvalid = errors.New("verify: given template is invalid")
)
var Verify = &cli.Command{
Use: "verify",
Short: "Verifies whether a template is valid or not",
Run: func(_ *cli.Command, args []string) {
templatePath := args[0]
info, err := os.Stat(templatePath)
if err != nil {
panic(err)
}
if !info.IsDir() {
panic(ErrTemplateInvalid)
}
templateDirInfo, err := os.Stat(filepath.Join(templatePath, "template"))
if err != nil {
if os.IsNotExist(err) {
panic(ErrTemplateInvalid)
}
panic(err)
}
if !templateDirInfo.IsDir() {
panic(ErrTemplateInvalid)
}
},
}
package cmd
import (
"fmt"
cli "github.com/spf13/cobra"
"github.com/tmrts/tmplt/pkg/tmplt"
)
var Version = &cli.Command{
Use: "version",
Short: "Version information for tmplt",
Run: func(_ *cli.Command, args []string) {
fmt.Println("Current version is", tmplt.Version)
},
}
package main
import (
"github.com/spf13/cobra"
cli "github.com/spf13/cobra"
"github.com/tmrts/tmplt/pkg/cmd"
)
func main() {
mainCmd := &cobra.Command{
Use: "main",
mainCmd := &cli.Command{
Use: "tmplt",
}
mainCmd.AddCommand(cmd.Use)
mainCmd.AddCommand(cmd.Save)
mainCmd.AddCommand(cmd.Verify)
mainCmd.AddCommand(cmd.Version)
mainCmd.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